comparison vendor/symfony/dependency-injection/Dumper/YamlDumper.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
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; 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;
15 use Symfony\Component\DependencyInjection\Alias; 18 use Symfony\Component\DependencyInjection\Alias;
19 use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
20 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
21 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
22 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
16 use Symfony\Component\DependencyInjection\ContainerInterface; 23 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\DependencyInjection\Definition; 24 use Symfony\Component\DependencyInjection\Definition;
18 use Symfony\Component\DependencyInjection\Parameter; 25 use Symfony\Component\DependencyInjection\Parameter;
19 use Symfony\Component\DependencyInjection\Reference; 26 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException; 27 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
30 private $dumper; 37 private $dumper;
31 38
32 /** 39 /**
33 * Dumps the service container as an YAML string. 40 * Dumps the service container as an YAML string.
34 * 41 *
35 * @param array $options An array of options
36 *
37 * @return string A YAML string representing of the service container 42 * @return string A YAML string representing of the service container
38 */ 43 */
39 public function dump(array $options = array()) 44 public function dump(array $options = array())
40 { 45 {
41 if (!class_exists('Symfony\Component\Yaml\Dumper')) { 46 if (!class_exists('Symfony\Component\Yaml\Dumper')) {
55 * @param string $id 60 * @param string $id
56 * @param Definition $definition 61 * @param Definition $definition
57 * 62 *
58 * @return string 63 * @return string
59 */ 64 */
60 private function addService($id, $definition) 65 private function addService($id, Definition $definition)
61 { 66 {
62 $code = " $id:\n"; 67 $code = " $id:\n";
63 if ($class = $definition->getClass()) { 68 if ($class = $definition->getClass()) {
64 if ('\\' === substr($class, 0, 1)) { 69 if ('\\' === substr($class, 0, 1)) {
65 $class = substr($class, 1); 70 $class = substr($class, 1);
66 } 71 }
67 72
68 $code .= sprintf(" class: %s\n", $this->dumper->dump($class)); 73 $code .= sprintf(" class: %s\n", $this->dumper->dump($class));
69 } 74 }
70 75
71 if (!$definition->isPublic()) { 76 if (!$definition->isPrivate()) {
72 $code .= " public: false\n"; 77 $code .= sprintf(" public: %s\n", $definition->isPublic() ? 'true' : 'false');
73 } 78 }
74 79
75 $tagsCode = ''; 80 $tagsCode = '';
76 foreach ($definition->getTags() as $name => $tags) { 81 foreach ($definition->getTags() as $name => $tags) {
77 foreach ($tags as $attributes) { 82 foreach ($tags as $attributes) {
103 if ($definition->isAutowired()) { 108 if ($definition->isAutowired()) {
104 $code .= " autowire: true\n"; 109 $code .= " autowire: true\n";
105 } 110 }
106 111
107 $autowiringTypesCode = ''; 112 $autowiringTypesCode = '';
108 foreach ($definition->getAutowiringTypes() as $autowiringType) { 113 foreach ($definition->getAutowiringTypes(false) as $autowiringType) {
109 $autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType)); 114 $autowiringTypesCode .= sprintf(" - %s\n", $this->dumper->dump($autowiringType));
110 } 115 }
111 if ($autowiringTypesCode) { 116 if ($autowiringTypesCode) {
112 $code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode); 117 $code .= sprintf(" autowiring_types:\n%s", $autowiringTypesCode);
118 }
119
120 if ($definition->isAutoconfigured()) {
121 $code .= " autoconfigure: true\n";
122 }
123
124 if ($definition->isAbstract()) {
125 $code .= " abstract: true\n";
113 } 126 }
114 127
115 if ($definition->isLazy()) { 128 if ($definition->isLazy()) {
116 $code .= " lazy: true\n"; 129 $code .= " lazy: true\n";
117 } 130 }
160 * @param string $alias 173 * @param string $alias
161 * @param Alias $id 174 * @param Alias $id
162 * 175 *
163 * @return string 176 * @return string
164 */ 177 */
165 private function addServiceAlias($alias, $id) 178 private function addServiceAlias($alias, Alias $id)
166 { 179 {
167 if ($id->isPublic()) { 180 if ($id->isPrivate()) {
168 return sprintf(" %s: '@%s'\n", $alias, $id); 181 return sprintf(" %s: '@%s'\n", $alias, $id);
169 } 182 }
170 183
171 return sprintf(" %s:\n alias: %s\n public: false\n", $alias, $id); 184 return sprintf(" %s:\n alias: %s\n public: %s\n", $alias, $id, $id->isPublic() ? 'true' : 'false');
172 } 185 }
173 186
174 /** 187 /**
175 * Adds services. 188 * Adds services.
176 * 189 *
207 { 220 {
208 if (!$this->container->getParameterBag()->all()) { 221 if (!$this->container->getParameterBag()->all()) {
209 return ''; 222 return '';
210 } 223 }
211 224
212 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isFrozen()); 225 $parameters = $this->prepareParameters($this->container->getParameterBag()->all(), $this->container->isCompiled());
213 226
214 return $this->dumper->dump(array('parameters' => $parameters), 2); 227 return $this->dumper->dump(array('parameters' => $parameters), 2);
215 } 228 }
216 229
217 /** 230 /**
243 * 256 *
244 * @throws RuntimeException When trying to dump object or resource 257 * @throws RuntimeException When trying to dump object or resource
245 */ 258 */
246 private function dumpValue($value) 259 private function dumpValue($value)
247 { 260 {
261 if ($value instanceof ServiceClosureArgument) {
262 $value = $value->getValues()[0];
263 }
264 if ($value instanceof ArgumentInterface) {
265 if ($value instanceof TaggedIteratorArgument) {
266 return new TaggedValue('tagged', $value->getTag());
267 }
268 if ($value instanceof IteratorArgument) {
269 $tag = 'iterator';
270 } else {
271 throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', get_class($value)));
272 }
273
274 return new TaggedValue($tag, $this->dumpValue($value->getValues()));
275 }
276
248 if (is_array($value)) { 277 if (is_array($value)) {
249 $code = array(); 278 $code = array();
250 foreach ($value as $k => $v) { 279 foreach ($value as $k => $v) {
251 $code[$k] = $this->dumpValue($v); 280 $code[$k] = $this->dumpValue($v);
252 } 281 }
256 return $this->getServiceCall((string) $value, $value); 285 return $this->getServiceCall((string) $value, $value);
257 } elseif ($value instanceof Parameter) { 286 } elseif ($value instanceof Parameter) {
258 return $this->getParameterCall((string) $value); 287 return $this->getParameterCall((string) $value);
259 } elseif ($value instanceof Expression) { 288 } elseif ($value instanceof Expression) {
260 return $this->getExpressionCall((string) $value); 289 return $this->getExpressionCall((string) $value);
290 } elseif ($value instanceof Definition) {
291 return new TaggedValue('service', (new Parser())->parse("_:\n".$this->addService('_', $value), Yaml::PARSE_CUSTOM_TAGS)['_']['_']);
261 } elseif (is_object($value) || is_resource($value)) { 292 } elseif (is_object($value) || is_resource($value)) {
262 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.');
263 } 294 }
264 295
265 return $value; 296 return $value;
273 * 304 *
274 * @return string 305 * @return string
275 */ 306 */
276 private function getServiceCall($id, Reference $reference = null) 307 private function getServiceCall($id, Reference $reference = null)
277 { 308 {
278 if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference->getInvalidBehavior()) { 309 if (null !== $reference) {
279 return sprintf('@?%s', $id); 310 switch ($reference->getInvalidBehavior()) {
311 case ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE: break;
312 case ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE: return sprintf('@!%s', $id);
313 default: return sprintf('@?%s', $id);
314 }
280 } 315 }
281 316
282 return sprintf('@%s', $id); 317 return sprintf('@%s', $id);
283 } 318 }
284 319
323 return $escape ? $this->escape($filtered) : $filtered; 358 return $escape ? $this->escape($filtered) : $filtered;
324 } 359 }
325 360
326 /** 361 /**
327 * Escapes arguments. 362 * Escapes arguments.
328 *
329 * @param array $arguments
330 * 363 *
331 * @return array 364 * @return array
332 */ 365 */
333 private function escape(array $arguments) 366 private function escape(array $arguments)
334 { 367 {