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
;
22 * XmlDumper dumps a service container as an XML string.
24 * @author Fabien Potencier <fabien@symfony.com>
25 * @author Martin HasoĊ <martin.hason@gmail.com>
27 class XmlDumper
extends Dumper
35 * Dumps the service container as an XML string.
37 * @param array $options An array of options
39 * @return string An xml string representing of the service container
41 public function dump(array $options = array())
43 $this->document
= new \
DOMDocument('1.0', 'utf-8');
44 $this->document
->formatOutput
= true;
46 $container = $this->document
->createElementNS('http://symfony.com/schema/dic/services', 'container');
47 $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
48 $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd');
50 $this->addParameters($container);
51 $this->addServices($container);
53 $this->document
->appendChild($container);
54 $xml = $this->document
->saveXML();
55 $this->document
= null;
63 * @param \DOMElement $parent
65 private function addParameters(\DOMElement
$parent)
67 $data = $this->container
->getParameterBag()->all();
72 if ($this->container
->isFrozen()) {
73 $data = $this->escape($data);
76 $parameters = $this->document
->createElement('parameters');
77 $parent->appendChild($parameters);
78 $this->convertParameters($data, 'parameter', $parameters);
84 * @param array $methodcalls
85 * @param \DOMElement $parent
87 private function addMethodCalls(array $methodcalls, \DOMElement
$parent)
89 foreach ($methodcalls as $methodcall) {
90 $call = $this->document
->createElement('call');
91 $call->setAttribute('method', $methodcall[0]);
92 if (count($methodcall[1])) {
93 $this->convertParameters($methodcall[1], 'argument', $call);
95 $parent->appendChild($call);
102 * @param Definition $definition
104 * @param \DOMElement $parent
106 private function addService($definition, $id, \DOMElement
$parent)
108 $service = $this->document
->createElement('service');
110 $service->setAttribute('id', $id);
112 if ($class = $definition->getClass()) {
113 if ('\\' === substr($class, 0, 1)) {
114 $class = substr($class, 1);
117 $service->setAttribute('class', $class);
119 if ($definition->getFactoryMethod()) {
120 $service->setAttribute('factory-method', $definition->getFactoryMethod());
122 if ($definition->getFactoryClass()) {
123 $service->setAttribute('factory-class', $definition->getFactoryClass());
125 if ($definition->getFactoryService()) {
126 $service->setAttribute('factory-service', $definition->getFactoryService());
128 if (ContainerInterface
::SCOPE_CONTAINER
!== $scope = $definition->getScope()) {
129 $service->setAttribute('scope', $scope);
131 if (!$definition->isPublic()) {
132 $service->setAttribute('public', 'false');
134 if ($definition->isSynthetic()) {
135 $service->setAttribute('synthetic', 'true');
137 if ($definition->isSynchronized()) {
138 $service->setAttribute('synchronized', 'true');
140 if ($definition->isLazy()) {
141 $service->setAttribute('lazy', 'true');
144 foreach ($definition->getTags() as $name => $tags) {
145 foreach ($tags as $attributes) {
146 $tag = $this->document
->createElement('tag');
147 $tag->setAttribute('name', $name);
148 foreach ($attributes as $key => $value) {
149 $tag->setAttribute($key, $value);
151 $service->appendChild($tag);
155 if ($definition->getFile()) {
156 $file = $this->document
->createElement('file');
157 $file->appendChild($this->document
->createTextNode($definition->getFile()));
158 $service->appendChild($file);
161 if ($parameters = $definition->getArguments()) {
162 $this->convertParameters($parameters, 'argument', $service);
165 if ($parameters = $definition->getProperties()) {
166 $this->convertParameters($parameters, 'property', $service, 'name');
169 $this->addMethodCalls($definition->getMethodCalls(), $service);
171 if ($callable = $definition->getConfigurator()) {
172 $configurator = $this->document
->createElement('configurator');
173 if (is_array($callable)) {
174 $configurator->setAttribute($callable[0] instanceof Reference ?
'service' : 'class', $callable[0]);
175 $configurator->setAttribute('method', $callable[1]);
177 $configurator->setAttribute('function', $callable);
179 $service->appendChild($configurator);
182 $parent->appendChild($service);
186 * Adds a service alias.
188 * @param string $alias
190 * @param \DOMElement $parent
192 private function addServiceAlias($alias, Alias
$id, \DOMElement
$parent)
194 $service = $this->document
->createElement('service');
195 $service->setAttribute('id', $alias);
196 $service->setAttribute('alias', $id);
197 if (!$id->isPublic()) {
198 $service->setAttribute('public', 'false');
200 $parent->appendChild($service);
206 * @param \DOMElement $parent
208 private function addServices(\DOMElement
$parent)
210 $definitions = $this->container
->getDefinitions();
215 $services = $this->document
->createElement('services');
216 foreach ($definitions as $id => $definition) {
217 $this->addService($definition, $id, $services);
220 $aliases = $this->container
->getAliases();
221 foreach ($aliases as $alias => $id) {
222 while (isset($aliases[(string) $id])) {
223 $id = $aliases[(string) $id];
225 $this->addServiceAlias($alias, $id, $services);
227 $parent->appendChild($services);
231 * Converts parameters.
233 * @param array $parameters
234 * @param string $type
235 * @param \DOMElement $parent
236 * @param string $keyAttribute
238 private function convertParameters($parameters, $type, \DOMElement
$parent, $keyAttribute = 'key')
240 $withKeys = array_keys($parameters) !== range(0, count($parameters) - 1);
241 foreach ($parameters as $key => $value) {
242 $element = $this->document
->createElement($type);
244 $element->setAttribute($keyAttribute, $key);
247 if (is_array($value)) {
248 $element->setAttribute('type', 'collection');
249 $this->convertParameters($value, $type, $element, 'key');
250 } elseif ($value instanceof Reference
) {
251 $element->setAttribute('type', 'service');
252 $element->setAttribute('id', (string) $value);
253 $behaviour = $value->getInvalidBehavior();
254 if ($behaviour == ContainerInterface
::NULL_ON_INVALID_REFERENCE
) {
255 $element->setAttribute('on-invalid', 'null');
256 } elseif ($behaviour == ContainerInterface
::IGNORE_ON_INVALID_REFERENCE
) {
257 $element->setAttribute('on-invalid', 'ignore');
259 if (!$value->isStrict()) {
260 $element->setAttribute('strict', 'false');
262 } elseif ($value instanceof Definition
) {
263 $element->setAttribute('type', 'service');
264 $this->addService($value, null, $element);
266 if (in_array($value, array('null', 'true', 'false'), true)) {
267 $element->setAttribute('type', 'string');
269 $text = $this->document
->createTextNode(self
::phpToXml($value));
270 $element->appendChild($text);
272 $parent->appendChild($element);
279 * @param array $arguments
283 private function escape($arguments)
286 foreach ($arguments as $k => $v) {
288 $args[$k] = $this->escape($v);
289 } elseif (is_string($v)) {
290 $args[$k] = str_replace('%', '%%', $v);
300 * Converts php types to xml types.
302 * @param mixed $value Value to convert
306 * @throws RuntimeException When trying to dump object or resource
308 public static function phpToXml($value)
311 case null === $value:
313 case true === $value:
315 case false === $value:
317 case $value instanceof Parameter
:
318 return '%'.$value.'%';
319 case is_object($value) ||
is_resource($value):
320 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
322 return (string) $value;