|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon-cqrs/config project. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace Daikon\Config; |
|
12
|
|
|
|
|
13
|
|
|
use Assert\Assertion; |
|
14
|
|
|
|
|
15
|
|
|
final class ConfigProvider implements ConfigProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private const INTERPOLATION_PATTERN = '/(\$\{(.*?)\})/'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var mixed[] */ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
|
|
/** @var ConfigProviderParamsInterface */ |
|
23
|
|
|
private $params; |
|
24
|
|
|
|
|
25
|
|
|
/** @var mixed[] */ |
|
26
|
|
|
private $paramInterpolations; |
|
27
|
|
|
|
|
28
|
3 |
|
public function __construct(ConfigProviderParamsInterface $params) |
|
29
|
|
|
{ |
|
30
|
3 |
|
$this->params = $params; |
|
31
|
3 |
|
$this->config = []; |
|
32
|
3 |
|
$this->paramInterpolations = []; |
|
33
|
3 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $path |
|
37
|
|
|
* @param mixed|null $default |
|
38
|
|
|
* @return mixed|null |
|
39
|
|
|
*/ |
|
40
|
7 |
|
public function get(string $path, $default = null) |
|
41
|
|
|
{ |
|
42
|
7 |
|
$path = ConfigPath::fromString($path); |
|
43
|
7 |
|
$scope = $path->getScope(); |
|
44
|
7 |
|
Assertion::keyNotExists( |
|
45
|
7 |
|
$this->paramInterpolations, |
|
46
|
7 |
|
$scope, |
|
47
|
|
|
'Recursive interpolations are not allowed when interpolating "locations" or "sources". '. |
|
48
|
7 |
|
sprintf('Trying to recurse into scope: "%s"', $scope) |
|
49
|
|
|
); |
|
50
|
7 |
|
if (!isset($this->config[$scope]) && $this->params->hasScope($scope)) { |
|
51
|
7 |
|
$this->config[$scope] = $this->loadScope($scope); |
|
52
|
5 |
|
} elseif (!isset($this->config[$scope])) { |
|
53
|
2 |
|
return $default; |
|
54
|
|
|
} |
|
55
|
6 |
|
return $this->resolvePath( |
|
56
|
6 |
|
$path->getParts(), |
|
57
|
6 |
|
$this->config[$path->getScope()], |
|
58
|
6 |
|
$path->getSeparator() |
|
59
|
6 |
|
) ?? $default; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function has(string $path): bool |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->get($path) !== null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
7 |
|
private function loadScope(string $scope): array |
|
68
|
|
|
{ |
|
69
|
7 |
|
$this->paramInterpolations[$scope] = true; |
|
70
|
7 |
|
$locations = $this->params->getLocations($scope); |
|
71
|
7 |
|
$sources = $this->params->getSources($scope); |
|
72
|
7 |
|
$loader = $this->params->getLoader($scope); |
|
73
|
7 |
|
if (!$loader instanceof ArrayConfigLoader) { |
|
74
|
2 |
|
$sources = $this->interpolateConfigValues($sources); |
|
75
|
|
|
} |
|
76
|
6 |
|
$locations = $this->interpolateConfigValues($locations); |
|
77
|
6 |
|
unset($this->paramInterpolations[$scope]); |
|
78
|
|
|
|
|
79
|
6 |
|
$this->config[$scope] = $loader->load($locations, $sources); |
|
80
|
6 |
|
return $this->interpolateConfigValues($this->config[$scope]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $parts |
|
85
|
|
|
* @param mixed[] $values |
|
86
|
|
|
* @param string $separator |
|
87
|
|
|
* @return mixed|null |
|
88
|
|
|
*/ |
|
89
|
6 |
|
private function resolvePath(array $parts, array $values, string $separator) |
|
90
|
|
|
{ |
|
91
|
6 |
|
$pos = 0; |
|
92
|
6 |
|
$length = count($parts); |
|
93
|
6 |
|
$value = &$values; |
|
94
|
6 |
|
while (!empty($parts)) { |
|
95
|
6 |
|
$pos++; |
|
96
|
6 |
|
$part = array_shift($parts); |
|
97
|
6 |
|
Assertion::isArray( |
|
98
|
6 |
|
$value, |
|
99
|
6 |
|
sprintf('Trying to traverse non array-value with pathpart: "%s"', join($separator, $parts)) |
|
100
|
|
|
); |
|
101
|
6 |
|
if ($part === ConfigPathInterface::WILDCARD_TOKEN) { |
|
102
|
1 |
|
return $this->expandWildcard($parts, $value, $separator); |
|
103
|
|
|
} |
|
104
|
6 |
|
if (!isset($value[$part]) && $pos === $length) { |
|
105
|
1 |
|
return null; |
|
106
|
6 |
|
} elseif (!isset($value[$part])) { |
|
107
|
2 |
|
array_unshift($parts, $part.$separator.array_shift($parts)); |
|
108
|
2 |
|
continue; |
|
109
|
|
|
} |
|
110
|
6 |
|
$value = &$value[$part]; |
|
111
|
|
|
} |
|
112
|
6 |
|
return $value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
private function expandWildcard(array $parts, array $values, string $separator) |
|
116
|
|
|
{ |
|
117
|
|
|
return array_merge(...array_reduce($values, function ($expanded, $value) use ($parts, $separator): array { |
|
118
|
1 |
|
$expandedValue = $this->resolvePath($parts, $value, $separator); |
|
119
|
1 |
|
if (!is_null($expandedValue)) { |
|
120
|
1 |
|
$expanded[] = (array)$expandedValue; |
|
121
|
|
|
} |
|
122
|
1 |
|
return $expanded; |
|
123
|
1 |
|
}, [])); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
7 |
|
private function interpolateConfigValues(array $config): array |
|
127
|
|
|
{ |
|
128
|
7 |
|
return array_map( |
|
129
|
|
|
/** @return mixed */ |
|
130
|
7 |
|
function ($value) { |
|
131
|
7 |
|
if (is_array($value)) { |
|
132
|
6 |
|
return $this->interpolateConfigValues($value); |
|
133
|
|
|
} |
|
134
|
7 |
|
if (is_string($value) && preg_match_all(self::INTERPOLATION_PATTERN, $value, $matches)) { |
|
135
|
3 |
|
$interpolatedValues = array_map([$this, "get"], $matches[2]); |
|
136
|
2 |
|
return array_filter($interpolatedValues, 'is_string') === $interpolatedValues |
|
137
|
2 |
|
? str_replace($matches[0], array_map([$this, "get"], $matches[2]), $value) |
|
138
|
2 |
|
: $interpolatedValues[0]; |
|
139
|
|
|
} |
|
140
|
6 |
|
return $value; |
|
141
|
7 |
|
}, |
|
142
|
7 |
|
$config |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|