4410a14704acaf354755a8619ecb4bcbafd75ae7
[yaffs-website] / vendor / symfony / dependency-injection / Dumper / XmlDumper.php
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\Dumper;
13
14 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
15 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
16 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
17 use Symfony\Component\DependencyInjection\ContainerInterface;
18 use Symfony\Component\DependencyInjection\Parameter;
19 use Symfony\Component\DependencyInjection\Reference;
20 use Symfony\Component\DependencyInjection\Definition;
21 use Symfony\Component\DependencyInjection\Alias;
22 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
23 use Symfony\Component\ExpressionLanguage\Expression;
24
25 /**
26  * XmlDumper dumps a service container as an XML string.
27  *
28  * @author Fabien Potencier <fabien@symfony.com>
29  * @author Martin HasoĊˆ <martin.hason@gmail.com>
30  */
31 class XmlDumper extends Dumper
32 {
33     /**
34      * @var \DOMDocument
35      */
36     private $document;
37
38     /**
39      * Dumps the service container as an XML string.
40      *
41      * @return string An xml string representing of the service container
42      */
43     public function dump(array $options = array())
44     {
45         $this->document = new \DOMDocument('1.0', 'utf-8');
46         $this->document->formatOutput = true;
47
48         $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
49         $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
50         $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
51
52         $this->addParameters($container);
53         $this->addServices($container);
54
55         $this->document->appendChild($container);
56         $xml = $this->document->saveXML();
57         $this->document = null;
58
59         return $this->container->resolveEnvPlaceholders($xml);
60     }
61
62     private function addParameters(\DOMElement $parent)
63     {
64         $data = $this->container->getParameterBag()->all();
65         if (!$data) {
66             return;
67         }
68
69         if ($this->container->isCompiled()) {
70             $data = $this->escape($data);
71         }
72
73         $parameters = $this->document->createElement('parameters');
74         $parent->appendChild($parameters);
75         $this->convertParameters($data, 'parameter', $parameters);
76     }
77
78     private function addMethodCalls(array $methodcalls, \DOMElement $parent)
79     {
80         foreach ($methodcalls as $methodcall) {
81             $call = $this->document->createElement('call');
82             $call->setAttribute('method', $methodcall[0]);
83             if (count($methodcall[1])) {
84                 $this->convertParameters($methodcall[1], 'argument', $call);
85             }
86             $parent->appendChild($call);
87         }
88     }
89
90     /**
91      * Adds a service.
92      *
93      * @param Definition  $definition
94      * @param string      $id
95      * @param \DOMElement $parent
96      */
97     private function addService($definition, $id, \DOMElement $parent)
98     {
99         $service = $this->document->createElement('service');
100         if (null !== $id) {
101             $service->setAttribute('id', $id);
102         }
103         if ($class = $definition->getClass()) {
104             if ('\\' === substr($class, 0, 1)) {
105                 $class = substr($class, 1);
106             }
107
108             $service->setAttribute('class', $class);
109         }
110         if (!$definition->isShared()) {
111             $service->setAttribute('shared', 'false');
112         }
113         if (!$definition->isPrivate()) {
114             $service->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
115         }
116         if ($definition->isSynthetic()) {
117             $service->setAttribute('synthetic', 'true');
118         }
119         if ($definition->isLazy()) {
120             $service->setAttribute('lazy', 'true');
121         }
122         if (null !== $decorated = $definition->getDecoratedService()) {
123             list($decorated, $renamedId, $priority) = $decorated;
124             $service->setAttribute('decorates', $decorated);
125             if (null !== $renamedId) {
126                 $service->setAttribute('decoration-inner-name', $renamedId);
127             }
128             if (0 !== $priority) {
129                 $service->setAttribute('decoration-priority', $priority);
130             }
131         }
132
133         foreach ($definition->getTags() as $name => $tags) {
134             foreach ($tags as $attributes) {
135                 $tag = $this->document->createElement('tag');
136                 $tag->setAttribute('name', $name);
137                 foreach ($attributes as $key => $value) {
138                     $tag->setAttribute($key, $value);
139                 }
140                 $service->appendChild($tag);
141             }
142         }
143
144         if ($definition->getFile()) {
145             $file = $this->document->createElement('file');
146             $file->appendChild($this->document->createTextNode($definition->getFile()));
147             $service->appendChild($file);
148         }
149
150         if ($parameters = $definition->getArguments()) {
151             $this->convertParameters($parameters, 'argument', $service);
152         }
153
154         if ($parameters = $definition->getProperties()) {
155             $this->convertParameters($parameters, 'property', $service, 'name');
156         }
157
158         $this->addMethodCalls($definition->getMethodCalls(), $service);
159
160         if ($callable = $definition->getFactory()) {
161             $factory = $this->document->createElement('factory');
162
163             if (is_array($callable) && $callable[0] instanceof Definition) {
164                 $this->addService($callable[0], null, $factory);
165                 $factory->setAttribute('method', $callable[1]);
166             } elseif (is_array($callable)) {
167                 if (null !== $callable[0]) {
168                     $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
169                 }
170                 $factory->setAttribute('method', $callable[1]);
171             } else {
172                 $factory->setAttribute('function', $callable);
173             }
174             $service->appendChild($factory);
175         }
176
177         if ($definition->isDeprecated()) {
178             $deprecated = $this->document->createElement('deprecated');
179             $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
180
181             $service->appendChild($deprecated);
182         }
183
184         if ($definition->isAutowired()) {
185             $service->setAttribute('autowire', 'true');
186         }
187
188         foreach ($definition->getAutowiringTypes(false) as $autowiringTypeValue) {
189             $autowiringType = $this->document->createElement('autowiring-type');
190             $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
191
192             $service->appendChild($autowiringType);
193         }
194
195         if ($definition->isAutoconfigured()) {
196             $service->setAttribute('autoconfigure', 'true');
197         }
198
199         if ($definition->isAbstract()) {
200             $service->setAttribute('abstract', 'true');
201         }
202
203         if ($callable = $definition->getConfigurator()) {
204             $configurator = $this->document->createElement('configurator');
205
206             if (is_array($callable) && $callable[0] instanceof Definition) {
207                 $this->addService($callable[0], null, $configurator);
208                 $configurator->setAttribute('method', $callable[1]);
209             } elseif (is_array($callable)) {
210                 $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
211                 $configurator->setAttribute('method', $callable[1]);
212             } else {
213                 $configurator->setAttribute('function', $callable);
214             }
215             $service->appendChild($configurator);
216         }
217
218         $parent->appendChild($service);
219     }
220
221     /**
222      * Adds a service alias.
223      *
224      * @param string      $alias
225      * @param Alias       $id
226      * @param \DOMElement $parent
227      */
228     private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
229     {
230         $service = $this->document->createElement('service');
231         $service->setAttribute('id', $alias);
232         $service->setAttribute('alias', $id);
233         if (!$id->isPrivate()) {
234             $service->setAttribute('public', $id->isPublic() ? 'true' : 'false');
235         }
236         $parent->appendChild($service);
237     }
238
239     private function addServices(\DOMElement $parent)
240     {
241         $definitions = $this->container->getDefinitions();
242         if (!$definitions) {
243             return;
244         }
245
246         $services = $this->document->createElement('services');
247         foreach ($definitions as $id => $definition) {
248             $this->addService($definition, $id, $services);
249         }
250
251         $aliases = $this->container->getAliases();
252         foreach ($aliases as $alias => $id) {
253             while (isset($aliases[(string) $id])) {
254                 $id = $aliases[(string) $id];
255             }
256             $this->addServiceAlias($alias, $id, $services);
257         }
258         $parent->appendChild($services);
259     }
260
261     /**
262      * Converts parameters.
263      *
264      * @param array       $parameters
265      * @param string      $type
266      * @param \DOMElement $parent
267      * @param string      $keyAttribute
268      */
269     private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
270     {
271         $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
272         foreach ($parameters as $key => $value) {
273             $element = $this->document->createElement($type);
274             if ($withKeys) {
275                 $element->setAttribute($keyAttribute, $key);
276             }
277
278             if ($value instanceof ServiceClosureArgument) {
279                 $value = $value->getValues()[0];
280             }
281             if (is_array($value)) {
282                 $element->setAttribute('type', 'collection');
283                 $this->convertParameters($value, $type, $element, 'key');
284             } elseif ($value instanceof TaggedIteratorArgument) {
285                 $element->setAttribute('type', 'tagged');
286                 $element->setAttribute('tag', $value->getTag());
287             } elseif ($value instanceof IteratorArgument) {
288                 $element->setAttribute('type', 'iterator');
289                 $this->convertParameters($value->getValues(), $type, $element, 'key');
290             } elseif ($value instanceof Reference) {
291                 $element->setAttribute('type', 'service');
292                 $element->setAttribute('id', (string) $value);
293                 $behaviour = $value->getInvalidBehavior();
294                 if (ContainerInterface::NULL_ON_INVALID_REFERENCE == $behaviour) {
295                     $element->setAttribute('on-invalid', 'null');
296                 } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE == $behaviour) {
297                     $element->setAttribute('on-invalid', 'ignore');
298                 } elseif (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE == $behaviour) {
299                     $element->setAttribute('on-invalid', 'ignore_uninitialized');
300                 }
301             } elseif ($value instanceof Definition) {
302                 $element->setAttribute('type', 'service');
303                 $this->addService($value, null, $element);
304             } elseif ($value instanceof Expression) {
305                 $element->setAttribute('type', 'expression');
306                 $text = $this->document->createTextNode(self::phpToXml((string) $value));
307                 $element->appendChild($text);
308             } else {
309                 if (in_array($value, array('null', 'true', 'false'), true)) {
310                     $element->setAttribute('type', 'string');
311                 }
312                 $text = $this->document->createTextNode(self::phpToXml($value));
313                 $element->appendChild($text);
314             }
315             $parent->appendChild($element);
316         }
317     }
318
319     /**
320      * Escapes arguments.
321      *
322      * @return array
323      */
324     private function escape(array $arguments)
325     {
326         $args = array();
327         foreach ($arguments as $k => $v) {
328             if (is_array($v)) {
329                 $args[$k] = $this->escape($v);
330             } elseif (is_string($v)) {
331                 $args[$k] = str_replace('%', '%%', $v);
332             } else {
333                 $args[$k] = $v;
334             }
335         }
336
337         return $args;
338     }
339
340     /**
341      * Converts php types to xml types.
342      *
343      * @param mixed $value Value to convert
344      *
345      * @return string
346      *
347      * @throws RuntimeException When trying to dump object or resource
348      */
349     public static function phpToXml($value)
350     {
351         switch (true) {
352             case null === $value:
353                 return 'null';
354             case true === $value:
355                 return 'true';
356             case false === $value:
357                 return 'false';
358             case $value instanceof Parameter:
359                 return '%'.$value.'%';
360             case is_object($value) || is_resource($value):
361                 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
362             default:
363                 return (string) $value;
364         }
365     }
366 }