Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| ContainerFactory | |
100.00% |
9 / 9 |
|
100.00% |
5 / 5 |
6 | |
100.00% |
1 / 1 |
| create | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| reset | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\Container; |
| 21 | |
| 22 | use DI\Container; |
| 23 | use FastForward\DevTools\Container\ServiceProvider\DevToolsServiceProvider; |
| 24 | use Psr\Container\ContainerInterface; |
| 25 | |
| 26 | /** |
| 27 | * Builds and caches the shared DevTools dependency injection container. |
| 28 | * |
| 29 | * The factory centralizes container bootstrapping so command traits and other |
| 30 | * internal helpers can resolve services without duplicating bootstrap logic or |
| 31 | * depending on the console application entrypoint. |
| 32 | */ |
| 33 | class ContainerFactory |
| 34 | { |
| 35 | private static ?Container $container = null; |
| 36 | |
| 37 | /** |
| 38 | * Creates or returns the shared DevTools container instance. |
| 39 | * |
| 40 | * @return ContainerInterface the shared container instance |
| 41 | */ |
| 42 | public static function create(): ContainerInterface |
| 43 | { |
| 44 | if (! self::$container instanceof Container) { |
| 45 | $serviceProvider = new DevToolsServiceProvider(); |
| 46 | self::$container = new Container($serviceProvider->getFactories()); |
| 47 | } |
| 48 | |
| 49 | return self::$container; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Resolves a service from the shared DevTools container. |
| 54 | * |
| 55 | * @template T |
| 56 | * |
| 57 | * @param string|class-string<T> $id the service identifier |
| 58 | * |
| 59 | * @return mixed|T the resolved service |
| 60 | */ |
| 61 | public static function get(string $id): mixed |
| 62 | { |
| 63 | return self::create()->get($id); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns whether the shared DevTools container can resolve a service. |
| 68 | * |
| 69 | * @param string $id the service identifier |
| 70 | */ |
| 71 | public static function has(string $id): bool |
| 72 | { |
| 73 | return self::create()->has($id); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Overrides a shared service entry for the current process. |
| 78 | * |
| 79 | * @internal this method exists so tests can replace container entries with doubles |
| 80 | * |
| 81 | * @param string $id the service identifier |
| 82 | * @param mixed $value the replacement service entry |
| 83 | */ |
| 84 | public static function set(string $id, mixed $value): void |
| 85 | { |
| 86 | self::create(); |
| 87 | self::$container?->set($id, $value); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Resets the cached shared container instance. |
| 92 | * |
| 93 | * @internal this method exists so tests can isolate container state between test cases |
| 94 | */ |
| 95 | public static function reset(): void |
| 96 | { |
| 97 | self::$container = null; |
| 98 | } |
| 99 | } |