Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
62 / 62 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
| Module | |
100.00% |
62 / 62 |
|
100.00% |
10 / 10 |
52 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
13 | |||
| register | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
25 | |||
| __set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| __get | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| __isset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| __unset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| 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 | |||
| 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 Pop\Application; |
| 17 | use Pop\Utils\Helper; |
| 18 | |
| 19 | /** |
| 20 | * Pop module class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Module |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 4.3.7 |
| 28 | * @property $config mixed |
| 29 | */ |
| 30 | class Module extends AbstractModule implements \ArrayAccess |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Constructor |
| 35 | * |
| 36 | * Instantiate a module object |
| 37 | * |
| 38 | * Optional parameters are an application instance or a configuration object or array |
| 39 | */ |
| 40 | public function __construct() |
| 41 | { |
| 42 | $args = func_get_args(); |
| 43 | $application = null; |
| 44 | $config = null; |
| 45 | $name = null; |
| 46 | $version = null; |
| 47 | |
| 48 | foreach ($args as $arg) { |
| 49 | if ($arg instanceof Application) { |
| 50 | $application = $arg; |
| 51 | } else if (is_array($arg) || ($arg instanceof \ArrayAccess) || ($arg instanceof \ArrayObject)) { |
| 52 | $config = $arg; |
| 53 | } else if (preg_match('/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(\+[0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*)?$/', $arg)) { |
| 54 | $version = $arg; |
| 55 | } else if (is_string($arg)) { |
| 56 | $name = $arg; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if ($name !== null) { |
| 61 | $this->setName($name); |
| 62 | } else if ($this->name === null) { |
| 63 | $this->setName(str_replace('\\', '_', strtolower(get_called_class()))); |
| 64 | } |
| 65 | |
| 66 | if ($version !== null) { |
| 67 | $this->setVersion($version); |
| 68 | } |
| 69 | |
| 70 | if ($config !== null) { |
| 71 | $this->registerConfig($config); |
| 72 | } |
| 73 | |
| 74 | if ($application !== null) { |
| 75 | $this->register($application); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Register module |
| 81 | * |
| 82 | * @param Application $application |
| 83 | * @throws Exception|\Pop\Service\Exception |
| 84 | * @return static |
| 85 | */ |
| 86 | public function register(Application $application): static |
| 87 | { |
| 88 | $this->application = $application; |
| 89 | |
| 90 | if ($this->config !== null) { |
| 91 | // Set the name, if available |
| 92 | if (isset($this->config['name'])) { |
| 93 | $this->setName($this->config['name']); |
| 94 | } |
| 95 | |
| 96 | // Set the version, if available |
| 97 | if (!empty($this->config['version'])) { |
| 98 | $this->setVersion($this->config['version']); |
| 99 | } |
| 100 | |
| 101 | // If the autoloader is set and the module config has a |
| 102 | // defined prefix and src, register the module with the autoloader |
| 103 | if (($this->application !== null) && ($this->application->autoloader() !== null) && |
| 104 | isset($this->config['prefix']) && isset($this->config['src']) && file_exists($this->config['src']) |
| 105 | ) { |
| 106 | // Register as PSR-0 |
| 107 | if (isset($this->config['psr-0']) && ($this->config['psr-0'])) { |
| 108 | $this->application->autoloader()->add($this->config['prefix'], $this->config['src']); |
| 109 | // Else, default to PSR-4 |
| 110 | } else { |
| 111 | $this->application->autoloader()->addPsr4($this->config['prefix'], $this->config['src']); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // If routes are set in the module config, register them with the application |
| 116 | if (isset($this->config['routes']) && ($this->application !== null) && ($this->application->router() !== null)) { |
| 117 | $this->application->router()->addRoutes($this->config['routes']); |
| 118 | } |
| 119 | |
| 120 | // If services are set in the module config, register them with the application |
| 121 | if (isset($this->config['services']) && ($this->application !== null) && ($this->application->services() !== null)) { |
| 122 | foreach ($this->config['services'] as $name => $service) { |
| 123 | $this->application->setService($name, $service); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // If events are set in the app config, register them with the application |
| 128 | if (isset($this->config['events']) && ($this->application !== null) && ($this->application->events() !== null)) { |
| 129 | foreach ($this->config['events'] as $event) { |
| 130 | if (isset($event['name']) && isset($event['action'])) { |
| 131 | $this->application->on( |
| 132 | $event['name'], |
| 133 | $event['action'], |
| 134 | ((isset($event['priority'])) ? $event['priority'] : 0) |
| 135 | ); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | $this->application->modules->register($this); |
| 142 | |
| 143 | return $this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Set a pre-designated value in the module object |
| 148 | * |
| 149 | * @param string $name |
| 150 | * @param mixed $value |
| 151 | * @return void |
| 152 | */ |
| 153 | public function __set(string $name, mixed $value): void |
| 154 | { |
| 155 | if ($name == 'config') { |
| 156 | $this->registerConfig($value); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get a pre-designated value from the module object |
| 162 | * |
| 163 | * @param string $name |
| 164 | * @return mixed |
| 165 | */ |
| 166 | public function __get(string $name): mixed |
| 167 | { |
| 168 | return match ($name) { |
| 169 | 'config' => $this->config, |
| 170 | default => null, |
| 171 | }; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Determine if a pre-designated value in the module object exists |
| 176 | * |
| 177 | * @param string $name |
| 178 | * @return bool |
| 179 | */ |
| 180 | public function __isset(string $name): bool |
| 181 | { |
| 182 | return match ($name) { |
| 183 | 'config' => ($this->config !== null), |
| 184 | default => false, |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Unset a pre-designated value in the module object |
| 190 | * |
| 191 | * @param string $name |
| 192 | * @return void |
| 193 | */ |
| 194 | public function __unset(mixed $name): void |
| 195 | { |
| 196 | if ($name == 'config') { |
| 197 | $this->config = null; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Set a value in the array |
| 203 | * |
| 204 | * @param mixed $offset |
| 205 | * @param mixed $value |
| 206 | * @return void |
| 207 | */ |
| 208 | public function offsetSet(mixed $offset, mixed $value): void |
| 209 | { |
| 210 | $this->__set($offset, $value); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get a value from the array |
| 215 | * |
| 216 | * @param mixed $offset |
| 217 | * @return mixed |
| 218 | */ |
| 219 | public function offsetGet(mixed $offset): mixed |
| 220 | { |
| 221 | return $this->__get($offset); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Determine if a value exists |
| 226 | * |
| 227 | * @param mixed $offset |
| 228 | * @return bool |
| 229 | */ |
| 230 | public function offsetExists(mixed $offset): bool |
| 231 | { |
| 232 | return $this->__isset($offset); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Unset a value from the array |
| 237 | * |
| 238 | * @param mixed $offset |
| 239 | * @return void |
| 240 | */ |
| 241 | public function offsetUnset(mixed $offset): void |
| 242 | { |
| 243 | $this->__unset($offset); |
| 244 | } |
| 245 | |
| 246 | } |