|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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 Symfony\Component\DependencyInjection\ParameterBag; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
|
|
15 |
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException; |
|
|
16 |
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
|
|
17 |
|
|
|
18 |
/** |
|
|
19 |
* |
|
|
20 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
21 |
* |
|
|
22 |
* @api |
|
|
23 |
*/ |
|
|
24 |
class ParameterBag implements ParameterBagInterface |
|
|
25 |
{ |
|
|
26 |
protected $parameters; |
|
|
27 |
protected $resolved; |
|
|
28 |
|
|
|
29 |
/** |
|
|
30 |
* Constructor. |
|
|
31 |
* |
|
|
32 |
* @param array $parameters An array of parameters |
|
|
33 |
* |
|
|
34 |
* @api |
|
|
35 |
*/ |
|
|
36 |
public function __construct(array $parameters = array()) |
|
|
37 |
{ |
|
|
38 |
$this->parameters = array(); |
|
|
39 |
$this->add($parameters); |
|
|
40 |
$this->resolved = false; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
/** |
|
|
44 |
* Clears all parameters. |
|
|
45 |
* |
|
|
46 |
* @api |
|
|
47 |
*/ |
|
|
48 |
public function clear() |
|
|
49 |
{ |
|
|
50 |
$this->parameters = array(); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
/** |
|
|
54 |
* Adds parameters to the service container parameters. |
|
|
55 |
* |
|
|
56 |
* @param array $parameters An array of parameters |
|
|
57 |
* |
|
|
58 |
* @api |
|
|
59 |
*/ |
|
|
60 |
public function add(array $parameters) |
|
|
61 |
{ |
|
|
62 |
foreach ($parameters as $key => $value) { |
|
|
63 |
$this->parameters[strtolower($key)] = $value; |
|
|
64 |
} |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* Gets the service container parameters. |
|
|
69 |
* |
|
|
70 |
* @return array An array of parameters |
|
|
71 |
* |
|
|
72 |
* @api |
|
|
73 |
*/ |
|
|
74 |
public function all() |
|
|
75 |
{ |
|
|
76 |
return $this->parameters; |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* Gets a service container parameter. |
|
|
81 |
* |
|
|
82 |
* @param string $name The parameter name |
|
|
83 |
* |
|
|
84 |
* @return mixed The parameter value |
|
|
85 |
* |
|
|
86 |
* @throws ParameterNotFoundException if the parameter is not defined |
|
|
87 |
* |
|
|
88 |
* @api |
|
|
89 |
*/ |
|
|
90 |
public function get($name) |
|
|
91 |
{ |
|
|
92 |
$name = strtolower($name); |
|
|
93 |
|
|
|
94 |
if (!array_key_exists($name, $this->parameters)) { |
|
|
95 |
throw new ParameterNotFoundException($name); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
return $this->parameters[$name]; |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
/** |
|
|
102 |
* Sets a service container parameter. |
|
|
103 |
* |
|
|
104 |
* @param string $name The parameter name |
|
|
105 |
* @param mixed $value The parameter value |
|
|
106 |
* |
|
|
107 |
* @api |
|
|
108 |
*/ |
|
|
109 |
public function set($name, $value) |
|
|
110 |
{ |
|
|
111 |
$this->parameters[strtolower($name)] = $value; |
|
|
112 |
} |
|
|
113 |
|
|
|
114 |
/** |
|
|
115 |
* Returns true if a parameter name is defined. |
|
|
116 |
* |
|
|
117 |
* @param string $name The parameter name |
|
|
118 |
* |
|
|
119 |
* @return Boolean true if the parameter name is defined, false otherwise |
|
|
120 |
* |
|
|
121 |
* @api |
|
|
122 |
*/ |
|
|
123 |
public function has($name) |
|
|
124 |
{ |
|
|
125 |
return array_key_exists(strtolower($name), $this->parameters); |
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
/** |
|
|
129 |
* Replaces parameter placeholders (%name%) by their values for all parameters. |
|
|
130 |
*/ |
|
|
131 |
public function resolve() |
|
|
132 |
{ |
|
|
133 |
if ($this->resolved) { |
|
|
134 |
return; |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
$parameters = array(); |
|
|
138 |
foreach ($this->parameters as $key => $value) { |
|
|
139 |
try { |
|
|
140 |
$value = $this->resolveValue($value); |
|
|
141 |
$parameters[$key] = is_string($value) ? str_replace('%%', '%', $value) : $value; |
|
|
142 |
} catch (ParameterNotFoundException $e) { |
|
|
143 |
$e->setSourceKey($key); |
|
|
144 |
|
|
|
145 |
throw $e; |
|
|
146 |
} |
|
|
147 |
} |
|
|
148 |
|
|
|
149 |
$this->parameters = $parameters; |
|
|
150 |
$this->resolved = true; |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
/** |
|
|
154 |
* Replaces parameter placeholders (%name%) by their values. |
|
|
155 |
* |
|
|
156 |
* @param mixed $value A value |
|
|
157 |
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references) |
|
|
158 |
* |
|
|
159 |
* @return mixed The resolved value |
|
|
160 |
* |
|
|
161 |
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist |
|
|
162 |
* @throws ParameterCircularReferenceException if a circular reference if detected |
|
|
163 |
* @throws RuntimeException when a given parameter has a type problem. |
|
|
164 |
*/ |
|
|
165 |
public function resolveValue($value, array $resolving = array()) |
|
|
166 |
{ |
|
|
167 |
if (is_array($value)) { |
|
|
168 |
$args = array(); |
|
|
169 |
foreach ($value as $k => $v) { |
|
|
170 |
$args[$this->resolveValue($k, $resolving)] = $this->resolveValue($v, $resolving); |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
return $args; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
if (!is_string($value)) { |
|
|
177 |
return $value; |
|
|
178 |
} |
|
|
179 |
|
|
|
180 |
return $this->resolveString($value, $resolving); |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
/** |
|
|
184 |
* Resolves parameters inside a string |
|
|
185 |
* |
|
|
186 |
* @param string $value The string to resolve |
|
|
187 |
* @param array $resolving An array of keys that are being resolved (used internally to detect circular references) |
|
|
188 |
* |
|
|
189 |
* @return string The resolved string |
|
|
190 |
* |
|
|
191 |
* @throws ParameterNotFoundException if a placeholder references a parameter that does not exist |
|
|
192 |
* @throws ParameterCircularReferenceException if a circular reference if detected |
|
|
193 |
* @throws RuntimeException when a given parameter has a type problem. |
|
|
194 |
*/ |
|
|
195 |
public function resolveString($value, array $resolving = array()) |
|
|
196 |
{ |
|
|
197 |
// we do this to deal with non string values (Boolean, integer, ...) |
|
|
198 |
// as the preg_replace_callback throw an exception when trying |
|
|
199 |
// a non-string in a parameter value |
|
|
200 |
if (preg_match('/^%([^%]+)%$/', $value, $match)) { |
|
|
201 |
$key = strtolower($match[1]); |
|
|
202 |
|
|
|
203 |
if (isset($resolving[$key])) { |
|
|
204 |
throw new ParameterCircularReferenceException(array_keys($resolving)); |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
$resolving[$key] = true; |
|
|
208 |
|
|
|
209 |
return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving); |
|
|
210 |
} |
|
|
211 |
|
|
|
212 |
$self = $this; |
|
|
213 |
|
|
|
214 |
return preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving, $value) { |
|
|
215 |
$key = strtolower($match[1]); |
|
|
216 |
if (isset($resolving[$key])) { |
|
|
217 |
throw new ParameterCircularReferenceException(array_keys($resolving)); |
|
|
218 |
} |
|
|
219 |
|
|
|
220 |
$resolved = $self->get($key); |
|
|
221 |
|
|
|
222 |
if (!is_string($resolved) && !is_numeric($resolved)) { |
|
|
223 |
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type %s inside string value "%s".', $key, gettype($resolved), $value)); |
|
|
224 |
} |
|
|
225 |
|
|
|
226 |
$resolved = (string) $resolved; |
|
|
227 |
$resolving[$key] = true; |
|
|
228 |
|
|
|
229 |
return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving); |
|
|
230 |
}, $value); |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
public function isResolved() |
|
|
234 |
{ |
|
|
235 |
return $this->resolved; |
|
|
236 |
} |
|
|
237 |
} |