|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the LaravelYaml package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Fidry\LaravelYaml\DependencyInjection\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\Exception\DependencyInjection\Resolver\ParameterCircularReferenceException; |
|
15
|
|
|
use Fidry\LaravelYaml\Exception\ParameterNotFoundException; |
|
16
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigRepository; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Théo FIDRY <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class BuildedParameterResolver implements ParameterResolverInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $defaultValue; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ConfigRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $config; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $parameters; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $resolved = []; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function __construct(array $parameters, ConfigRepository $config) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->parameters = $parameters; |
|
46
|
|
|
$this->config = $config; |
|
47
|
|
|
$this->defaultValue = spl_object_hash(new \stdClass()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc}array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function resolve($parameter) |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->resolveValue($parameter, [$parameter => true]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* @param array $resolving |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
* |
|
64
|
|
|
* @throws ParameterCircularReferenceException |
|
65
|
|
|
* @throws ParameterNotFoundException |
|
66
|
|
|
*/ |
|
67
|
|
View Code Duplication |
private function resolveValue($value, $resolving = []) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
if (is_bool($value) || is_numeric($value)) { |
|
70
|
|
|
return $value; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (is_array($value)) { |
|
74
|
|
|
return $this->resolveArray($value, $resolving); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (is_string($value)) { |
|
78
|
|
|
return $this->resolveString($value, $resolving); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
View Code Duplication |
private function resolveArray(array $arrayValue, array $resolving) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
$resolvedValue = []; |
|
87
|
|
|
foreach ($arrayValue as $key => $value) { |
|
88
|
|
|
$resolvedValue[$key] = $this->resolveValue($value, $resolving); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $resolvedValue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param $value |
|
96
|
|
|
* @param $resolving |
|
97
|
|
|
* |
|
98
|
|
|
* @return array|mixed |
|
99
|
|
|
* @throws ParameterCircularReferenceException |
|
100
|
|
|
* @throws ParameterNotFoundException |
|
101
|
|
|
*/ |
|
102
|
|
|
private function resolveString($value, array $resolving) |
|
103
|
|
|
{ |
|
104
|
|
|
if (0 === preg_match('/^%([^%\s]+)%$/', $value, $match)) { |
|
105
|
|
|
if (false === array_key_exists($value, $resolving)) { |
|
106
|
|
|
return $value; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$key = $value; |
|
110
|
|
|
} else { |
|
111
|
|
|
$key = $match[1]; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if (array_key_exists($key, $this->parameters)) { |
|
115
|
|
|
$resolving[$key] = true; |
|
116
|
|
|
|
|
117
|
|
|
return $this->resolveValue($this->parameters[$key], $resolving); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if ($this->config->has($key)) { |
|
121
|
|
|
return $this->config->get($key); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->resolveEnvironmentValue($key); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $key |
|
129
|
|
|
* |
|
130
|
|
|
* @return string|int|bool|null |
|
131
|
|
|
* @throws ParameterNotFoundException |
|
132
|
|
|
*/ |
|
133
|
|
View Code Duplication |
private function resolveEnvironmentValue($key) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$environmentKey = strtoupper(str_replace('.', '_', $key)); |
|
136
|
|
|
$value = env($environmentKey, $this->defaultValue); |
|
137
|
|
|
if ($this->defaultValue !== $value) { |
|
138
|
|
|
return $value; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
throw new ParameterNotFoundException(sprintf('No parameter "%s" found', $key)); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.