comparison vendor/symfony/dependency-injection/ParameterBag/ParameterBag.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
23 class ParameterBag implements ParameterBagInterface 23 class ParameterBag implements ParameterBagInterface
24 { 24 {
25 protected $parameters = array(); 25 protected $parameters = array();
26 protected $resolved = false; 26 protected $resolved = false;
27 27
28 private $normalizedNames = array();
29
28 /** 30 /**
29 * @param array $parameters An array of parameters 31 * @param array $parameters An array of parameters
30 */ 32 */
31 public function __construct(array $parameters = array()) 33 public function __construct(array $parameters = array())
32 { 34 {
47 * @param array $parameters An array of parameters 49 * @param array $parameters An array of parameters
48 */ 50 */
49 public function add(array $parameters) 51 public function add(array $parameters)
50 { 52 {
51 foreach ($parameters as $key => $value) { 53 foreach ($parameters as $key => $value) {
52 $this->parameters[strtolower($key)] = $value; 54 $this->set($key, $value);
53 } 55 }
54 } 56 }
55 57
56 /** 58 /**
57 * {@inheritdoc} 59 * {@inheritdoc}
64 /** 66 /**
65 * {@inheritdoc} 67 * {@inheritdoc}
66 */ 68 */
67 public function get($name) 69 public function get($name)
68 { 70 {
69 $name = strtolower($name); 71 $name = $this->normalizeName($name);
70 72
71 if (!array_key_exists($name, $this->parameters)) { 73 if (!array_key_exists($name, $this->parameters)) {
72 if (!$name) { 74 if (!$name) {
73 throw new ParameterNotFoundException($name); 75 throw new ParameterNotFoundException($name);
74 } 76 }
109 * @param string $name The parameter name 111 * @param string $name The parameter name
110 * @param mixed $value The parameter value 112 * @param mixed $value The parameter value
111 */ 113 */
112 public function set($name, $value) 114 public function set($name, $value)
113 { 115 {
114 $this->parameters[strtolower($name)] = $value; 116 $this->parameters[$this->normalizeName($name)] = $value;
115 } 117 }
116 118
117 /** 119 /**
118 * {@inheritdoc} 120 * {@inheritdoc}
119 */ 121 */
120 public function has($name) 122 public function has($name)
121 { 123 {
122 return array_key_exists(strtolower($name), $this->parameters); 124 return array_key_exists($this->normalizeName($name), $this->parameters);
123 } 125 }
124 126
125 /** 127 /**
126 * Removes a parameter. 128 * Removes a parameter.
127 * 129 *
128 * @param string $name The parameter name 130 * @param string $name The parameter name
129 */ 131 */
130 public function remove($name) 132 public function remove($name)
131 { 133 {
132 unset($this->parameters[strtolower($name)]); 134 unset($this->parameters[$this->normalizeName($name)]);
133 } 135 }
134 136
135 /** 137 /**
136 * {@inheritdoc} 138 * {@inheritdoc}
137 */ 139 */
165 * 167 *
166 * @return mixed The resolved value 168 * @return mixed The resolved value
167 * 169 *
168 * @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
169 * @throws ParameterCircularReferenceException if a circular reference if detected 171 * @throws ParameterCircularReferenceException if a circular reference if detected
170 * @throws RuntimeException when a given parameter has a type problem. 172 * @throws RuntimeException when a given parameter has a type problem
171 */ 173 */
172 public function resolveValue($value, array $resolving = array()) 174 public function resolveValue($value, array $resolving = array())
173 { 175 {
174 if (is_array($value)) { 176 if (\is_array($value)) {
175 $args = array(); 177 $args = array();
176 foreach ($value as $k => $v) { 178 foreach ($value as $k => $v) {
177 $args[$this->resolveValue($k, $resolving)] = $this->resolveValue($v, $resolving); 179 $args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving);
178 } 180 }
179 181
180 return $args; 182 return $args;
181 } 183 }
182 184
183 if (!is_string($value)) { 185 if (!\is_string($value) || 2 > \strlen($value)) {
184 return $value; 186 return $value;
185 } 187 }
186 188
187 return $this->resolveString($value, $resolving); 189 return $this->resolveString($value, $resolving);
188 } 190 }
195 * 197 *
196 * @return string The resolved string 198 * @return string The resolved string
197 * 199 *
198 * @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
199 * @throws ParameterCircularReferenceException if a circular reference if detected 201 * @throws ParameterCircularReferenceException if a circular reference if detected
200 * @throws RuntimeException when a given parameter has a type problem. 202 * @throws RuntimeException when a given parameter has a type problem
201 */ 203 */
202 public function resolveString($value, array $resolving = array()) 204 public function resolveString($value, array $resolving = array())
203 { 205 {
204 // we do this to deal with non string values (Boolean, integer, ...) 206 // we do this to deal with non string values (Boolean, integer, ...)
205 // as the preg_replace_callback throw an exception when trying 207 // as the preg_replace_callback throw an exception when trying
206 // a non-string in a parameter value 208 // a non-string in a parameter value
207 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) { 209 if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
208 $key = $match[1]; 210 $key = $match[1];
209 $lcKey = strtolower($key); 211 $lcKey = strtolower($key); // strtolower() to be removed in 4.0
210 212
211 if (isset($resolving[$lcKey])) { 213 if (isset($resolving[$lcKey])) {
212 throw new ParameterCircularReferenceException(array_keys($resolving)); 214 throw new ParameterCircularReferenceException(array_keys($resolving));
213 } 215 }
214 216
222 if (!isset($match[1])) { 224 if (!isset($match[1])) {
223 return '%%'; 225 return '%%';
224 } 226 }
225 227
226 $key = $match[1]; 228 $key = $match[1];
227 $lcKey = strtolower($key); 229 $lcKey = strtolower($key); // strtolower() to be removed in 4.0
228 if (isset($resolving[$lcKey])) { 230 if (isset($resolving[$lcKey])) {
229 throw new ParameterCircularReferenceException(array_keys($resolving)); 231 throw new ParameterCircularReferenceException(array_keys($resolving));
230 } 232 }
231 233
232 $resolved = $this->get($key); 234 $resolved = $this->get($key);
286 return $result; 288 return $result;
287 } 289 }
288 290
289 return $value; 291 return $value;
290 } 292 }
293
294 private function normalizeName($name)
295 {
296 if (isset($this->normalizedNames[$normalizedName = strtolower($name)])) {
297 $normalizedName = $this->normalizedNames[$normalizedName];
298 if ((string) $name !== $normalizedName) {
299 @trigger_error(sprintf('Parameter names will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since Symfony 3.4.', $name, $normalizedName), E_USER_DEPRECATED);
300 }
301 } else {
302 $normalizedName = $this->normalizedNames[$normalizedName] = (string) $name;
303 }
304
305 return $normalizedName;
306 }
291 } 307 }