Version 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 ($callable = $definition->getConfigurator()) {
219             $configurator = $this->document->createElement('configurator');
220
221             if (is_array($callable) && $callable[0] instanceof Definition) {
222                 $this->addService($callable[0], null, $configurator);
223                 $configurator->setAttribute('method', $callable[1]);
224             } elseif (is_array($callable)) {
225                 $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
226                 $configurator->setAttribute('method', $callable[1]);
227             } else {
228                 $configurator->setAttribute('function', $callable);
229             }
230             $service->appendChild($configurator);
231         }
232
233         $parent->appendChild($service);
234     }
235
236     /**
237      * Adds a service alias.
238      *
239      * @param string      $alias
240      * @param Alias       $id
241      * @param \DOMElement $parent
242      */
243     private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
244     {
245         $service = $this->document->createElement('service');
246         $service->setAttribute('id', $alias);
247         $service->setAttribute('alias', $id);
248         if (!$id->isPublic()) {
249             $service->setAttribute('public', 'false');
250         }
251         $parent->appendChild($service);
252     }
253
254     /**
255      * Adds services.
256      *
257      * @param \DOMElement $parent
258      */
259     private function addServices(\DOMElement $parent)
260     {
261         $definitions = $this->container->getDefinitions();
262         if (!$definitions) {
263             return;
264         }
265
266         $services = $this->document->createElement('services');
267         foreach ($definitions as $id => $definition) {
268             $this->addService($definition, $id, $services);
269         }
270
271         $aliases = $this->container->getAliases();
272         foreach ($aliases as $alias => $id) {
273             while (isset($aliases[(string) $id])) {
274                 $id = $aliases[(string) $id];
275             }
276             $this->addServiceAlias($alias, $id, $services);
277         }
278         $parent->appendChild($services);
279     }
280
281     /**
282      * Converts parameters.
283      *
284      * @param array       $parameters
285      * @param string      $type
286      * @param \DOMElement $parent
287      * @param string      $keyAttribute
288      */
289     private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
290     {
291         $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
292         foreach ($parameters as $key => $value) {
293             $element = $this->document->createElement($type);
294             if ($withKeys) {
295                 $element->setAttribute($keyAttribute, $key);
296             }
297
298             if (is_array($value)) {
299                 $element->setAttribute('type', 'collection');
300                 $this->convertParameters($value, $type, $element, 'key');
301             } elseif ($value instanceof Reference) {
302                 $element->setAttribute('type', 'service');
303                 $element->setAttribute('id', (string) $value);
304                 $behaviour = $value->getInvalidBehavior();
305                 if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
306                     $element->setAttribute('on-invalid', 'null');
307                 } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
308                     $element->setAttribute('on-invalid', 'ignore');
309                 }
310                 if (!$value->isStrict(false)) {
311                     $element->setAttribute('strict', 'false');
312                 }
313             } elseif ($value instanceof Definition) {
314                 $element->setAttribute('type', 'service');
315                 $this->addService($value, null, $element);
316             } elseif ($value instanceof Expression) {
317                 $element->setAttribute('type', 'expression');
318                 $text = $this->document->createTextNode(self::phpToXml((string) $value));
319                 $element->appendChild($text);
320             } else {
321                 if (in_array($value, array('null', 'true', 'false'), true)) {
322                     $element->setAttribute('type', 'string');
323                 }
324                 $text = $this->document->createTextNode(self::phpToXml($value));
325                 $element->appendChild($text);
326             }
327             $parent->appendChild($element);
328         }
329     }
330
331     /**
332      * Escapes arguments.
333      *
334      * @param array $arguments
335      *
336      * @return array
337      */
338     private function escape(array $arguments)
339     {
340         $args = array();
341         foreach ($arguments as $k => $v) {
342             if (is_array($v)) {
343                 $args[$k] = $this->escape($v);
344             } elseif (is_string($v)) {
345                 $args[$k] = str_replace('%', '%%', $v);
346             } else {
347                 $args[$k] = $v;
348             }
349         }
350
351         return $args;
352     }
353
354     /**
355      * Converts php types to xml types.
356      *
357      * @param mixed $value Value to convert
358      *
359      * @return string
360      *
361      * @throws RuntimeException When trying to dump object or resource
362      */
363     public static function phpToXml($value)
364     {
365         switch (true) {
366             case null === $value:
367                 return 'null';
368             case true === $value:
369                 return 'true';
370             case false === $value:
371                 return 'false';
372             case $value instanceof Parameter:
373                 return '%'.$value.'%';
374             case is_object($value) || is_resource($value):
375                 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
376             default:
377                 return (string) $value;
378         }
379     }
380 }