|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFile; |
|
8
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigItem; |
|
9
|
|
|
|
|
10
|
|
|
final class ConfigLoader |
|
11
|
|
|
{ |
|
12
|
|
|
private GacelaConfigFileFactoryInterface $configFactory; |
|
13
|
|
|
|
|
14
|
|
|
private PathFinderInterface $pathFinder; |
|
15
|
|
|
|
|
16
|
|
|
private PathNormalizerInterface $pathNormalizer; |
|
17
|
|
|
|
|
18
|
29 |
|
public function __construct( |
|
19
|
|
|
GacelaConfigFileFactoryInterface $configFactory, |
|
20
|
|
|
PathFinderInterface $pathFinder, |
|
21
|
|
|
PathNormalizerInterface $configPathGenerator |
|
22
|
|
|
) { |
|
23
|
29 |
|
$this->configFactory = $configFactory; |
|
24
|
29 |
|
$this->pathFinder = $pathFinder; |
|
25
|
29 |
|
$this->pathNormalizer = $configPathGenerator; |
|
26
|
29 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return array<string,mixed> |
|
30
|
|
|
*/ |
|
31
|
29 |
|
public function loadAll(): array |
|
32
|
|
|
{ |
|
33
|
29 |
|
$gacelaFileConfig = $this->configFactory->createGacelaFileConfig(); |
|
34
|
29 |
|
$configs = []; |
|
35
|
29 |
|
$cacheConfigFileContent = []; |
|
36
|
|
|
|
|
37
|
|
|
/** @var list<array<string,mixed>> $result */ |
|
38
|
29 |
|
$result = []; |
|
39
|
29 |
|
foreach ($gacelaFileConfig->getConfigItems() as $configItem) { |
|
40
|
29 |
|
$absolutePatternPath = $this->pathNormalizer->normalizePathPattern($configItem); |
|
41
|
29 |
|
$result[] = $this->readAbsolutePatternPath($absolutePatternPath, $configItem, $cacheConfigFileContent); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
29 |
|
foreach ($gacelaFileConfig->getConfigItems() as $configItem) { |
|
45
|
29 |
|
$absolutePatternPath = $this->pathNormalizer->normalizePathPatternWithEnvironment($configItem); |
|
46
|
29 |
|
$result[] = $this->readAbsolutePatternPath($absolutePatternPath, $configItem, $cacheConfigFileContent); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** @psalm-suppress MixedArgument */ |
|
50
|
29 |
|
$configs[] = array_merge(...array_merge(...$result)); // @phpstan-ignore-line |
|
51
|
29 |
|
$configs[] = $this->readLocalConfigFile($gacelaFileConfig); |
|
52
|
|
|
|
|
53
|
|
|
/** @var array<string,mixed> $allConfigKeyValues */ |
|
54
|
29 |
|
$allConfigKeyValues = array_merge(...$configs); |
|
55
|
|
|
|
|
56
|
29 |
|
return $allConfigKeyValues; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return array<string,mixed> |
|
61
|
|
|
*/ |
|
62
|
29 |
|
private function readLocalConfigFile(GacelaConfigFile $gacelaConfigFile): array |
|
63
|
|
|
{ |
|
64
|
29 |
|
$result = []; |
|
65
|
29 |
|
$configItems = $gacelaConfigFile->getConfigItems(); |
|
66
|
|
|
|
|
67
|
29 |
|
foreach ($configItems as $configItem) { |
|
68
|
29 |
|
$absolutePath = $this->normalizePathLocal($configItem); |
|
69
|
29 |
|
$result[] = $configItem->reader()->read($absolutePath); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
29 |
|
return array_merge(...array_filter($result)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
29 |
|
private function normalizePathLocal(GacelaConfigItem $configItem): string |
|
76
|
|
|
{ |
|
77
|
29 |
|
return $this->pathNormalizer->normalizePathLocal($configItem); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array<string,array<string,mixed>> $cacheConfigFileContent |
|
82
|
|
|
* |
|
83
|
|
|
* @return list<array<string,mixed>> |
|
84
|
|
|
*/ |
|
85
|
29 |
|
private function readAbsolutePatternPath( |
|
86
|
|
|
string $pattern, |
|
87
|
|
|
GacelaConfigItem $configItem, |
|
88
|
|
|
array &$cacheConfigFileContent |
|
89
|
|
|
): array { |
|
90
|
29 |
|
$matchingPattern = $this->pathFinder->matchingPattern($pattern); |
|
91
|
29 |
|
$excludePattern = [$this->normalizePathLocal($configItem)]; |
|
92
|
29 |
|
$configPaths = array_diff($matchingPattern, $excludePattern); |
|
93
|
|
|
|
|
94
|
|
|
/** @var list<array<string,mixed>> $result */ |
|
95
|
29 |
|
$result = []; |
|
96
|
29 |
|
foreach ($configPaths as $absolutePath) { |
|
97
|
26 |
|
if (!isset($cacheConfigFileContent[$absolutePath])) { |
|
98
|
26 |
|
$cacheConfigFileContent[$absolutePath] = $configItem->reader()->read($absolutePath); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
26 |
|
$result[] = $cacheConfigFileContent[$absolutePath]; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
29 |
|
return $result; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|