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