Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| Manager | |
100.00% |
19 / 19 |
|
100.00% |
8 / 8 |
13 | |
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% |
1 / 1 |
|
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 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unregister | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __set | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Module; |
| 16 | |
| 17 | use InvalidArgumentException; |
| 18 | use Pop\AbstractManager; |
| 19 | |
| 20 | /** |
| 21 | * Module manager class |
| 22 | * |
| 23 | * @category Pop |
| 24 | * @package Pop\Module |
| 25 | * @author Nick Sagona, III <nick@popphp.org> |
| 26 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 27 | * @license https://www.popphp.org/license New BSD License |
| 28 | * @version 5.0.0 |
| 29 | */ |
| 30 | class Manager extends AbstractManager |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * |
| 36 | * Instantiate the module manager object. |
| 37 | * |
| 38 | * @param ?array $modules |
| 39 | */ |
| 40 | public function __construct(?array $modules = null) |
| 41 | { |
| 42 | if ($modules !== null) { |
| 43 | $this->registerModules($modules); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Register module objects |
| 49 | * |
| 50 | * @param array $modules |
| 51 | * @return static |
| 52 | */ |
| 53 | public function registerModules(array $modules): static |
| 54 | { |
| 55 | foreach ($modules as $module) { |
| 56 | $this->register($module); |
| 57 | } |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Register a module object |
| 63 | * |
| 64 | * @param ModuleInterface $module |
| 65 | * @return static |
| 66 | */ |
| 67 | public function register(ModuleInterface $module): static |
| 68 | { |
| 69 | return $this->addItem($module, $module->getName()); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Determine if a module object is registered with the manager by $name |
| 74 | * |
| 75 | * @param string $name |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function isRegistered(string $name): bool |
| 79 | { |
| 80 | return $this->hasItem($name); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Determine if a module object is registered with the manager by $module object comparison |
| 85 | * |
| 86 | * @param ModuleInterface $module |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function hasModule(ModuleInterface $module): bool |
| 90 | { |
| 91 | $result = false; |
| 92 | |
| 93 | foreach ($this->items as $mod) { |
| 94 | if ($mod === $module) { |
| 95 | $result = true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return $result; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get a module |
| 105 | * |
| 106 | * @param string $name |
| 107 | * @return mixed |
| 108 | */ |
| 109 | public function get(string $name): mixed |
| 110 | { |
| 111 | return $this->getItem($name); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Unregister a module |
| 116 | * |
| 117 | * @param string $name |
| 118 | * @return static |
| 119 | */ |
| 120 | public function unregister(string $name): static |
| 121 | { |
| 122 | return $this->removeItem($name); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Register a module with the manager |
| 127 | * |
| 128 | * @param string $name |
| 129 | * @param mixed $value |
| 130 | * @return void |
| 131 | */ |
| 132 | public function __set(string $name, mixed $value): void |
| 133 | { |
| 134 | if (!($value instanceof ModuleInterface)) { |
| 135 | throw new InvalidArgumentException('Error: The value passed must be instance of ModuleInterface'); |
| 136 | } |
| 137 | $value->setName($name); |
| 138 | $this->register($value); |
| 139 | } |
| 140 | |
| 141 | } |