Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
10 / 10 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LoadYamlConfiguration | |
100.00% |
10 / 10 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
bootstrap | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
mergeParsedConfiguration | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
mergeConfiguration | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Hyde\Foundation\Internal; |
6 | |
7 | use Illuminate\Support\Arr; |
8 | use Hyde\Foundation\Application; |
9 | use Illuminate\Config\Repository; |
10 | |
11 | use function array_merge; |
12 | |
13 | /** |
14 | * @internal Bootstrap service that loads the YAML configuration file. |
15 | * |
16 | * @see docs/digging-deeper/customization.md#yaml-configuration |
17 | * |
18 | * It also supports loading multiple configuration namespaces, where a configuration namespace is defined |
19 | * as a firs level entry in the service container configuration repository array, and corresponds |
20 | * one-to-one with a file in the config directory, and a root-level key in the YAML file. |
21 | * |
22 | * The namespace feature by design, requires a top-level configuration entry to be present as 'hyde' in the YAML file. |
23 | * Existing config files will be parsed as normal, but can be migrated by indenting all entries by one level, |
24 | * and adding a top-level 'hyde' key. Then additional namespaces can be added underneath as needed. |
25 | */ |
26 | class LoadYamlConfiguration |
27 | { |
28 | protected YamlConfigurationRepository $yaml; |
29 | |
30 | /** @var array<string, array<string, null|scalar|array>> */ |
31 | protected array $config; |
32 | |
33 | public function bootstrap(Application $app): void |
34 | { |
35 | $this->yaml = $app->make(YamlConfigurationRepository::class); |
36 | |
37 | if ($this->yaml->hasYamlConfigFile()) { |
38 | /** @var Repository $config */ |
39 | $config = $app->make('config'); |
40 | |
41 | tap($config, function (Repository $config): void { |
42 | $this->config = $config->all(); |
43 | $this->mergeParsedConfiguration(); |
44 | })->set($this->config); |
45 | } |
46 | } |
47 | |
48 | protected function mergeParsedConfiguration(): void |
49 | { |
50 | foreach ($this->yaml->getData() as $namespace => $data) { |
51 | $this->mergeConfiguration($namespace, Arr::undot($data ?: [])); |
52 | } |
53 | } |
54 | |
55 | protected function mergeConfiguration(string $namespace, array $yaml): void |
56 | { |
57 | $this->config[$namespace] = array_merge($this->config[$namespace] ?? [], $yaml); |
58 | } |
59 | } |