4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Dumper;
14 use Symfony\Component\DependencyInjection\Alias;
15 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
16 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
17 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
18 use Symfony\Component\DependencyInjection\ContainerInterface;
19 use Symfony\Component\DependencyInjection\Definition;
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21 use Symfony\Component\DependencyInjection\Parameter;
22 use Symfony\Component\DependencyInjection\Reference;
23 use Symfony\Component\ExpressionLanguage\Expression;
26 * XmlDumper dumps a service container as an XML string.
28 * @author Fabien Potencier <fabien@symfony.com>
29 * @author Martin HasoĊ <martin.hason@gmail.com>
31 class XmlDumper extends Dumper
39 * Dumps the service container as an XML string.
41 * @return string An xml string representing of the service container
43 public function dump(array $options = array())
45 $this->document = new \DOMDocument('1.0', 'utf-8');
46 $this->document->formatOutput = true;
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');
52 $this->addParameters($container);
53 $this->addServices($container);
55 $this->document->appendChild($container);
56 $xml = $this->document->saveXML();
57 $this->document = null;
59 return $this->container->resolveEnvPlaceholders($xml);
62 private function addParameters(\DOMElement $parent)
64 $data = $this->container->getParameterBag()->all();
69 if ($this->container->isCompiled()) {
70 $data = $this->escape($data);
73 $parameters = $this->document->createElement('parameters');
74 $parent->appendChild($parameters);
75 $this->convertParameters($data, 'parameter', $parameters);
78 private function addMethodCalls(array $methodcalls, \DOMElement $parent)
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);
86 $parent->appendChild($call);
93 * @param Definition $definition
95 * @param \DOMElement $parent
97 private function addService($definition, $id, \DOMElement $parent)
99 $service = $this->document->createElement('service');
101 $service->setAttribute('id', $id);
103 if ($class = $definition->getClass()) {
104 if ('\\' === substr($class, 0, 1)) {
105 $class = substr($class, 1);
108 $service->setAttribute('class', $class);
110 if (!$definition->isShared()) {
111 $service->setAttribute('shared', 'false');
113 if (!$definition->isPrivate()) {
114 $service->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
116 if ($definition->isSynthetic()) {
117 $service->setAttribute('synthetic', 'true');
119 if ($definition->isLazy()) {
120 $service->setAttribute('lazy', 'true');
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);
128 if (0 !== $priority) {
129 $service->setAttribute('decoration-priority', $priority);
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);
140 $service->appendChild($tag);
144 if ($definition->getFile()) {
145 $file = $this->document->createElement('file');
146 $file->appendChild($this->document->createTextNode($definition->getFile()));
147 $service->appendChild($file);
150 if ($parameters = $definition->getArguments()) {
151 $this->convertParameters($parameters, 'argument', $service);
154 if ($parameters = $definition->getProperties()) {
155 $this->convertParameters($parameters, 'property', $service, 'name');
158 $this->addMethodCalls($definition->getMethodCalls(), $service);
160 if ($callable = $definition->getFactory()) {
161 $factory = $this->document->createElement('factory');
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]);
170 $factory->setAttribute('method', $callable[1]);
172 $factory->setAttribute('function', $callable);
174 $service->appendChild($factory);
177 if ($definition->isDeprecated()) {
178 $deprecated = $this->document->createElement('deprecated');
179 $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
181 $service->appendChild($deprecated);
184 if ($definition->isAutowired()) {
185 $service->setAttribute('autowire', 'true');
188 foreach ($definition->getAutowiringTypes(false) as $autowiringTypeValue) {
189 $autowiringType = $this->document->createElement('autowiring-type');
190 $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
192 $service->appendChild($autowiringType);
195 if ($definition->isAutoconfigured()) {
196 $service->setAttribute('autoconfigure', 'true');
199 if ($definition->isAbstract()) {
200 $service->setAttribute('abstract', 'true');
203 if ($callable = $definition->getConfigurator()) {
204 $configurator = $this->document->createElement('configurator');
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]);
213 $configurator->setAttribute('function', $callable);
215 $service->appendChild($configurator);
218 $parent->appendChild($service);
222 * Adds a service alias.
224 * @param string $alias
226 * @param \DOMElement $parent
228 private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
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');
236 $parent->appendChild($service);
239 private function addServices(\DOMElement $parent)
241 $definitions = $this->container->getDefinitions();
246 $services = $this->document->createElement('services');
247 foreach ($definitions as $id => $definition) {
248 $this->addService($definition, $id, $services);
251 $aliases = $this->container->getAliases();
252 foreach ($aliases as $alias => $id) {
253 while (isset($aliases[(string) $id])) {
254 $id = $aliases[(string) $id];
256 $this->addServiceAlias($alias, $id, $services);
258 $parent->appendChild($services);
262 * Converts parameters.
264 * @param array $parameters
265 * @param string $type
266 * @param \DOMElement $parent
267 * @param string $keyAttribute
269 private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
271 $withKeys = array_keys($parameters) !== range(0, \count($parameters) - 1);
272 foreach ($parameters as $key => $value) {
273 $element = $this->document->createElement($type);
275 $element->setAttribute($keyAttribute, $key);
278 if ($value instanceof ServiceClosureArgument) {
279 $value = $value->getValues()[0];
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');
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);
309 if (\in_array($value, array('null', 'true', 'false'), true)) {
310 $element->setAttribute('type', 'string');
312 $text = $this->document->createTextNode(self::phpToXml($value));
313 $element->appendChild($text);
315 $parent->appendChild($element);
324 private function escape(array $arguments)
327 foreach ($arguments as $k => $v) {
329 $args[$k] = $this->escape($v);
330 } elseif (\is_string($v)) {
331 $args[$k] = str_replace('%', '%%', $v);
341 * Converts php types to xml types.
343 * @param mixed $value Value to convert
347 * @throws RuntimeException When trying to dump object or resource
349 public static function phpToXml($value)
352 case null === $value:
354 case true === $value:
356 case false === $value:
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.');
363 return (string) $value;