Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZObjectRegistry
92.31% covered (success)
92.31%
12 / 13
85.71% covered (warning)
85.71%
6 / 7
12.07
0.00% covered (danger)
0.00%
0 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
2
 singleton
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 initialize
n/a
0 / 0
n/a
0 / 0
0
 unregisterZid
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 clearAll
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unregister
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clear
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isZidCached
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
11namespace MediaWiki\Extension\WikiLambda\Registry;
12
13abstract 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 = static::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(): static {
45        $class = static::class;
46        if ( !array_key_exists( $class, self::$instances ) ) {
47            // @phan-suppress-next-line PhanTypeInstantiateAbstract
48            self::$instances[ $class ] = new $class();
49        }
50        return self::$instances[ $class ];
51    }
52
53    /**
54     * Initialize method, to be implemented by every registry class
55     */
56    abstract protected function initialize(): void;
57
58    /**
59     * Unregisters the zid from any of the existing registry instances
60     *
61     * @param string $zid
62     */
63    public static function unregisterZid( string $zid ) {
64        foreach ( self::$instances as $class => $registry ) {
65            $registry->unregister( $zid );
66        }
67    }
68
69    /**
70     * Clears and re-initializes all existing registry instances
71     */
72    public static function clearAll(): void {
73        foreach ( self::$instances as $class => $registry ) {
74            $registry->clear();
75        }
76    }
77
78    /**
79     * Utility method to cache a key value in a registry instance,
80     * where the key is the Zid of the cached ZObject. (E.g. a ZLangRegistry
81     * will register the ZLanguage Zid and the language code string as its value)
82     *
83     * @param string $zid
84     * @param string $value
85     */
86    public function register( string $zid, string $value ): void {
87        $this->registry[ $zid ] = $value;
88    }
89
90    /**
91     * Utility method to remove a given Zid from a registry instance
92     *
93     * @param string $zid
94     */
95    public function unregister( string $zid ): void {
96        unset( $this->registry[ $zid ] );
97    }
98
99    /**
100     * Utility method to clear the whole cache of a registry instance and
101     * set it to initial values.
102     */
103    public function clear(): void {
104        $this->registry = [];
105        $this->initialize();
106    }
107
108    /**
109     * Utility method to check if the given Zid is cached in the registry.
110     *
111     * @param string $zid
112     * @return bool
113     */
114    public function isZidCached( string $zid ): bool {
115        return array_key_exists( $zid, $this->registry );
116    }
117}