44922717afbff1a48800cb3ad917000486633b1e
[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 $this->container->resolveEnvPlaceholders($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->isShared()) {
121             $service->setAttribute('shared', 'false');
122         }
123         if (!$definition->isPublic()) {
124             $service->setAttribute('public', 'false');
125         }
126         if ($definition->isSynthetic()) {
127             $service->setAttribute('synthetic', 'true');
128         }
129         if ($definition->isLazy()) {
130             $service->setAttribute('lazy', 'true');
131         }
132         if (null !== $decorated = $definition->getDecoratedService()) {
133             list($decorated, $renamedId, $priority) = $decorated;
134             $service->setAttribute('decorates', $decorated);
135             if (null !== $renamedId) {
136                 $service->setAttribute('decoration-inner-name', $renamedId);
137             }
138             if (0 !== $priority) {
139                 $service->setAttribute('decoration-priority', $priority);
140             }
141         }
142
143         foreach ($definition->getTags() as $name => $tags) {
144             foreach ($tags as $attributes) {
145                 $tag = $this->document->createElement('tag');
146                 $tag->setAttribute('name', $name);
147                 foreach ($attributes as $key => $value) {
148                     $tag->setAttribute($key, $value);
149                 }
150                 $service->appendChild($tag);
151             }
152         }
153
154         if ($definition->getFile()) {
155             $file = $this->document->createElement('file');
156             $file->appendChild($this->document->createTextNode($definition->getFile()));
157             $service->appendChild($file);
158         }
159
160         if ($parameters = $definition->getArguments()) {
161             $this->convertParameters($parameters, 'argument', $service);
162         }
163
164         if ($parameters = $definition->getProperties()) {
165             $this->convertParameters($parameters, 'property', $service, 'name');
166         }
167
168         $this->addMethodCalls($definition->getMethodCalls(), $service);
169
170         if ($callable = $definition->getFactory()) {
171             $factory = $this->document->createElement('factory');
172
173             if (is_array($callable) && $callable[0] instanceof Definition) {
174                 $this->addService($callable[0], null, $factory);
175                 $factory->setAttribute('method', $callable[1]);
176             } elseif (is_array($callable)) {
177                 $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
178                 $factory->setAttribute('method', $callable[1]);
179             } else {
180                 $factory->setAttribute('function', $callable);
181             }
182             $service->appendChild($factory);
183         }
184
185         if ($definition->isDeprecated()) {
186             $deprecated = $this->document->createElement('deprecated');
187             $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
188
189             $service->appendChild($deprecated);
190         }
191
192         if ($definition->isAutowired()) {
193             $service->setAttribute('autowire', 'true');
194         }
195
196         foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
197             $autowiringType = $this->document->createElement('autowiring-type');
198             $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
199
200             $service->appendChild($autowiringType);
201         }
202
203         if ($definition->isAbstract()) {
204             $service->setAttribute('abstract', 'true');
205         }
206
207         if ($callable = $definition->getConfigurator()) {
208             $configurator = $this->document->createElement('configurator');
209
210             if (is_array($callable) && $callable[0] instanceof Definition) {
211                 $this->addService($callable[0], null, $configurator);
212                 $configurator->setAttribute('method', $callable[1]);
213             } elseif (is_array($callable)) {
214                 $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
215                 $configurator->setAttribute('method', $callable[1]);
216             } else {
217                 $configurator->setAttribute('function', $callable);
218             }
219             $service->appendChild($configurator);
220         }
221
222         $parent->appendChild($service);
223     }
224
225     /**
226      * Adds a service alias.
227      *
228      * @param string      $alias
229      * @param Alias       $id
230      * @param \DOMElement $parent
231      */
232     private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
233     {
234         $service = $this->document->createElement('service');
235         $service->setAttribute('id', $alias);
236         $service->setAttribute('alias', $id);
237         if (!$id->isPublic()) {
238             $service->setAttribute('public', 'false');
239         }
240         $parent->appendChild($service);
241     }
242
243     /**
244      * Adds services.
245      *
246      * @param \DOMElement $parent
247      */
248     private function addServices(\DOMElement $parent)
249     {
250         $definitions = $this->container->getDefinitions();
251         if (!$definitions) {
252             return;
253         }
254
255         $services = $this->document->createElement('services');
256         foreach ($definitions as $id => $definition) {
257             $this->addService($definition, $id, $services);
258         }
259
260         $aliases = $this->container->getAliases();
261         foreach ($aliases as $alias => $id) {
262             while (isset($aliases[(string) $id])) {
263                 $id = $aliases[(string) $id];
264             }
265             $this->addServiceAlias($alias, $id, $services);
266         }
267         $parent->appendChild($services);
268     }
269
270     /**
271      * Converts parameters.
272      *
273      * @param array       $parameters
274      * @param string      $type
275      * @param \DOMElement $parent
276      * @param string      $keyAttribute
277      */
278     private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
279     {
280         $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
281         foreach ($parameters as $key => $value) {
282             $element = $this->document->createElement($type);
283             if ($withKeys) {
284                 $element->setAttribute($keyAttribute, $key);
285             }
286
287             if (is_array($value)) {
288                 $element->setAttribute('type', 'collection');
289                 $this->convertParameters($value, $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 ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) {
295                     $element->setAttribute('on-invalid', 'null');
296                 } elseif ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
297                     $element->setAttribute('on-invalid', 'ignore');
298                 }
299             } elseif ($value instanceof Definition) {
300                 $element->setAttribute('type', 'service');
301                 $this->addService($value, null, $element);
302             } elseif ($value instanceof Expression) {
303                 $element->setAttribute('type', 'expression');
304                 $text = $this->document->createTextNode(self::phpToXml((string) $value));
305                 $element->appendChild($text);
306             } else {
307                 if (in_array($value, array('null', 'true', 'false'), true)) {
308                     $element->setAttribute('type', 'string');
309                 }
310                 $text = $this->document->createTextNode(self::phpToXml($value));
311                 $element->appendChild($text);
312             }
313             $parent->appendChild($element);
314         }
315     }
316
317     /**
318      * Escapes arguments.
319      *
320      * @param array $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 }