-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLoadYamlConfiguration.php
93 lines (79 loc) · 2.89 KB
/
LoadYamlConfiguration.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace Hyde\Foundation\Internal;
use Hyde\Hyde;
use Hyde\Facades\Config;
use Illuminate\Support\Arr;
use Symfony\Component\Yaml\Yaml;
use function array_key_first;
use function file_get_contents;
use function array_merge;
use function file_exists;
/**
* @internal Bootstrap service that loads the YAML configuration file.
*
* @see docs/digging-deeper/customization.md#yaml-configuration
*
* It also supports loading multiple configuration namespaces, where a configuration namespace is defined
* as the first level in the service container configuration repository array, and usually corresponds
* one-to-one with a file in the config directory. This feature, by design, requires a top-level
* configuration entry to be present as 'hyde' in the YAML file. Existing config files
* will be parsed as normal, but can be migrated by indenting all entries by one
* level, and adding a top-level 'hyde' key. Then additional namespaces can
* be added underneath as needed.
*/
class LoadYamlConfiguration
{
/**
* Performs a core task that needs to be performed on
* early stages of the framework.
*/
public function bootstrap(): void
{
if ($this->hasYamlConfigFile()) {
$this->mergeParsedConfiguration();
}
}
protected function hasYamlConfigFile(): bool
{
return file_exists(Hyde::path('hyde.yml'))
|| file_exists(Hyde::path('hyde.yaml'));
}
/** @return array<string, scalar> */
protected function getYaml(): array
{
return Arr::undot((array) Yaml::parse(file_get_contents($this->getFile())));
}
protected function getFile(): string
{
return file_exists(Hyde::path('hyde.yml'))
? Hyde::path('hyde.yml')
: Hyde::path('hyde.yaml');
}
protected function mergeParsedConfiguration(): void
{
$yaml = $this->getYaml();
// If the Yaml file contains namespaces, we merge those using more granular logic
// that only applies the namespace data to each configuration namespace.
if ($this->configurationContainsNamespaces($yaml)) {
/** @var array<string, array<string, scalar>> $yaml */
foreach ($yaml as $namespace => $data) {
$this->mergeConfiguration($namespace, Arr::undot((array) $data));
}
return;
}
// Otherwise, we can merge using the default strategy, which is simply applying all the data to the hyde namespace.
$this->mergeConfiguration('hyde', $yaml);
}
protected function mergeConfiguration(string $namespace, array $yamlData): void
{
Config::set($namespace, array_merge(
Config::getArray($namespace, []),
$yamlData
));
}
protected function configurationContainsNamespaces(array $yaml): bool
{
return array_key_first($yaml) === 'hyde';
}
}