Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AutowireContainer
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(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
19namespace FastForward\Container;
20
21use Throwable;
22use DI\Container;
23use Psr\Container\ContainerExceptionInterface;
24use Psr\Container\ContainerInterface as PsrContainerInterface;
25use Psr\Container\NotFoundExceptionInterface;
26
27/**
28 * A composite container implementation that wraps another PSR-11 container and appends
29 * an internal PHP-DI autowiring container.
30 *
31 * It provides auto-resolution of services while maintaining compatibility with
32 * pre-defined service providers.
33 *
34 * This container MUST be used in scenarios where automatic dependency resolution
35 * via autowiring is required alongside explicitly registered services.
36 */
37  class AutowireContainer implements ContainerInterface
38{
39    /**
40     * @var PsrContainerInterface the internal composite container with autowiring support
41     */
42    private PsrContainerInterface $container;
43
44    /**
45     * Constructs the AutowireContainer.
46     *
47     * If the provided container is not an AggregateContainer, it is wrapped within one.
48     * A PHP-DI container is appended to the aggregate to support autowiring.
49     *
50     * @param PsrContainerInterface $delegateContainer the delegate container to wrap and extend
51     */
52    public function __construct(PsrContainerInterface $delegateContainer)
53    {
54        $aggregateContainer = $delegateContainer instanceof AggregateContainer
55            ? $delegateContainer
56            : new AggregateContainer($delegateContainer);
57
58        $aggregateContainer->append(new Container(wrapperContainer: $this));
59
60        $this->container = $aggregateContainer;
61    }
62
63    /**
64     * Retrieves an entry from the container by its identifier.
65     *
66     * @param string $id identifier of the entry to retrieve
67     *
68     * @return mixed the resolved entry
69     *
70     * @throws NotFoundExceptionInterface if the identifier is not found
71     * @throws ContainerExceptionInterface if the entry cannot be resolved
72     */
73    public function get(string $id): mixed
74    {
75        return $this->container->get($id);
76    }
77
78    /**
79     * @param string $id
80     *
81     * @return bool
82     */
83    public function has(string $id): bool
84    {
85        if (! $this->container->has($id)) {
86            return false;
87        }
88
89        try {
90            // Attempt to resolve the service to check if it is valid
91            $this->get($id);
92        } catch (Throwable) {
93            return false;
94        }
95
96        return true;
97    }
98}