Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
18 / 18 |
CRAP | |
100.00% |
1 / 1 |
Manager | |
100.00% |
36 / 36 |
|
100.00% |
18 / 18 |
26 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
registerModules | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
register | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isRegistered | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasModule | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getModuleName | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
unregister | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
__set | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
__get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__isset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__unset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetSet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetGet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetExists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
offsetUnset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (https://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@noladev.com> |
7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
8 | * @license https://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Module; |
15 | |
16 | use ArrayAccess; |
17 | use ArrayIterator; |
18 | use Countable; |
19 | use IteratorAggregate; |
20 | |
21 | /** |
22 | * Module manager class |
23 | * |
24 | * @category Pop |
25 | * @package Pop\Module |
26 | * @author Nick Sagona, III <dev@noladev.com> |
27 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
28 | * @license https://www.popphp.org/license New BSD License |
29 | * @version 4.3.5 |
30 | */ |
31 | class Manager implements ArrayAccess, Countable, IteratorAggregate |
32 | { |
33 | |
34 | /** |
35 | * Modules |
36 | * @var array |
37 | */ |
38 | protected array $modules = []; |
39 | |
40 | /** |
41 | * Constructor |
42 | * |
43 | * Instantiate the module manager object. |
44 | * |
45 | * @param ?array $modules |
46 | * @throws Exception |
47 | */ |
48 | public function __construct(?array $modules = null) |
49 | { |
50 | if ($modules !== null) { |
51 | $this->registerModules($modules); |
52 | } |
53 | } |
54 | |
55 | /** |
56 | * Register module objects |
57 | * |
58 | * @param array $modules |
59 | * @throws Exception |
60 | * @return static |
61 | */ |
62 | public function registerModules(array $modules): static |
63 | { |
64 | foreach ($modules as $module) { |
65 | $this->register($module); |
66 | } |
67 | return $this; |
68 | } |
69 | |
70 | /** |
71 | * Register a module object |
72 | * |
73 | * @param ModuleInterface $module |
74 | * @return static |
75 | */ |
76 | public function register(ModuleInterface $module): static |
77 | { |
78 | $this->modules[$module->getName()] = $module; |
79 | return $this; |
80 | } |
81 | |
82 | /** |
83 | * Determine if a module object is registered with the manager by $name |
84 | * |
85 | * @param string $name |
86 | * @return bool |
87 | */ |
88 | public function isRegistered(string $name): bool |
89 | { |
90 | return isset($this->modules[$name]); |
91 | } |
92 | |
93 | /** |
94 | * Determine if a module object is registered with the manager by $module object comparison |
95 | * |
96 | * @param ModuleInterface $module |
97 | * @return bool |
98 | */ |
99 | public function hasModule(ModuleInterface $module): bool |
100 | { |
101 | $result = false; |
102 | |
103 | foreach ($this->modules as $name => $mod) { |
104 | if ($mod === $module) { |
105 | $result = true; |
106 | break; |
107 | } |
108 | } |
109 | |
110 | return $result; |
111 | } |
112 | |
113 | /** |
114 | * Get a module object's registered name |
115 | * |
116 | * @param ModuleInterface $module |
117 | * @return string |
118 | */ |
119 | public function getModuleName(ModuleInterface $module): string |
120 | { |
121 | $moduleName = null; |
122 | |
123 | foreach ($this->modules as $name => $mod) { |
124 | if ($mod === $module) { |
125 | $moduleName = $name; |
126 | break; |
127 | } |
128 | } |
129 | |
130 | return $moduleName; |
131 | } |
132 | |
133 | /** |
134 | * Get a module |
135 | * |
136 | * @param string $name |
137 | * @return mixed |
138 | */ |
139 | public function get(string $name): mixed |
140 | { |
141 | return $this->modules[$name] ?? null; |
142 | } |
143 | |
144 | /** |
145 | * Unregister a module |
146 | * |
147 | * @param string $name |
148 | * @return static |
149 | */ |
150 | public function unregister(string $name): static |
151 | { |
152 | if (isset($this->modules[$name])) { |
153 | unset($this->modules[$name]); |
154 | } |
155 | return $this; |
156 | } |
157 | |
158 | /** |
159 | * Register a module with the manager |
160 | * |
161 | * @param string $name |
162 | * @param mixed $value |
163 | * @throws Exception |
164 | * @return void |
165 | */ |
166 | public function __set(string $name, mixed $value): void |
167 | { |
168 | if ($value instanceof ModuleInterface) { |
169 | $value->setName($name); |
170 | } |
171 | $this->register($value); |
172 | } |
173 | |
174 | /** |
175 | * Get a registered module |
176 | * |
177 | * @param string $name |
178 | * @return mixed |
179 | */ |
180 | public function __get(string $name): mixed |
181 | { |
182 | return $this->get($name); |
183 | } |
184 | |
185 | /** |
186 | * Determine if a module is registered with the manager object |
187 | * |
188 | * @param string $name |
189 | * @return bool |
190 | */ |
191 | public function __isset(string $name): bool |
192 | { |
193 | return isset($this->modules[$name]); |
194 | } |
195 | |
196 | /** |
197 | * Unregister a module with the manager |
198 | * |
199 | * @param string $name |
200 | * @return void |
201 | */ |
202 | public function __unset(string $name): void |
203 | { |
204 | $this->unregister($name); |
205 | } |
206 | |
207 | /** |
208 | * Register a module with the manager |
209 | * |
210 | * @param mixed $offset |
211 | * @param mixed $value |
212 | * @throws Exception |
213 | * @return void |
214 | */ |
215 | public function offsetSet(mixed $offset, mixed $value): void |
216 | { |
217 | $this->__set($offset, $value); |
218 | } |
219 | |
220 | /** |
221 | * Get a registered module |
222 | * |
223 | * @param mixed $offset |
224 | * @return mixed |
225 | */ |
226 | public function offsetGet(mixed $offset): mixed |
227 | { |
228 | return $this->__get($offset); |
229 | } |
230 | |
231 | /** |
232 | * Determine if a module is registered with the manager object |
233 | * |
234 | * @param mixed $offset |
235 | * @return bool |
236 | */ |
237 | public function offsetExists(mixed $offset): bool |
238 | { |
239 | return $this->__isset($offset); |
240 | } |
241 | |
242 | /** |
243 | * Unregister a module with the manager |
244 | * |
245 | * @param mixed $offset |
246 | * @return void |
247 | */ |
248 | public function offsetUnset(mixed $offset): void |
249 | { |
250 | $this->__unset($offset); |
251 | } |
252 | |
253 | /** |
254 | * Return count |
255 | * |
256 | * @return int |
257 | */ |
258 | public function count(): int |
259 | { |
260 | return count($this->modules); |
261 | } |
262 | |
263 | /** |
264 | * Get iterator |
265 | * |
266 | * @return ArrayIterator |
267 | */ |
268 | public function getIterator(): ArrayIterator |
269 | { |
270 | return new ArrayIterator($this->modules); |
271 | } |
272 | |
273 | } |