Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Registry | |
100.00% |
5 / 5 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Miniframe\Core; |
| 4 | |
| 5 | class Registry |
| 6 | { |
| 7 | /** |
| 8 | * Collection of all registered objects |
| 9 | * |
| 10 | * @var array<string, mixed> |
| 11 | */ |
| 12 | private static $services = array(); |
| 13 | |
| 14 | /** |
| 15 | * Registers a service |
| 16 | * |
| 17 | * @param string $name Name of the object that needs to be registered. |
| 18 | * @param mixed $service Reference to the object. |
| 19 | * |
| 20 | * @return void |
| 21 | */ |
| 22 | public static function register(string $name, $service): void |
| 23 | { |
| 24 | self::$services[$name] = $service; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns a service |
| 29 | * |
| 30 | * @param string $name Name of the registered object. |
| 31 | * |
| 32 | * @return mixed |
| 33 | */ |
| 34 | public static function get(string $name) |
| 35 | { |
| 36 | if (!isset(self::$services[$name])) { |
| 37 | throw new \RuntimeException('Middleware not registered: ' . $name); |
| 38 | } |
| 39 | return self::$services[$name]; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Validates if a service is registered |
| 44 | * |
| 45 | * @param string $name Name of the registered object. |
| 46 | * |
| 47 | * @return boolean |
| 48 | */ |
| 49 | public static function has(string $name): bool |
| 50 | { |
| 51 | return isset(self::$services[$name]); |
| 52 | } |
| 53 | } |