Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.31% |
12 / 13 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
ZObjectRegistry | |
92.31% |
12 / 13 |
|
85.71% |
6 / 7 |
12.07 | |
0.00% |
0 / 1 |
__construct | n/a |
0 / 0 |
n/a |
0 / 0 |
2 | |||||
singleton | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
initialize | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
unregisterZid | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
clearAll | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unregister | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
clear | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isZidCached | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * WikiLambda ZObjectRegistry abstract class |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
8 | * @license MIT |
9 | */ |
10 | |
11 | namespace MediaWiki\Extension\WikiLambda\Registry; |
12 | |
13 | abstract class ZObjectRegistry { |
14 | |
15 | /** |
16 | * @var array |
17 | */ |
18 | protected $registry = []; |
19 | |
20 | /** |
21 | * @var string |
22 | */ |
23 | protected $type; |
24 | |
25 | /** |
26 | * @var array |
27 | */ |
28 | private static $instances = []; |
29 | |
30 | /** |
31 | * @codeCoverageIgnore |
32 | */ |
33 | private function __construct() { |
34 | $class = get_called_class(); |
35 | if ( !array_key_exists( $class, self::$instances ) ) { |
36 | /** |
37 | * @codeCoverageIgnore Our test suites always create these first in setUp(), so |
38 | * we never run this line in test coverage. |
39 | */ |
40 | $this->initialize(); |
41 | } |
42 | } |
43 | |
44 | final public static function singleton() { |
45 | $class = get_called_class(); |
46 | if ( !array_key_exists( $class, self::$instances ) ) { |
47 | self::$instances[ $class ] = new $class(); |
48 | } |
49 | return self::$instances[ $class ]; |
50 | } |
51 | |
52 | /** |
53 | * Initialize method, to be implemented by every registry class |
54 | */ |
55 | abstract protected function initialize(): void; |
56 | |
57 | /** |
58 | * Unregisters the zid from any of the existing registry instances |
59 | * |
60 | * @param string $zid |
61 | */ |
62 | public static function unregisterZid( string $zid ) { |
63 | foreach ( self::$instances as $class => $registry ) { |
64 | $registry->unregister( $zid ); |
65 | } |
66 | } |
67 | |
68 | /** |
69 | * Clears and re-initializes all existing registry instances |
70 | */ |
71 | public static function clearAll(): void { |
72 | foreach ( self::$instances as $class => $registry ) { |
73 | $registry->clear(); |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * Utility method to cache a key value in a registry instance, |
79 | * where the key is the Zid of the cached ZObject. (E.g. a ZLangRegistry |
80 | * will register the ZLanguage Zid and the language code string as its value) |
81 | * |
82 | * @param string $zid |
83 | * @param string $value |
84 | */ |
85 | public function register( string $zid, string $value ): void { |
86 | $this->registry[ $zid ] = $value; |
87 | } |
88 | |
89 | /** |
90 | * Utility method to remove a given Zid from a registry instance |
91 | * |
92 | * @param string $zid |
93 | */ |
94 | public function unregister( string $zid ): void { |
95 | unset( $this->registry[ $zid ] ); |
96 | } |
97 | |
98 | /** |
99 | * Utility method to clear the whole cache of a registry instance and |
100 | * set it to initial values. |
101 | */ |
102 | public function clear(): void { |
103 | $this->registry = []; |
104 | $this->initialize(); |
105 | } |
106 | |
107 | /** |
108 | * Utility method to check if the given Zid is cached in the registry. |
109 | * |
110 | * @param string $zid |
111 | * @return bool |
112 | */ |
113 | public function isZidCached( string $zid ): bool { |
114 | return array_key_exists( $zid, $this->registry ); |
115 | } |
116 | } |