Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
AggregateConfig | |
100.00% |
5 / 5 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__invoke | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(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 | * @link https://github.com/php-fast-forward/config |
12 | * @copyright Copyright (c) 2025 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
13 | * @license https://opensource.org/licenses/MIT MIT License |
14 | */ |
15 | |
16 | namespace FastForward\Config; |
17 | |
18 | /** |
19 | * Class AggregateConfig. |
20 | * |
21 | * Represents an aggregated configuration loader that combines multiple configuration sources. |
22 | * This class MUST be used to merge multiple configurations into a single, unified configuration structure. |
23 | * |
24 | * It SHALL lazily load and resolve configuration data only when invoked. |
25 | */ |
26 | final class AggregateConfig implements ConfigInterface |
27 | { |
28 | use LazyLoadConfigTrait; |
29 | |
30 | /** |
31 | * @var ConfigInterface[] A list of configuration providers to be aggregated. |
32 | * Each configuration MUST implement ConfigInterface. |
33 | */ |
34 | private readonly array $configs; |
35 | |
36 | /** |
37 | * AggregateConfig constructor. |
38 | * |
39 | * Constructs a new instance by accepting a variadic list of configuration objects. |
40 | * These configuration objects MUST implement the ConfigInterface. |
41 | * |
42 | * @param ConfigInterface ...$configs One or more configuration instances to aggregate. |
43 | */ |
44 | public function __construct(ConfigInterface ...$configs) |
45 | { |
46 | $this->configs = $configs; |
47 | } |
48 | |
49 | /** |
50 | * Invokes the configuration aggregator. |
51 | * |
52 | * This method SHALL initialize a new ArrayConfig instance and populate it |
53 | * with the values from each provided configuration source. |
54 | * |
55 | * It MUST return a fully merged configuration in the form of a ConfigInterface implementation. |
56 | * |
57 | * @return ConfigInterface the resulting merged configuration object |
58 | */ |
59 | public function __invoke(): ConfigInterface |
60 | { |
61 | $arrayConfig = new ArrayConfig(); |
62 | |
63 | foreach ($this->configs as $config) { |
64 | $arrayConfig->set($config->toArray()); |
65 | } |
66 | |
67 | return $arrayConfig; |
68 | } |
69 | } |