Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
AggregateServiceProvider
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFactories
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
1
 getExtensions
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
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\ServiceProvider;
20
21use FastForward\Container\Exception\RuntimeException;
22use FastForward\Container\Extension\ServiceExtension;
23use FastForward\Container\Factory\ServiceFactory;
24use Interop\Container\ServiceProviderInterface;
25use Psr\Container\ContainerInterface;
26
27/**
28 * Aggregates multiple service providers into a single provider.
29 *
30 * This class MUST be used to compose a unified list of factories and extensions
31 * from several ServiceProviderInterface implementations.
32 *
33 * Factories and extensions returned by this class are merged in registration order.
34 */
35class AggregateServiceProvider implements ServiceProviderInterface
36{
37    /**
38     * @var ServiceProviderInterface[] list of service providers to aggregate
39     */
40    private  array $serviceProviders;
41
42    /**
43     * Constructs the AggregateServiceProvider.
44     *
45     * @param ServiceProviderInterface ...$serviceProviders One or more service providers to aggregate.
46     */
47    public function __construct(ServiceProviderInterface ...$serviceProviders)
48    {
49        $this->serviceProviders = $serviceProviders;
50    }
51
52    /**
53     * Retrieves all service factories from aggregated providers.
54     *
55     * This method merges the factories from each service provider into a single array.
56     * The factory for this class itself is added under the key of its class name.
57     *
58     * @return array<string, callable> an associative array of service factories
59     */
60    public function getFactories(): array
61    {
62        $serviceProviders = array_reduce(
63            $this->serviceProviders,
64            static fn(array $carry, ServiceProviderInterface $provider): array => $carry + [
65                $provider::class => new ServiceFactory($provider),
66            ],
67            [
68                static::class => new ServiceFactory($this),
69            ],
70        );
71
72        return array_reduce(
73            $this->serviceProviders,
74            static fn($factories, $serviceProvider): array => array_replace(
75                $factories,
76                $serviceProvider->getFactories()
77            ),
78            $serviceProviders,
79        );
80    }
81
82    /**
83     * Retrieves all service extensions from aggregated providers.
84     *
85     * This method merges extensions from each provider. If multiple extensions exist for
86     * the same service ID, they are composed in the order they are added using nested closures.
87     *
88     * @return array<string, callable> an associative array of service extensions
89     *
90     * @throws \RuntimeException if any extension is not callable
91     */
92    public function getExtensions(): array
93    {
94        return array_reduce(
95            $this->serviceProviders,
96            static function (array $extensions, ServiceProviderInterface $serviceProvider): array {
97                foreach ($serviceProvider->getExtensions() as $id => $extension) {
98                    if (! \is_callable($extension)) {
99                        throw RuntimeException::forNonCallableExtension($id, get_debug_type($extension));
100                    }
101
102                    $extension = new ServiceExtension($extension);
103
104                    $extensions[$id] = \array_key_exists($id, $extensions)
105                        ? static fn(ContainerInterface $container, $previous): mixed => $extension(
106                            $container,
107                            $extensions[$id]($container, $previous)
108                        )
109                        : $extension;
110                }
111
112                return $extensions;
113            },
114            []
115        );
116    }
117}