|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigFileInterface; |
|
8
|
|
|
use Gacela\Framework\Config\GacelaFileConfig\GacelaConfigItem; |
|
9
|
|
|
|
|
10
|
|
|
final class ConfigLoader |
|
11
|
|
|
{ |
|
12
|
111 |
|
/** @var array<string,array<string,mixed>> */ |
|
13
|
|
|
private array $cachedConfigs = []; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct( |
|
16
|
|
|
private readonly GacelaConfigFileInterface $gacelaConfigFile, |
|
17
|
111 |
|
private readonly PathFinderInterface $pathFinder, |
|
18
|
|
|
private readonly PathNormalizerInterface $pathNormalizer, |
|
19
|
|
|
) { |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
111 |
|
/** |
|
23
|
|
|
* @return array<string,mixed> |
|
24
|
111 |
|
*/ |
|
25
|
111 |
|
public function loadAll(): array |
|
26
|
|
|
{ |
|
27
|
|
|
$allConfigs = []; |
|
28
|
111 |
|
|
|
29
|
111 |
|
foreach ($this->gacelaConfigFile->getConfigItems() as $configItem) { |
|
30
|
32 |
|
$allConfigs[] = $this->loadConfigsFromPatterns($configItem); |
|
31
|
32 |
|
$allConfigs[] = $this->loadLocalConfig($configItem); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
111 |
|
return array_merge(...$allConfigs); |
|
35
|
32 |
|
} |
|
36
|
32 |
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array<string,mixed> |
|
39
|
|
|
*/ |
|
40
|
111 |
|
private function loadConfigsFromPatterns(GacelaConfigItem $configItem): array |
|
41
|
111 |
|
{ |
|
42
|
|
|
$patterns = [ |
|
43
|
111 |
|
$this->pathNormalizer->normalizePathPattern($configItem), |
|
44
|
|
|
$this->pathNormalizer->normalizePathPatternWithEnvironment($configItem), |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
$localPath = $this->pathNormalizer->normalizePathLocal($configItem); |
|
48
|
|
|
$mergedConfigs = []; |
|
49
|
111 |
|
|
|
50
|
|
|
foreach ($patterns as $pattern) { |
|
51
|
111 |
|
$configPaths = $this->getMatchingConfigPaths($pattern, $localPath); |
|
52
|
111 |
|
|
|
53
|
|
|
foreach ($configPaths as $absolutePath) { |
|
54
|
111 |
|
$mergedConfigs[] = $this->readConfigWithCache($absolutePath, $configItem); |
|
55
|
32 |
|
} |
|
56
|
32 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
return array_merge(...$mergedConfigs); |
|
59
|
111 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
32 |
|
* @return array<string,mixed> |
|
63
|
|
|
*/ |
|
64
|
32 |
|
private function loadLocalConfig(GacelaConfigItem $configItem): array |
|
65
|
|
|
{ |
|
66
|
|
|
$localPath = $this->pathNormalizer->normalizePathLocal($configItem); |
|
67
|
|
|
return $configItem->reader()->read($localPath); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return list<string> |
|
72
|
32 |
|
*/ |
|
73
|
|
|
private function getMatchingConfigPaths(string $pattern, string $excludePath): array |
|
74
|
|
|
{ |
|
75
|
|
|
$matchingPaths = $this->pathFinder->matchingPattern($pattern); |
|
76
|
|
|
return array_diff($matchingPaths, [$excludePath]); |
|
77
|
32 |
|
} |
|
78
|
32 |
|
|
|
79
|
32 |
|
/** |
|
80
|
|
|
* @return array<string,mixed> |
|
81
|
|
|
*/ |
|
82
|
32 |
|
private function readConfigWithCache(string $absolutePath, GacelaConfigItem $configItem): array |
|
83
|
32 |
|
{ |
|
84
|
16 |
|
if (!isset($this->cachedConfigs[$absolutePath])) { |
|
85
|
16 |
|
$this->cachedConfigs[$absolutePath] = $configItem->reader()->read($absolutePath); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
16 |
|
return $this->cachedConfigs[$absolutePath]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|