Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| AutowireContainer | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
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 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/container. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/container |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Container; |
| 20 | |
| 21 | use FastForward\Container\Autowire\ReflectionContainer; |
| 22 | use Psr\Container\ContainerExceptionInterface; |
| 23 | use Psr\Container\ContainerInterface as PsrContainerInterface; |
| 24 | use Psr\Container\NotFoundExceptionInterface; |
| 25 | |
| 26 | /** |
| 27 | * Resolves services through an optional delegate and native constructor autowiring. |
| 28 | * |
| 29 | * Explicit delegate entries take precedence over autowiring. Without a delegate, |
| 30 | * an empty aggregate is created to resolve concrete dependency graphs directly. |
| 31 | */ |
| 32 | class AutowireContainer implements ContainerInterface |
| 33 | { |
| 34 | /** |
| 35 | * @var PsrContainerInterface the internal composite container with autowiring support |
| 36 | */ |
| 37 | private PsrContainerInterface $container; |
| 38 | |
| 39 | /** |
| 40 | * Constructs the AutowireContainer. |
| 41 | * |
| 42 | * Omission or null creates an empty AggregateContainer. An existing aggregate is |
| 43 | * reused; other PSR-11 containers are wrapped. Autowiring is appended last so |
| 44 | * delegate entries take precedence, including for nested dependencies. |
| 45 | * |
| 46 | * @param PsrContainerInterface|null $delegateContainer the optional delegate to wrap and extend |
| 47 | */ |
| 48 | public function __construct(?PsrContainerInterface $delegateContainer = null) |
| 49 | { |
| 50 | $delegateContainer ??= new AggregateContainer(); |
| 51 | |
| 52 | $aggregateContainer = $delegateContainer instanceof AggregateContainer |
| 53 | ? $delegateContainer |
| 54 | : new AggregateContainer($delegateContainer); |
| 55 | |
| 56 | $aggregateContainer->append(new ReflectionContainer($aggregateContainer)); |
| 57 | |
| 58 | $this->container = $aggregateContainer; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Retrieves an entry from the container by its identifier. |
| 63 | * |
| 64 | * @param string $id identifier of the entry to retrieve |
| 65 | * |
| 66 | * @return mixed the resolved entry |
| 67 | * |
| 68 | * @throws NotFoundExceptionInterface if the identifier is not found |
| 69 | * @throws ContainerExceptionInterface if the entry cannot be resolved |
| 70 | */ |
| 71 | public function get(string $id): mixed |
| 72 | { |
| 73 | return $this->container->get($id); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks whether the composed container knows the entry without calling get(). |
| 78 | * |
| 79 | * @param string $id the entry identifier |
| 80 | * |
| 81 | * @return bool whether an entry or instantiable class is known |
| 82 | * |
| 83 | * @throws ContainerExceptionInterface if delegated lookup or class loading fails |
| 84 | */ |
| 85 | public function has(string $id): bool |
| 86 | { |
| 87 | return $this->container->has($id); |
| 88 | } |
| 89 | } |