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 BuiltParameterResolver 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
|
|
|
public function __construct(array $parameters, ConfigRepository $config) |
39
|
|
|
{ |
40
|
|
|
$this->parameters = $parameters; |
41
|
|
|
$this->config = $config; |
42
|
|
|
$this->defaultValue = spl_object_hash(new \stdClass()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc}array |
47
|
|
|
*/ |
48
|
|
|
public function resolve($parameter) |
49
|
|
|
{ |
50
|
|
|
return $this->resolveValue($parameter, [$parameter => true]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param mixed $value |
55
|
|
|
* @param array $resolving |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
* |
59
|
|
|
* @throws ParameterCircularReferenceException |
60
|
|
|
* @throws ParameterNotFoundException |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
private function resolveValue($value, $resolving = []) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
if (is_bool($value) || is_numeric($value)) { |
65
|
|
|
return $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (is_array($value)) { |
69
|
|
|
return $this->resolveArray($value, $resolving); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (is_string($value)) { |
73
|
|
|
return $this->resolveString($value, $resolving); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
private function resolveArray(array $arrayValue, array $resolving) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$resolvedValue = []; |
82
|
|
|
foreach ($arrayValue as $key => $value) { |
83
|
|
|
$resolvedValue[$key] = $this->resolveValue($value, $resolving); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $resolvedValue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $value |
91
|
|
|
* @param $resolving |
92
|
|
|
* |
93
|
|
|
* @return array|mixed |
94
|
|
|
* @throws ParameterCircularReferenceException |
95
|
|
|
* @throws ParameterNotFoundException |
96
|
|
|
*/ |
97
|
|
|
private function resolveString($value, array $resolving) |
98
|
|
|
{ |
99
|
|
|
if (0 === preg_match('/^%([^%\s]+)%$/', $value, $match)) { |
100
|
|
|
if (false === array_key_exists($value, $resolving)) { |
101
|
|
|
return $value; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$key = $value; |
105
|
|
|
} else { |
106
|
|
|
$key = $match[1]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (array_key_exists($key, $this->parameters)) { |
110
|
|
|
$resolving[$key] = true; |
111
|
|
|
|
112
|
|
|
return $this->resolveValue($this->parameters[$key], $resolving); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->config->has($key)) { |
116
|
|
|
return $this->config->get($key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->resolveEnvironmentValue($key); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $key |
124
|
|
|
* |
125
|
|
|
* @return string|int|bool|null |
126
|
|
|
* @throws ParameterNotFoundException |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
private function resolveEnvironmentValue($key) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$environmentKey = strtoupper(str_replace('.', '_', $key)); |
131
|
|
|
$value = env($environmentKey, $this->defaultValue); |
132
|
|
|
if ($this->defaultValue !== $value) { |
133
|
|
|
return $value; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
throw new ParameterNotFoundException(sprintf('No parameter "%s" found', $key)); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.