comparison vendor/symfony/dependency-injection/ParameterBag/ParameterBag.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\DependencyInjection\ParameterBag; 12 namespace Symfony\Component\DependencyInjection\ParameterBag;
13 13
14 use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
14 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; 15 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
15 use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException; 16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17 17
18 /** 18 /**
19 * Holds parameters. 19 * Holds parameters.
20 * 20 *
21 * @author Fabien Potencier <fabien@symfony.com> 21 * @author Fabien Potencier <fabien@symfony.com>
22 */ 22 */
23 class ParameterBag implements ParameterBagInterface 23 class ParameterBag implements ParameterBagInterface
24 { 24 {
25 protected $parameters = array(); 25 protected $parameters = [];
26 protected $resolved = false; 26 protected $resolved = false;
27 27
28 private $normalizedNames = array(); 28 private $normalizedNames = [];
29 29
30 /** 30 /**
31 * @param array $parameters An array of parameters 31 * @param array $parameters An array of parameters
32 */ 32 */
33 public function __construct(array $parameters = array()) 33 public function __construct(array $parameters = [])
34 { 34 {
35 $this->add($parameters); 35 $this->add($parameters);
36 } 36 }
37 37
38 /** 38 /**
39 * Clears all parameters. 39 * Clears all parameters.
40 */ 40 */
41 public function clear() 41 public function clear()
42 { 42 {
43 $this->parameters = array(); 43 $this->parameters = [];
44 } 44 }
45 45
46 /** 46 /**
47 * Adds parameters to the service container parameters. 47 * Adds parameters to the service container parameters.
48 * 48 *
73 if (!array_key_exists($name, $this->parameters)) { 73 if (!array_key_exists($name, $this->parameters)) {
74 if (!$name) { 74 if (!$name) {
75 throw new ParameterNotFoundException($name); 75 throw new ParameterNotFoundException($name);
76 } 76 }
77 77
78 $alternatives = array(); 78 $alternatives = [];
79 foreach ($this->parameters as $key => $parameterValue) { 79 foreach ($this->parameters as $key => $parameterValue) {
80 $lev = levenshtein($name, $key); 80 $lev = levenshtein($name, $key);
81 if ($lev <= strlen($name) / 3 || false !== strpos($key, $name)) { 81 if ($lev <= \strlen($name) / 3 || false !== strpos($key, $name)) {
82 $alternatives[] = $key; 82 $alternatives[] = $key;
83 } 83 }
84 } 84 }
85 85
86 $nonNestedAlternative = null; 86 $nonNestedAlternative = null;
87 if (!count($alternatives) && false !== strpos($name, '.')) { 87 if (!\count($alternatives) && false !== strpos($name, '.')) {
88 $namePartsLength = array_map('strlen', explode('.', $name)); 88 $namePartsLength = array_map('strlen', explode('.', $name));
89 $key = substr($name, 0, -1 * (1 + array_pop($namePartsLength))); 89 $key = substr($name, 0, -1 * (1 + array_pop($namePartsLength)));
90 while (count($namePartsLength)) { 90 while (\count($namePartsLength)) {
91 if ($this->has($key)) { 91 if ($this->has($key)) {
92 if (is_array($this->get($key))) { 92 if (\is_array($this->get($key))) {
93 $nonNestedAlternative = $key; 93 $nonNestedAlternative = $key;
94 } 94 }
95 break; 95 break;
96 } 96 }
97 97
141 { 141 {
142 if ($this->resolved) { 142 if ($this->resolved) {
143 return; 143 return;
144 } 144 }
145 145
146 $parameters = array(); 146 $parameters = [];
147 foreach ($this->parameters as $key => $value) { 147 foreach ($this->parameters as $key => $value) {
148 try { 148 try {
149 $value = $this->resolveValue($value); 149 $value = $this->resolveValue($value);
150 $parameters[$key] = $this->unescapeValue($value); 150 $parameters[$key] = $this->unescapeValue($value);
151 } catch (ParameterNotFoundException $e) { 151 } catch (ParameterNotFoundException $e) {
169 * 169 *
170 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist 170 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
171 * @throws ParameterCircularReferenceException if a circular reference if detected 171 * @throws ParameterCircularReferenceException if a circular reference if detected
172 * @throws RuntimeException when a given parameter has a type problem 172 * @throws RuntimeException when a given parameter has a type problem
173 */ 173 */
174 public function resolveValue($value, array $resolving = array()) 174 public function resolveValue($value, array $resolving = [])
175 { 175 {
176 if (\is_array($value)) { 176 if (\is_array($value)) {
177 $args = array(); 177 $args = [];
178 foreach ($value as $k => $v) { 178 foreach ($value as $k => $v) {
179 $args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving); 179 $args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving);
180 } 180 }
181 181
182 return $args; 182 return $args;
199 * 199 *
200 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist 200 * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
201 * @throws ParameterCircularReferenceException if a circular reference if detected 201 * @throws ParameterCircularReferenceException if a circular reference if detected
202 * @throws RuntimeException when a given parameter has a type problem 202 * @throws RuntimeException when a given parameter has a type problem
203 */ 203 */
204 public function resolveString($value, array $resolving = array()) 204 public function resolveString($value, array $resolving = [])
205 { 205 {
206 // we do this to deal with non string values (Boolean, integer, ...) 206 // we do this to deal with non string values (Boolean, integer, ...)
207 // as the preg_replace_callback throw an exception when trying 207 // as the preg_replace_callback throw an exception when trying
208 // a non-string in a parameter value 208 // a non-string in a parameter value
209 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) { 209 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
231 throw new ParameterCircularReferenceException(array_keys($resolving)); 231 throw new ParameterCircularReferenceException(array_keys($resolving));
232 } 232 }
233 233
234 $resolved = $this->get($key); 234 $resolved = $this->get($key);
235 235
236 if (!is_string($resolved) && !is_numeric($resolved)) { 236 if (!\is_string($resolved) && !is_numeric($resolved)) {
237 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)); 237 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));
238 } 238 }
239 239
240 $resolved = (string) $resolved; 240 $resolved = (string) $resolved;
241 $resolving[$lcKey] = true; 241 $resolving[$lcKey] = true;
242 242
252 /** 252 /**
253 * {@inheritdoc} 253 * {@inheritdoc}
254 */ 254 */
255 public function escapeValue($value) 255 public function escapeValue($value)
256 { 256 {
257 if (is_string($value)) { 257 if (\is_string($value)) {
258 return str_replace('%', '%%', $value); 258 return str_replace('%', '%%', $value);
259 } 259 }
260 260
261 if (is_array($value)) { 261 if (\is_array($value)) {
262 $result = array(); 262 $result = [];
263 foreach ($value as $k => $v) { 263 foreach ($value as $k => $v) {
264 $result[$k] = $this->escapeValue($v); 264 $result[$k] = $this->escapeValue($v);
265 } 265 }
266 266
267 return $result; 267 return $result;
273 /** 273 /**
274 * {@inheritdoc} 274 * {@inheritdoc}
275 */ 275 */
276 public function unescapeValue($value) 276 public function unescapeValue($value)
277 { 277 {
278 if (is_string($value)) { 278 if (\is_string($value)) {
279 return str_replace('%%', '%', $value); 279 return str_replace('%%', '%', $value);
280 } 280 }
281 281
282 if (is_array($value)) { 282 if (\is_array($value)) {
283 $result = array(); 283 $result = [];
284 foreach ($value as $k => $v) { 284 foreach ($value as $k => $v) {
285 $result[$k] = $this->unescapeValue($v); 285 $result[$k] = $this->unescapeValue($v);
286 } 286 }
287 287
288 return $result; 288 return $result;