Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| LeagueContainerBuilder | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| build | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| buildSingle | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace pvc\container\container_builder; |
| 6 | |
| 7 | use League\Container\Container; |
| 8 | use League\Container\Definition\Definition as LeagueDefinition; |
| 9 | use League\Container\Definition\DefinitionAggregate; |
| 10 | use League\Container\ReflectionContainer; |
| 11 | use Psr\Container\ContainerInterface; |
| 12 | use pvc\container\defs\DefinitionCollection; |
| 13 | use pvc\interfaces\container\ContainerBuilderInterface; |
| 14 | use pvc\interfaces\container\DefinitionInterface; |
| 15 | |
| 16 | class LeagueContainerBuilder implements ContainerBuilderInterface |
| 17 | { |
| 18 | |
| 19 | public function __construct( |
| 20 | protected DefinitionCollection $definitionCollection, |
| 21 | ) {} |
| 22 | |
| 23 | /** |
| 24 | * build |
| 25 | * |
| 26 | * @param array<DefinitionInterface> $definitions |
| 27 | * |
| 28 | * @return ContainerInterface |
| 29 | */ |
| 30 | public function build(array $definitions): ContainerInterface |
| 31 | { |
| 32 | $this->definitionCollection->hydrate($definitions); |
| 33 | |
| 34 | $leagueDefinitionsArray = array_map([$this, 'buildSingle'], iterator_to_array($this->definitionCollection)); |
| 35 | $aggregate = new DefinitionAggregate($leagueDefinitionsArray); |
| 36 | $container = new Container($aggregate); |
| 37 | |
| 38 | /** |
| 39 | * add autowiring |
| 40 | */ |
| 41 | $container->delegate(new ReflectionContainer()); |
| 42 | |
| 43 | return $container; |
| 44 | } |
| 45 | |
| 46 | protected function buildSingle(DefinitionInterface $definition): LeagueDefinition |
| 47 | { |
| 48 | $leagueDefinition = new LeagueDefinition( |
| 49 | $definition->getAlias(), |
| 50 | $definition->getClassString(), |
| 51 | ); |
| 52 | |
| 53 | $leagueDefinition->addArguments($definition->getConstructorArgs()); |
| 54 | |
| 55 | foreach($definition->getMethodCalls() as $methodCall) { |
| 56 | $leagueDefinition->addMethodCall($methodCall->getMethodName(), $methodCall->getArguments()); |
| 57 | } |
| 58 | |
| 59 | return $leagueDefinition; |
| 60 | } |
| 61 | } |