Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
YamlConfigurationRepository | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
getData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasYamlConfigFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parseYamlFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFilePath | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
configurationContainsNamespaces | |
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 Hyde\Hyde; |
8 | use Illuminate\Support\Arr; |
9 | use Symfony\Component\Yaml\Yaml; |
10 | |
11 | use function file_exists; |
12 | use function file_get_contents; |
13 | use function array_key_first; |
14 | |
15 | /** |
16 | * @internal Contains shared logic for loading and parsing the YAML configuration file. |
17 | * |
18 | * @see LoadYamlEnvironmentVariables Which uses this repository to inject environment variables from the YAML configuration file. |
19 | * @see LoadYamlConfiguration Which uses this repository to merge the YAML configuration data with the existing configuration. |
20 | */ |
21 | class YamlConfigurationRepository |
22 | { |
23 | protected false|string $file; |
24 | protected array $data; |
25 | |
26 | public function __construct() |
27 | { |
28 | $this->file = $this->getFilePath(); |
29 | |
30 | if ($this->hasYamlConfigFile()) { |
31 | $data = $this->parseYamlFile(); |
32 | |
33 | if (! self::configurationContainsNamespaces($data)) { |
34 | $data = ['hyde' => $data]; |
35 | } |
36 | |
37 | $this->data = $data; |
38 | } |
39 | } |
40 | |
41 | /** @return array<string, array<string, null|scalar|array>> */ |
42 | public function getData(): array |
43 | { |
44 | return $this->data; |
45 | } |
46 | |
47 | public function hasYamlConfigFile(): bool |
48 | { |
49 | return $this->file !== false; |
50 | } |
51 | |
52 | protected function parseYamlFile(): array |
53 | { |
54 | return Arr::undot((array) Yaml::parse(file_get_contents($this->file))); |
55 | } |
56 | |
57 | protected function getFilePath(): string|false |
58 | { |
59 | return match (true) { |
60 | file_exists(Hyde::path('hyde.yml')) => Hyde::path('hyde.yml'), |
61 | file_exists(Hyde::path('hyde.yaml')) => Hyde::path('hyde.yaml'), |
62 | default => false, |
63 | }; |
64 | } |
65 | |
66 | protected static function configurationContainsNamespaces(array $config): bool |
67 | { |
68 | return array_key_first($config) === 'hyde'; |
69 | } |
70 | } |