Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Container | |
100.00% |
3 / 3 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | |
| 5 | namespace pvc\container; |
| 6 | |
| 7 | use Psr\Container\ContainerExceptionInterface; |
| 8 | use Psr\Container\ContainerInterface; |
| 9 | use Psr\Container\NotFoundExceptionInterface; |
| 10 | use pvc\interfaces\container\ContainerBuilderInterface; |
| 11 | use pvc\interfaces\container\DefinitionInterface; |
| 12 | |
| 13 | /** |
| 14 | * Class Container |
| 15 | * |
| 16 | * some container implementations provide a mechanism for creating a new instance of an object each time |
| 17 | * it is retrieved from the container. E.g. the concept of a factory is embedded in the definitions which |
| 18 | * fuel the container (see League\Container from the php League) |
| 19 | * |
| 20 | * Other container implementations do not. (see phhpdi). |
| 21 | * |
| 22 | * This discrepancy arises because of the PDS-11 specification which says that when you get an object |
| 23 | * from the container, it MAY or MAY NOT be the same instance each time. |
| 24 | * |
| 25 | * In my view, this is a flaw. The PSR should define a specific behavior. For example, if |
| 26 | * all containers should be able to make a new instance each time, then change the PSR to add |
| 27 | * a 'make' method to ContainerInterface. |
| 28 | * |
| 29 | * But since that is not currently the case, the pvc Container->get method will always return the same instance |
| 30 | * of an object because all implementations are capable of that much. Of course, if that object is |
| 31 | * a factory, then you can use that factory in your code to make as many new instances of something else as you want. |
| 32 | */ |
| 33 | class Container implements ContainerInterface |
| 34 | { |
| 35 | protected ContainerInterface $container; |
| 36 | |
| 37 | /** |
| 38 | * @param ContainerBuilderInterface $containerBuilder |
| 39 | * @param array<DefinitionInterface> $definitions |
| 40 | */ |
| 41 | public function __construct( |
| 42 | ContainerBuilderInterface $containerBuilder, |
| 43 | array $definitions |
| 44 | ) |
| 45 | { |
| 46 | $this->container = $containerBuilder->build($definitions); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * has |
| 51 | * @param string $id |
| 52 | * @return bool |
| 53 | */ |
| 54 | public function has(string $id): bool |
| 55 | { |
| 56 | return $this->container->has($id); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * get |
| 61 | * @param string $id |
| 62 | * @return mixed |
| 63 | * @throws ContainerExceptionInterface |
| 64 | * @throws NotFoundExceptionInterface |
| 65 | * successive calls to get will always return the same instance |
| 66 | */ |
| 67 | public function get(string $id): mixed |
| 68 | { |
| 69 | return $this->container->get($id); |
| 70 | } |
| 71 | } |