Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/dependency-injection/Dumper/YamlDumper.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 |
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\Dumper; | 12 namespace Symfony\Component\DependencyInjection\Dumper; |
13 | 13 |
14 use Symfony\Component\Yaml\Dumper as YmlDumper; | |
15 use Symfony\Component\Yaml\Parser; | |
16 use Symfony\Component\Yaml\Tag\TaggedValue; | |
17 use Symfony\Component\Yaml\Yaml; | |
18 use Symfony\Component\DependencyInjection\Alias; | 14 use Symfony\Component\DependencyInjection\Alias; |
19 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; | 15 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; |
20 use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | 16 use Symfony\Component\DependencyInjection\Argument\IteratorArgument; |
21 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; | 17 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
22 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; | 18 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; |
23 use Symfony\Component\DependencyInjection\ContainerInterface; | 19 use Symfony\Component\DependencyInjection\ContainerInterface; |
24 use Symfony\Component\DependencyInjection\Definition; | 20 use Symfony\Component\DependencyInjection\Definition; |
21 use Symfony\Component\DependencyInjection\Exception\RuntimeException; | |
25 use Symfony\Component\DependencyInjection\Parameter; | 22 use Symfony\Component\DependencyInjection\Parameter; |
26 use Symfony\Component\DependencyInjection\Reference; | 23 use Symfony\Component\DependencyInjection\Reference; |
27 use Symfony\Component\DependencyInjection\Exception\RuntimeException; | |
28 use Symfony\Component\ExpressionLanguage\Expression; | 24 use Symfony\Component\ExpressionLanguage\Expression; |
25 use Symfony\Component\Yaml\Dumper as YmlDumper; | |
26 use Symfony\Component\Yaml\Parser; | |
27 use Symfony\Component\Yaml\Tag\TaggedValue; | |
28 use Symfony\Component\Yaml\Yaml; | |
29 | 29 |
30 /** | 30 /** |
31 * YamlDumper dumps a service container as a YAML string. | 31 * YamlDumper dumps a service container as a YAML string. |
32 * | 32 * |
33 * @author Fabien Potencier <fabien@symfony.com> | 33 * @author Fabien Potencier <fabien@symfony.com> |
39 /** | 39 /** |
40 * Dumps the service container as an YAML string. | 40 * Dumps the service container as an YAML string. |
41 * | 41 * |
42 * @return string A YAML string representing of the service container | 42 * @return string A YAML string representing of the service container |
43 */ | 43 */ |
44 public function dump(array $options = array()) | 44 public function dump(array $options = []) |
45 { | 45 { |
46 if (!class_exists('Symfony\Component\Yaml\Dumper')) { | 46 if (!class_exists('Symfony\Component\Yaml\Dumper')) { |
47 throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.'); | 47 throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.'); |
48 } | 48 } |
49 | 49 |
78 } | 78 } |
79 | 79 |
80 $tagsCode = ''; | 80 $tagsCode = ''; |
81 foreach ($definition->getTags() as $name => $tags) { | 81 foreach ($definition->getTags() as $name => $tags) { |
82 foreach ($tags as $attributes) { | 82 foreach ($tags as $attributes) { |
83 $att = array(); | 83 $att = []; |
84 foreach ($attributes as $key => $value) { | 84 foreach ($attributes as $key => $value) { |
85 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value)); | 85 $att[] = sprintf('%s: %s', $this->dumper->dump($key), $this->dumper->dump($value)); |
86 } | 86 } |
87 $att = $att ? ', '.implode(', ', $att) : ''; | 87 $att = $att ? ', '.implode(', ', $att) : ''; |
88 | 88 |
100 if ($definition->isSynthetic()) { | 100 if ($definition->isSynthetic()) { |
101 $code .= " synthetic: true\n"; | 101 $code .= " synthetic: true\n"; |
102 } | 102 } |
103 | 103 |
104 if ($definition->isDeprecated()) { | 104 if ($definition->isDeprecated()) { |
105 $code .= sprintf(" deprecated: %s\n", $definition->getDeprecationMessage('%service_id%')); | 105 $code .= sprintf(" deprecated: %s\n", $this->dumper->dump($definition->getDeprecationMessage('%service_id%'))); |
106 } | 106 } |
107 | 107 |
108 if ($definition->isAutowired()) { | 108 if ($definition->isAutowired()) { |
109 $code .= " autowire: true\n"; | 109 $code .= " autowire: true\n"; |
110 } | 110 } |
222 return ''; | 222 return ''; |
223 } | 223 } |
224 | 224 |
225 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isCompiled()); | 225 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isCompiled()); |
226 | 226 |
227 return $this->dumper->dump(array('parameters' => $parameters), 2); | 227 return $this->dumper->dump(['parameters' => $parameters], 2); |
228 } | 228 } |
229 | 229 |
230 /** | 230 /** |
231 * Dumps callable to YAML format. | 231 * Dumps callable to YAML format. |
232 * | 232 * |
234 * | 234 * |
235 * @return callable | 235 * @return callable |
236 */ | 236 */ |
237 private function dumpCallable($callable) | 237 private function dumpCallable($callable) |
238 { | 238 { |
239 if (is_array($callable)) { | 239 if (\is_array($callable)) { |
240 if ($callable[0] instanceof Reference) { | 240 if ($callable[0] instanceof Reference) { |
241 $callable = array($this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]); | 241 $callable = [$this->getServiceCall((string) $callable[0], $callable[0]), $callable[1]]; |
242 } else { | 242 } else { |
243 $callable = array($callable[0], $callable[1]); | 243 $callable = [$callable[0], $callable[1]]; |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 return $callable; | 247 return $callable; |
248 } | 248 } |
266 return new TaggedValue('tagged', $value->getTag()); | 266 return new TaggedValue('tagged', $value->getTag()); |
267 } | 267 } |
268 if ($value instanceof IteratorArgument) { | 268 if ($value instanceof IteratorArgument) { |
269 $tag = 'iterator'; | 269 $tag = 'iterator'; |
270 } else { | 270 } else { |
271 throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', get_class($value))); | 271 throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', \get_class($value))); |
272 } | 272 } |
273 | 273 |
274 return new TaggedValue($tag, $this->dumpValue($value->getValues())); | 274 return new TaggedValue($tag, $this->dumpValue($value->getValues())); |
275 } | 275 } |
276 | 276 |
277 if (is_array($value)) { | 277 if (\is_array($value)) { |
278 $code = array(); | 278 $code = []; |
279 foreach ($value as $k => $v) { | 279 foreach ($value as $k => $v) { |
280 $code[$k] = $this->dumpValue($v); | 280 $code[$k] = $this->dumpValue($v); |
281 } | 281 } |
282 | 282 |
283 return $code; | 283 return $code; |
287 return $this->getParameterCall((string) $value); | 287 return $this->getParameterCall((string) $value); |
288 } elseif ($value instanceof Expression) { | 288 } elseif ($value instanceof Expression) { |
289 return $this->getExpressionCall((string) $value); | 289 return $this->getExpressionCall((string) $value); |
290 } elseif ($value instanceof Definition) { | 290 } elseif ($value instanceof Definition) { |
291 return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']); | 291 return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']); |
292 } elseif (is_object($value) || is_resource($value)) { | 292 } elseif (\is_object($value) || \is_resource($value)) { |
293 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.'); | 293 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.'); |
294 } | 294 } |
295 | 295 |
296 return $value; | 296 return $value; |
297 } | 297 } |
342 * | 342 * |
343 * @return array | 343 * @return array |
344 */ | 344 */ |
345 private function prepareParameters(array $parameters, $escape = true) | 345 private function prepareParameters(array $parameters, $escape = true) |
346 { | 346 { |
347 $filtered = array(); | 347 $filtered = []; |
348 foreach ($parameters as $key => $value) { | 348 foreach ($parameters as $key => $value) { |
349 if (is_array($value)) { | 349 if (\is_array($value)) { |
350 $value = $this->prepareParameters($value, $escape); | 350 $value = $this->prepareParameters($value, $escape); |
351 } elseif ($value instanceof Reference || is_string($value) && 0 === strpos($value, '@')) { | 351 } elseif ($value instanceof Reference || \is_string($value) && 0 === strpos($value, '@')) { |
352 $value = '@'.$value; | 352 $value = '@'.$value; |
353 } | 353 } |
354 | 354 |
355 $filtered[$key] = $value; | 355 $filtered[$key] = $value; |
356 } | 356 } |
363 * | 363 * |
364 * @return array | 364 * @return array |
365 */ | 365 */ |
366 private function escape(array $arguments) | 366 private function escape(array $arguments) |
367 { | 367 { |
368 $args = array(); | 368 $args = []; |
369 foreach ($arguments as $k => $v) { | 369 foreach ($arguments as $k => $v) { |
370 if (is_array($v)) { | 370 if (\is_array($v)) { |
371 $args[$k] = $this->escape($v); | 371 $args[$k] = $this->escape($v); |
372 } elseif (is_string($v)) { | 372 } elseif (\is_string($v)) { |
373 $args[$k] = str_replace('%', '%%', $v); | 373 $args[$k] = str_replace('%', '%%', $v); |
374 } else { | 374 } else { |
375 $args[$k] = $v; | 375 $args[$k] = $v; |
376 } | 376 } |
377 } | 377 } |