Passed
Push — master ( 034b15...36859c )
by Caen
04:33 queued 14s
created

LoadYamlConfiguration::hasYamlConfigFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
rs 10
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
    protected array $config;
30
31
    public function bootstrap(Application $app): void
32
    {
33
        $this->yaml = $app->make(YamlConfigurationRepository::class);
34
35
        if ($this->yaml->hasYamlConfigFile()) {
36
            tap($app->make('config'), function (Repository $config): void {
37
                $this->config = $config->all();
38
                $this->mergeParsedConfiguration();
39
            })->set($this->config);
40
        }
41
    }
42
43
    protected function mergeParsedConfiguration(): void
44
    {
45
        foreach ($this->yaml->getData() as $namespace => $data) {
46
            $this->mergeConfiguration($namespace, Arr::undot($data ?: []));
47
        }
48
    }
49
50
    protected function mergeConfiguration(string $namespace, array $yaml): void
51
    {
52
        $this->config[$namespace] = array_merge($this->config[$namespace] ?? [], $yaml);
53
    }
54
}
55