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\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;
23 * XmlDumper dumps a service container as an XML string.
25 * @author Fabien Potencier <fabien@symfony.com>
26 * @author Martin HasoĊ <martin.hason@gmail.com>
28 class XmlDumper extends Dumper
36 * Dumps the service container as an XML string.
38 * @param array $options An array of options
40 * @return string An xml string representing of the service container
42 public function dump(array $options = array())
44 $this->document = new \DOMDocument('1.0', 'utf-8');
45 $this->document->formatOutput = true;
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');
51 $this->addParameters($container);
52 $this->addServices($container);
54 $this->document->appendChild($container);
55 $xml = $this->document->saveXML();
56 $this->document = null;
64 * @param \DOMElement $parent
66 private function addParameters(\DOMElement $parent)
68 $data = $this->container->getParameterBag()->all();
73 if ($this->container->isFrozen()) {
74 $data = $this->escape($data);
77 $parameters = $this->document->createElement('parameters');
78 $parent->appendChild($parameters);
79 $this->convertParameters($data, 'parameter', $parameters);
85 * @param array $methodcalls
86 * @param \DOMElement $parent
88 private function addMethodCalls(array $methodcalls, \DOMElement $parent)
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);
96 $parent->appendChild($call);
103 * @param Definition $definition
105 * @param \DOMElement $parent
107 private function addService($definition, $id, \DOMElement $parent)
109 $service = $this->document->createElement('service');
111 $service->setAttribute('id', $id);
113 if ($class = $definition->getClass()) {
114 if ('\\' === substr($class, 0, 1)) {
115 $class = substr($class, 1);
118 $service->setAttribute('class', $class);
120 if ($definition->getFactoryMethod(false)) {
121 $service->setAttribute('factory-method', $definition->getFactoryMethod(false));
123 if ($definition->getFactoryClass(false)) {
124 $service->setAttribute('factory-class', $definition->getFactoryClass(false));
126 if ($definition->getFactoryService(false)) {
127 $service->setAttribute('factory-service', $definition->getFactoryService(false));
129 if (!$definition->isShared()) {
130 $service->setAttribute('shared', 'false');
132 if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope(false)) {
133 $service->setAttribute('scope', $scope);
135 if (!$definition->isPublic()) {
136 $service->setAttribute('public', 'false');
138 if ($definition->isSynthetic()) {
139 $service->setAttribute('synthetic', 'true');
141 if ($definition->isSynchronized(false)) {
142 $service->setAttribute('synchronized', 'true');
144 if ($definition->isLazy()) {
145 $service->setAttribute('lazy', 'true');
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);
153 if (0 !== $priority) {
154 $service->setAttribute('decoration-priority', $priority);
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);
165 $service->appendChild($tag);
169 if ($definition->getFile()) {
170 $file = $this->document->createElement('file');
171 $file->appendChild($this->document->createTextNode($definition->getFile()));
172 $service->appendChild($file);
175 if ($parameters = $definition->getArguments()) {
176 $this->convertParameters($parameters, 'argument', $service);
179 if ($parameters = $definition->getProperties()) {
180 $this->convertParameters($parameters, 'property', $service, 'name');
183 $this->addMethodCalls($definition->getMethodCalls(), $service);
185 if ($callable = $definition->getFactory()) {
186 $factory = $this->document->createElement('factory');
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]);
195 $factory->setAttribute('function', $callable);
197 $service->appendChild($factory);
200 if ($definition->isDeprecated()) {
201 $deprecated = $this->document->createElement('deprecated');
202 $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
204 $service->appendChild($deprecated);
207 if ($definition->isAutowired()) {
208 $service->setAttribute('autowire', 'true');
211 foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
212 $autowiringType = $this->document->createElement('autowiring-type');
213 $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
215 $service->appendChild($autowiringType);
218 if ($callable = $definition->getConfigurator()) {
219 $configurator = $this->document->createElement('configurator');
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]);
228 $configurator->setAttribute('function', $callable);
230 $service->appendChild($configurator);
233 $parent->appendChild($service);
237 * Adds a service alias.
239 * @param string $alias
241 * @param \DOMElement $parent
243 private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
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');
251 $parent->appendChild($service);
257 * @param \DOMElement $parent
259 private function addServices(\DOMElement $parent)
261 $definitions = $this->container->getDefinitions();
266 $services = $this->document->createElement('services');
267 foreach ($definitions as $id => $definition) {
268 $this->addService($definition, $id, $services);
271 $aliases = $this->container->getAliases();
272 foreach ($aliases as $alias => $id) {
273 while (isset($aliases[(string) $id])) {
274 $id = $aliases[(string) $id];
276 $this->addServiceAlias($alias, $id, $services);
278 $parent->appendChild($services);
282 * Converts parameters.
284 * @param array $parameters
285 * @param string $type
286 * @param \DOMElement $parent
287 * @param string $keyAttribute
289 private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
291 $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
292 foreach ($parameters as $key => $value) {
293 $element = $this->document->createElement($type);
295 $element->setAttribute($keyAttribute, $key);
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');
310 if (!$value->isStrict(false)) {
311 $element->setAttribute('strict', 'false');
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);
321 if (in_array($value, array('null', 'true', 'false'), true)) {
322 $element->setAttribute('type', 'string');
324 $text = $this->document->createTextNode(self::phpToXml($value));
325 $element->appendChild($text);
327 $parent->appendChild($element);
334 * @param array $arguments
338 private function escape(array $arguments)
341 foreach ($arguments as $k => $v) {
343 $args[$k] = $this->escape($v);
344 } elseif (is_string($v)) {
345 $args[$k] = str_replace('%', '%%', $v);
355 * Converts php types to xml types.
357 * @param mixed $value Value to convert
361 * @throws RuntimeException When trying to dump object or resource
363 public static function phpToXml($value)
366 case null === $value:
368 case true === $value:
370 case false === $value:
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.');
377 return (string) $value;