|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the daikon/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
|
|
|
final class ConfigProvider implements ConfigProviderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private const INTERPOLATION_PATTERN = '/(\$\{(.*?)\})/'; |
|
16
|
|
|
|
|
17
|
|
|
private $config; |
|
18
|
|
|
|
|
19
|
|
|
private $params; |
|
20
|
|
|
|
|
21
|
3 |
|
public function __construct(ConfigProviderParamsInterface $params) |
|
22
|
|
|
{ |
|
23
|
3 |
|
$this->params = $params; |
|
24
|
3 |
|
$this->config = []; |
|
25
|
3 |
|
} |
|
26
|
|
|
|
|
27
|
3 |
|
public function get(string $path, $default = null) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$configPath = ConfigPath::fromString($path); |
|
30
|
3 |
|
$scope = $configPath->getScope(); |
|
31
|
3 |
|
if (!isset($this->config[$scope])) { |
|
32
|
3 |
|
if ($this->params->hasScope($scope)) { |
|
33
|
3 |
|
$this->config[$scope] = $this->loadScope($scope); |
|
34
|
|
|
} else { |
|
35
|
|
|
return $default; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
3 |
|
if ($configPath->hasParts()) { |
|
39
|
3 |
|
return $this->resolvePath($configPath) ?? $default; |
|
40
|
|
|
} |
|
41
|
2 |
|
return $this->config[$scope]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function has(string $path): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->get($path) !== null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
private function loadScope(string $scope): array |
|
50
|
|
|
{ |
|
51
|
3 |
|
return $this->interpolateConfigValues( |
|
52
|
3 |
|
$this->params->getLoader($scope)->load( |
|
53
|
3 |
|
$this->params->getLocations($scope), |
|
54
|
3 |
|
$this->params->getSources($scope) |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
private function resolvePath(ConfigPathInterface $path) |
|
60
|
|
|
{ |
|
61
|
3 |
|
$value = &$this->config[$path->getScope()]; |
|
62
|
3 |
|
$pathParts = $path->getParts(); |
|
63
|
|
|
do { |
|
64
|
3 |
|
$pathPart = array_shift($pathParts); |
|
65
|
3 |
|
if (!is_array($value)) { |
|
66
|
|
|
throw new \Exception("Trying to traverse non array-value with key: '".$path->getKey()."'"); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
3 |
|
if (!isset($value[$pathPart])) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
3 |
|
$value = &$value[$pathPart]; |
|
72
|
|
|
} while (!empty($pathParts)); |
|
73
|
3 |
|
return $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
private function interpolateConfigValues(array $config): array |
|
77
|
|
|
{ |
|
78
|
3 |
|
foreach ($config as $key => $value) { |
|
79
|
3 |
|
if (is_array($value)) { |
|
80
|
3 |
|
$config[$key] = $this->interpolateConfigValues($value); |
|
81
|
3 |
|
} elseif (is_string($value) && preg_match_all(self::INTERPOLATION_PATTERN, $value, $matches)) { |
|
82
|
2 |
|
$replacements = []; |
|
83
|
2 |
|
foreach ($matches[2] as $configKey) { |
|
84
|
2 |
|
$replacements[] = $this->get($configKey); |
|
85
|
|
|
} |
|
86
|
2 |
|
$config[$key] = str_replace($matches[0], $replacements, $value); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
3 |
|
return $config; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.