Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
28 / 34
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LaminasConfigAggregatorConfig
82.35% covered (warning)
82.35%
28 / 34
75.00% covered (warning)
75.00%
3 / 4
22.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __invoke
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 normalizeProviders
76.00% covered (warning)
76.00%
19 / 25
0.00% covered (danger)
0.00%
0 / 1
15.34
 normalizeResult
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3declare(strict_types=1);
4
5/**
6 * This file is part of php-fast-forward/config.
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/config
15 * @see       https://github.com/php-fast-forward
16 * @see       https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Config;
20
21use FastForward\Config\Helper\ConfigHelper;
22use Generator;
23use Laminas\ConfigAggregator\ConfigAggregator;
24use Laminas\ConfigAggregator\InvalidConfigProviderException;
25
26/**
27 * Class LaminasConfigAggregatorConfig.
28 *
29 * Integrates Laminas\ConfigAggregator for collecting configuration data from multiple providers.
30 * This class MUST support optional caching of the merged configuration using a defined file path.
31 * It SHALL return an ArrayConfig with the aggregated data upon invocation.
32 */
33class LaminasConfigAggregatorConfig implements ConfigInterface
34{
35    use LazyLoadConfigTrait;
36
37    /**
38     * Constructs the configuration aggregator.
39     *
40     * This constructor SHALL accept a set of configuration providers and an optional cache file path.
41     * Providers MUST be iterable and yield valid configuration arrays.
42     * Generating a cache file additionally requires a truthy config_cache_enabled entry in merged provider output.
43     *
44     * @param iterable $providers the configuration providers for aggregation
45     * @param string|null $cachedConfigFile the optional path to a cached config file
46     */
47    public function __construct(
48        private iterable $providers,
49        private ?string $cachedConfigFile = null,
50    ) {}
51
52    /**
53     * Aggregates configuration from the provided sources and returns a unified configuration object.
54     *
55     * If a cached config file is provided and exists, it SHALL be used to hydrate the result.
56     * Otherwise, all providers SHALL be executed to merge configurations.
57     *
58     * @return ConfigInterface the resulting configuration object as an ArrayConfig instance
59     */
60    public function __invoke(): ConfigInterface
61    {
62        $configAggregator = new ConfigAggregator($this->normalizeProviders(), $this->cachedConfigFile);
63
64        return new ArrayConfig(config: $configAggregator->getMergedConfig());
65    }
66
67    /**
68     * Normalizes each provider result before Laminas applies cross-source merging.
69     * Repeated provider identities retain the same wrapper for upstream validation.
70     *
71     * @return iterable
72     */
73    private function normalizeProviders(): iterable
74    {
75        $normalizers = [];
76
77        foreach ($this->providers as $provider) {
78            if (! \is_string($provider) && ! \is_callable($provider)) {
79                yield $provider;
80
81                continue;
82            }
83
84            $identity = match (true) {
85                \is_string($provider) => 'class:' . $provider,
86                \is_object($provider) => 'object:' . spl_object_id($provider),
87                default => null,
88            };
89
90            if (null !== $identity && isset($normalizers[$identity])) {
91                yield $normalizers[$identity];
92
93                continue;
94            }
95
96            $normalizer = static function () use ($provider): mixed {
97                if (\is_string($provider)) {
98                    if (! class_exists($provider)) {
99                        throw InvalidConfigProviderException::fromNamedProvider($provider);
100                    }
101
102                    $provider = new $provider();
103
104                    if (! \is_callable($provider)) {
105                        throw InvalidConfigProviderException::fromUnsupportedType(get_debug_type($provider));
106                    }
107                }
108
109                return self::normalizeResult($provider());
110            };
111
112            if (null !== $identity) {
113                $normalizers[$identity] = $normalizer;
114            }
115
116            yield $normalizer;
117        }
118    }
119
120    /**
121     * Normalizes individual arrays yielded by generator-based file providers too.
122     *
123     * @param mixed $config
124     *
125     * @return mixed
126     */
127    private static function normalizeResult(mixed $config): mixed
128    {
129        if ($config instanceof Generator) {
130            return (static function () use ($config): Generator {
131                foreach ($config as $values) {
132                    yield \is_array($values) ? ConfigHelper::normalize($values) : $values;
133                }
134            })();
135        }
136
137        return \is_array($config) ? ConfigHelper::normalize($config) : $config;
138    }
139}