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 * @return string An xml string representing of the service container
40 public function dump(array $options = array())
42 $this->document
= new \
DOMDocument('1.0', 'utf-8');
43 $this->document
->formatOutput
= true;
45 $container = $this->document
->createElementNS('http://symfony.com/schema/dic/services', 'container');
46 $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
47 $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
49 $this->addParameters($container);
50 $this->addServices($container);
52 $this->document
->appendChild($container);
53 $xml = $this->document
->saveXML();
54 $this->document
= null;
59 private function addParameters(\DOMElement
$parent)
61 $data = $this->container
->getParameterBag()->all();
66 if ($this->container
->isFrozen()) {
67 $data = $this->escape($data);
70 $parameters = $this->document
->createElement('parameters');
71 $parent->appendChild($parameters);
72 $this->convertParameters($data, 'parameter', $parameters);
75 private function addMethodCalls(array $methodcalls, \DOMElement
$parent)
77 foreach ($methodcalls as $methodcall) {
78 $call = $this->document
->createElement('call');
79 $call->setAttribute('method', $methodcall[0]);
80 if (count($methodcall[1])) {
81 $this->convertParameters($methodcall[1], 'argument', $call);
83 $parent->appendChild($call);
90 * @param Definition $definition
92 * @param \DOMElement $parent
94 private function addService($definition, $id, \DOMElement
$parent)
96 $service = $this->document
->createElement('service');
98 $service->setAttribute('id', $id);
100 if ($class = $definition->getClass()) {
101 if ('\\' === substr($class, 0, 1)) {
102 $class = substr($class, 1);
105 $service->setAttribute('class', $class);
107 if ($definition->getFactoryMethod(false)) {
108 $service->setAttribute('factory-method', $definition->getFactoryMethod(false));
110 if ($definition->getFactoryClass(false)) {
111 $service->setAttribute('factory-class', $definition->getFactoryClass(false));
113 if ($definition->getFactoryService(false)) {
114 $service->setAttribute('factory-service', $definition->getFactoryService(false));
116 if (!$definition->isShared()) {
117 $service->setAttribute('shared', 'false');
119 if (ContainerInterface
::SCOPE_CONTAINER
!== $scope = $definition->getScope(false)) {
120 $service->setAttribute('scope', $scope);
122 if (!$definition->isPublic()) {
123 $service->setAttribute('public', 'false');
125 if ($definition->isSynthetic()) {
126 $service->setAttribute('synthetic', 'true');
128 if ($definition->isSynchronized(false)) {
129 $service->setAttribute('synchronized', 'true');
131 if ($definition->isLazy()) {
132 $service->setAttribute('lazy', 'true');
134 if (null !== $decorated = $definition->getDecoratedService()) {
135 list($decorated, $renamedId, $priority) = $decorated;
136 $service->setAttribute('decorates', $decorated);
137 if (null !== $renamedId) {
138 $service->setAttribute('decoration-inner-name', $renamedId);
140 if (0 !== $priority) {
141 $service->setAttribute('decoration-priority', $priority);
145 foreach ($definition->getTags() as $name => $tags) {
146 foreach ($tags as $attributes) {
147 $tag = $this->document
->createElement('tag');
148 $tag->setAttribute('name', $name);
149 foreach ($attributes as $key => $value) {
150 $tag->setAttribute($key, $value);
152 $service->appendChild($tag);
156 if ($definition->getFile()) {
157 $file = $this->document
->createElement('file');
158 $file->appendChild($this->document
->createTextNode($definition->getFile()));
159 $service->appendChild($file);
162 if ($parameters = $definition->getArguments()) {
163 $this->convertParameters($parameters, 'argument', $service);
166 if ($parameters = $definition->getProperties()) {
167 $this->convertParameters($parameters, 'property', $service, 'name');
170 $this->addMethodCalls($definition->getMethodCalls(), $service);
172 if ($callable = $definition->getFactory()) {
173 $factory = $this->document
->createElement('factory');
175 if (is_array($callable) && $callable[0] instanceof Definition
) {
176 $this->addService($callable[0], null, $factory);
177 $factory->setAttribute('method', $callable[1]);
178 } elseif (is_array($callable)) {
179 $factory->setAttribute($callable[0] instanceof Reference ?
'service' : 'class', $callable[0]);
180 $factory->setAttribute('method', $callable[1]);
182 $factory->setAttribute('function', $callable);
184 $service->appendChild($factory);
187 if ($definition->isDeprecated()) {
188 $deprecated = $this->document
->createElement('deprecated');
189 $deprecated->appendChild($this->document
->createTextNode($definition->getDeprecationMessage('%service_id%')));
191 $service->appendChild($deprecated);
194 if ($definition->isAutowired()) {
195 $service->setAttribute('autowire', 'true');
198 foreach ($definition->getAutowiringTypes() as $autowiringTypeValue) {
199 $autowiringType = $this->document
->createElement('autowiring-type');
200 $autowiringType->appendChild($this->document
->createTextNode($autowiringTypeValue));
202 $service->appendChild($autowiringType);
205 if ($definition->isAbstract()) {
206 $service->setAttribute('abstract', 'true');
209 if ($callable = $definition->getConfigurator()) {
210 $configurator = $this->document
->createElement('configurator');
212 if (is_array($callable) && $callable[0] instanceof Definition
) {
213 $this->addService($callable[0], null, $configurator);
214 $configurator->setAttribute('method', $callable[1]);
215 } elseif (is_array($callable)) {
216 $configurator->setAttribute($callable[0] instanceof Reference ?
'service' : 'class', $callable[0]);
217 $configurator->setAttribute('method', $callable[1]);
219 $configurator->setAttribute('function', $callable);
221 $service->appendChild($configurator);
224 $parent->appendChild($service);
228 * Adds a service alias.
230 * @param string $alias
232 * @param \DOMElement $parent
234 private function addServiceAlias($alias, Alias
$id, \DOMElement
$parent)
236 $service = $this->document
->createElement('service');
237 $service->setAttribute('id', $alias);
238 $service->setAttribute('alias', $id);
239 if (!$id->isPublic()) {
240 $service->setAttribute('public', 'false');
242 $parent->appendChild($service);
245 private function addServices(\DOMElement
$parent)
247 $definitions = $this->container
->getDefinitions();
252 $services = $this->document
->createElement('services');
253 foreach ($definitions as $id => $definition) {
254 $this->addService($definition, $id, $services);
257 $aliases = $this->container
->getAliases();
258 foreach ($aliases as $alias => $id) {
259 while (isset($aliases[(string) $id])) {
260 $id = $aliases[(string) $id];
262 $this->addServiceAlias($alias, $id, $services);
264 $parent->appendChild($services);
268 * Converts parameters.
270 * @param array $parameters
271 * @param string $type
272 * @param \DOMElement $parent
273 * @param string $keyAttribute
275 private function convertParameters(array $parameters, $type, \DOMElement
$parent, $keyAttribute = 'key')
277 $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
278 foreach ($parameters as $key => $value) {
279 $element = $this->document
->createElement($type);
281 $element->setAttribute($keyAttribute, $key);
284 if (is_array($value)) {
285 $element->setAttribute('type', 'collection');
286 $this->convertParameters($value, $type, $element, 'key');
287 } elseif ($value instanceof Reference
) {
288 $element->setAttribute('type', 'service');
289 $element->setAttribute('id', (string) $value);
290 $behaviour = $value->getInvalidBehavior();
291 if (ContainerInterface
::NULL_ON_INVALID_REFERENCE
== $behaviour) {
292 $element->setAttribute('on-invalid', 'null');
293 } elseif (ContainerInterface
::IGNORE_ON_INVALID_REFERENCE
== $behaviour) {
294 $element->setAttribute('on-invalid', 'ignore');
296 if (!$value->isStrict(false)) {
297 $element->setAttribute('strict', 'false');
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);
307 if (in_array($value, array('null', 'true', 'false'), true)) {
308 $element->setAttribute('type', 'string');
310 $text = $this->document
->createTextNode(self
::phpToXml($value));
311 $element->appendChild($text);
313 $parent->appendChild($element);
322 private function escape(array $arguments)
325 foreach ($arguments as $k => $v) {
327 $args[$k] = $this->escape($v);
328 } elseif (is_string($v)) {
329 $args[$k] = str_replace('%', '%%', $v);
339 * Converts php types to xml types.
341 * @param mixed $value Value to convert
345 * @throws RuntimeException When trying to dump object or resource
347 public static function phpToXml($value)
350 case null === $value:
352 case true === $value:
354 case false === $value:
356 case $value instanceof Parameter
:
357 return '%'.$value.'%';
358 case is_object($value) ||
is_resource($value):
359 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
361 return (string) $value;