Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\DependencyInjection\Dumper;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\DependencyInjection\Alias;
|
Chris@14
|
15 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
|
Chris@14
|
16 use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
|
Chris@14
|
17 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@17
|
19 use Symfony\Component\DependencyInjection\Definition;
|
Chris@17
|
20 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
21 use Symfony\Component\DependencyInjection\Parameter;
|
Chris@0
|
22 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
23 use Symfony\Component\ExpressionLanguage\Expression;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * XmlDumper dumps a service container as an XML string.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
29 * @author Martin HasoĊ <martin.hason@gmail.com>
|
Chris@0
|
30 */
|
Chris@0
|
31 class XmlDumper extends Dumper
|
Chris@0
|
32 {
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var \DOMDocument
|
Chris@0
|
35 */
|
Chris@0
|
36 private $document;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Dumps the service container as an XML string.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @return string An xml string representing of the service container
|
Chris@0
|
42 */
|
Chris@17
|
43 public function dump(array $options = [])
|
Chris@0
|
44 {
|
Chris@0
|
45 $this->document = new \DOMDocument('1.0', 'utf-8');
|
Chris@0
|
46 $this->document->formatOutput = true;
|
Chris@0
|
47
|
Chris@0
|
48 $container = $this->document->createElementNS('http://symfony.com/schema/dic/services', 'container');
|
Chris@0
|
49 $container->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
|
Chris@18
|
50 $container->setAttribute('xsi:schemaLocation', 'http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd');
|
Chris@0
|
51
|
Chris@0
|
52 $this->addParameters($container);
|
Chris@0
|
53 $this->addServices($container);
|
Chris@0
|
54
|
Chris@0
|
55 $this->document->appendChild($container);
|
Chris@0
|
56 $xml = $this->document->saveXML();
|
Chris@0
|
57 $this->document = null;
|
Chris@0
|
58
|
Chris@0
|
59 return $this->container->resolveEnvPlaceholders($xml);
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 private function addParameters(\DOMElement $parent)
|
Chris@0
|
63 {
|
Chris@0
|
64 $data = $this->container->getParameterBag()->all();
|
Chris@0
|
65 if (!$data) {
|
Chris@0
|
66 return;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@14
|
69 if ($this->container->isCompiled()) {
|
Chris@0
|
70 $data = $this->escape($data);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 $parameters = $this->document->createElement('parameters');
|
Chris@0
|
74 $parent->appendChild($parameters);
|
Chris@0
|
75 $this->convertParameters($data, 'parameter', $parameters);
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 private function addMethodCalls(array $methodcalls, \DOMElement $parent)
|
Chris@0
|
79 {
|
Chris@0
|
80 foreach ($methodcalls as $methodcall) {
|
Chris@0
|
81 $call = $this->document->createElement('call');
|
Chris@0
|
82 $call->setAttribute('method', $methodcall[0]);
|
Chris@17
|
83 if (\count($methodcall[1])) {
|
Chris@0
|
84 $this->convertParameters($methodcall[1], 'argument', $call);
|
Chris@0
|
85 }
|
Chris@0
|
86 $parent->appendChild($call);
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Adds a service.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @param Definition $definition
|
Chris@0
|
94 * @param string $id
|
Chris@0
|
95 * @param \DOMElement $parent
|
Chris@0
|
96 */
|
Chris@0
|
97 private function addService($definition, $id, \DOMElement $parent)
|
Chris@0
|
98 {
|
Chris@0
|
99 $service = $this->document->createElement('service');
|
Chris@0
|
100 if (null !== $id) {
|
Chris@0
|
101 $service->setAttribute('id', $id);
|
Chris@0
|
102 }
|
Chris@0
|
103 if ($class = $definition->getClass()) {
|
Chris@0
|
104 if ('\\' === substr($class, 0, 1)) {
|
Chris@0
|
105 $class = substr($class, 1);
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 $service->setAttribute('class', $class);
|
Chris@0
|
109 }
|
Chris@0
|
110 if (!$definition->isShared()) {
|
Chris@0
|
111 $service->setAttribute('shared', 'false');
|
Chris@0
|
112 }
|
Chris@14
|
113 if (!$definition->isPrivate()) {
|
Chris@14
|
114 $service->setAttribute('public', $definition->isPublic() ? 'true' : 'false');
|
Chris@0
|
115 }
|
Chris@0
|
116 if ($definition->isSynthetic()) {
|
Chris@0
|
117 $service->setAttribute('synthetic', 'true');
|
Chris@0
|
118 }
|
Chris@0
|
119 if ($definition->isLazy()) {
|
Chris@0
|
120 $service->setAttribute('lazy', 'true');
|
Chris@0
|
121 }
|
Chris@0
|
122 if (null !== $decorated = $definition->getDecoratedService()) {
|
Chris@0
|
123 list($decorated, $renamedId, $priority) = $decorated;
|
Chris@0
|
124 $service->setAttribute('decorates', $decorated);
|
Chris@0
|
125 if (null !== $renamedId) {
|
Chris@0
|
126 $service->setAttribute('decoration-inner-name', $renamedId);
|
Chris@0
|
127 }
|
Chris@0
|
128 if (0 !== $priority) {
|
Chris@0
|
129 $service->setAttribute('decoration-priority', $priority);
|
Chris@0
|
130 }
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 foreach ($definition->getTags() as $name => $tags) {
|
Chris@0
|
134 foreach ($tags as $attributes) {
|
Chris@0
|
135 $tag = $this->document->createElement('tag');
|
Chris@0
|
136 $tag->setAttribute('name', $name);
|
Chris@0
|
137 foreach ($attributes as $key => $value) {
|
Chris@0
|
138 $tag->setAttribute($key, $value);
|
Chris@0
|
139 }
|
Chris@0
|
140 $service->appendChild($tag);
|
Chris@0
|
141 }
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 if ($definition->getFile()) {
|
Chris@0
|
145 $file = $this->document->createElement('file');
|
Chris@0
|
146 $file->appendChild($this->document->createTextNode($definition->getFile()));
|
Chris@0
|
147 $service->appendChild($file);
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 if ($parameters = $definition->getArguments()) {
|
Chris@0
|
151 $this->convertParameters($parameters, 'argument', $service);
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 if ($parameters = $definition->getProperties()) {
|
Chris@0
|
155 $this->convertParameters($parameters, 'property', $service, 'name');
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 $this->addMethodCalls($definition->getMethodCalls(), $service);
|
Chris@0
|
159
|
Chris@0
|
160 if ($callable = $definition->getFactory()) {
|
Chris@0
|
161 $factory = $this->document->createElement('factory');
|
Chris@0
|
162
|
Chris@17
|
163 if (\is_array($callable) && $callable[0] instanceof Definition) {
|
Chris@0
|
164 $this->addService($callable[0], null, $factory);
|
Chris@0
|
165 $factory->setAttribute('method', $callable[1]);
|
Chris@17
|
166 } elseif (\is_array($callable)) {
|
Chris@14
|
167 if (null !== $callable[0]) {
|
Chris@14
|
168 $factory->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
|
Chris@14
|
169 }
|
Chris@0
|
170 $factory->setAttribute('method', $callable[1]);
|
Chris@0
|
171 } else {
|
Chris@0
|
172 $factory->setAttribute('function', $callable);
|
Chris@0
|
173 }
|
Chris@0
|
174 $service->appendChild($factory);
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 if ($definition->isDeprecated()) {
|
Chris@0
|
178 $deprecated = $this->document->createElement('deprecated');
|
Chris@0
|
179 $deprecated->appendChild($this->document->createTextNode($definition->getDeprecationMessage('%service_id%')));
|
Chris@0
|
180
|
Chris@0
|
181 $service->appendChild($deprecated);
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 if ($definition->isAutowired()) {
|
Chris@0
|
185 $service->setAttribute('autowire', 'true');
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@14
|
188 foreach ($definition->getAutowiringTypes(false) as $autowiringTypeValue) {
|
Chris@0
|
189 $autowiringType = $this->document->createElement('autowiring-type');
|
Chris@0
|
190 $autowiringType->appendChild($this->document->createTextNode($autowiringTypeValue));
|
Chris@0
|
191
|
Chris@0
|
192 $service->appendChild($autowiringType);
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@14
|
195 if ($definition->isAutoconfigured()) {
|
Chris@14
|
196 $service->setAttribute('autoconfigure', 'true');
|
Chris@14
|
197 }
|
Chris@14
|
198
|
Chris@12
|
199 if ($definition->isAbstract()) {
|
Chris@12
|
200 $service->setAttribute('abstract', 'true');
|
Chris@12
|
201 }
|
Chris@12
|
202
|
Chris@0
|
203 if ($callable = $definition->getConfigurator()) {
|
Chris@0
|
204 $configurator = $this->document->createElement('configurator');
|
Chris@0
|
205
|
Chris@17
|
206 if (\is_array($callable) && $callable[0] instanceof Definition) {
|
Chris@0
|
207 $this->addService($callable[0], null, $configurator);
|
Chris@0
|
208 $configurator->setAttribute('method', $callable[1]);
|
Chris@17
|
209 } elseif (\is_array($callable)) {
|
Chris@0
|
210 $configurator->setAttribute($callable[0] instanceof Reference ? 'service' : 'class', $callable[0]);
|
Chris@0
|
211 $configurator->setAttribute('method', $callable[1]);
|
Chris@0
|
212 } else {
|
Chris@0
|
213 $configurator->setAttribute('function', $callable);
|
Chris@0
|
214 }
|
Chris@0
|
215 $service->appendChild($configurator);
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 $parent->appendChild($service);
|
Chris@0
|
219 }
|
Chris@0
|
220
|
Chris@0
|
221 /**
|
Chris@0
|
222 * Adds a service alias.
|
Chris@0
|
223 *
|
Chris@0
|
224 * @param string $alias
|
Chris@0
|
225 * @param Alias $id
|
Chris@0
|
226 * @param \DOMElement $parent
|
Chris@0
|
227 */
|
Chris@0
|
228 private function addServiceAlias($alias, Alias $id, \DOMElement $parent)
|
Chris@0
|
229 {
|
Chris@0
|
230 $service = $this->document->createElement('service');
|
Chris@0
|
231 $service->setAttribute('id', $alias);
|
Chris@0
|
232 $service->setAttribute('alias', $id);
|
Chris@14
|
233 if (!$id->isPrivate()) {
|
Chris@14
|
234 $service->setAttribute('public', $id->isPublic() ? 'true' : 'false');
|
Chris@0
|
235 }
|
Chris@0
|
236 $parent->appendChild($service);
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 private function addServices(\DOMElement $parent)
|
Chris@0
|
240 {
|
Chris@0
|
241 $definitions = $this->container->getDefinitions();
|
Chris@0
|
242 if (!$definitions) {
|
Chris@0
|
243 return;
|
Chris@0
|
244 }
|
Chris@0
|
245
|
Chris@0
|
246 $services = $this->document->createElement('services');
|
Chris@0
|
247 foreach ($definitions as $id => $definition) {
|
Chris@0
|
248 $this->addService($definition, $id, $services);
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 $aliases = $this->container->getAliases();
|
Chris@0
|
252 foreach ($aliases as $alias => $id) {
|
Chris@0
|
253 while (isset($aliases[(string) $id])) {
|
Chris@0
|
254 $id = $aliases[(string) $id];
|
Chris@0
|
255 }
|
Chris@0
|
256 $this->addServiceAlias($alias, $id, $services);
|
Chris@0
|
257 }
|
Chris@0
|
258 $parent->appendChild($services);
|
Chris@0
|
259 }
|
Chris@0
|
260
|
Chris@0
|
261 /**
|
Chris@0
|
262 * Converts parameters.
|
Chris@0
|
263 *
|
Chris@0
|
264 * @param array $parameters
|
Chris@0
|
265 * @param string $type
|
Chris@0
|
266 * @param \DOMElement $parent
|
Chris@0
|
267 * @param string $keyAttribute
|
Chris@0
|
268 */
|
Chris@0
|
269 private function convertParameters(array $parameters, $type, \DOMElement $parent, $keyAttribute = 'key')
|
Chris@0
|
270 {
|
Chris@17
|
271 $withKeys = array_keys($parameters) !== range(0, \count($parameters) - 1);
|
Chris@0
|
272 foreach ($parameters as $key => $value) {
|
Chris@0
|
273 $element = $this->document->createElement($type);
|
Chris@0
|
274 if ($withKeys) {
|
Chris@0
|
275 $element->setAttribute($keyAttribute, $key);
|
Chris@0
|
276 }
|
Chris@0
|
277
|
Chris@14
|
278 if ($value instanceof ServiceClosureArgument) {
|
Chris@14
|
279 $value = $value->getValues()[0];
|
Chris@14
|
280 }
|
Chris@17
|
281 if (\is_array($value)) {
|
Chris@0
|
282 $element->setAttribute('type', 'collection');
|
Chris@0
|
283 $this->convertParameters($value, $type, $element, 'key');
|
Chris@14
|
284 } elseif ($value instanceof TaggedIteratorArgument) {
|
Chris@14
|
285 $element->setAttribute('type', 'tagged');
|
Chris@14
|
286 $element->setAttribute('tag', $value->getTag());
|
Chris@14
|
287 } elseif ($value instanceof IteratorArgument) {
|
Chris@14
|
288 $element->setAttribute('type', 'iterator');
|
Chris@14
|
289 $this->convertParameters($value->getValues(), $type, $element, 'key');
|
Chris@0
|
290 } elseif ($value instanceof Reference) {
|
Chris@0
|
291 $element->setAttribute('type', 'service');
|
Chris@0
|
292 $element->setAttribute('id', (string) $value);
|
Chris@18
|
293 $behavior = $value->getInvalidBehavior();
|
Chris@18
|
294 if (ContainerInterface::NULL_ON_INVALID_REFERENCE == $behavior) {
|
Chris@0
|
295 $element->setAttribute('on-invalid', 'null');
|
Chris@18
|
296 } elseif (ContainerInterface::IGNORE_ON_INVALID_REFERENCE == $behavior) {
|
Chris@0
|
297 $element->setAttribute('on-invalid', 'ignore');
|
Chris@18
|
298 } elseif (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE == $behavior) {
|
Chris@14
|
299 $element->setAttribute('on-invalid', 'ignore_uninitialized');
|
Chris@0
|
300 }
|
Chris@0
|
301 } elseif ($value instanceof Definition) {
|
Chris@0
|
302 $element->setAttribute('type', 'service');
|
Chris@0
|
303 $this->addService($value, null, $element);
|
Chris@0
|
304 } elseif ($value instanceof Expression) {
|
Chris@0
|
305 $element->setAttribute('type', 'expression');
|
Chris@0
|
306 $text = $this->document->createTextNode(self::phpToXml((string) $value));
|
Chris@0
|
307 $element->appendChild($text);
|
Chris@0
|
308 } else {
|
Chris@17
|
309 if (\in_array($value, ['null', 'true', 'false'], true)) {
|
Chris@0
|
310 $element->setAttribute('type', 'string');
|
Chris@0
|
311 }
|
Chris@0
|
312 $text = $this->document->createTextNode(self::phpToXml($value));
|
Chris@0
|
313 $element->appendChild($text);
|
Chris@0
|
314 }
|
Chris@0
|
315 $parent->appendChild($element);
|
Chris@0
|
316 }
|
Chris@0
|
317 }
|
Chris@0
|
318
|
Chris@0
|
319 /**
|
Chris@0
|
320 * Escapes arguments.
|
Chris@0
|
321 *
|
Chris@0
|
322 * @return array
|
Chris@0
|
323 */
|
Chris@0
|
324 private function escape(array $arguments)
|
Chris@0
|
325 {
|
Chris@17
|
326 $args = [];
|
Chris@0
|
327 foreach ($arguments as $k => $v) {
|
Chris@17
|
328 if (\is_array($v)) {
|
Chris@0
|
329 $args[$k] = $this->escape($v);
|
Chris@17
|
330 } elseif (\is_string($v)) {
|
Chris@0
|
331 $args[$k] = str_replace('%', '%%', $v);
|
Chris@0
|
332 } else {
|
Chris@0
|
333 $args[$k] = $v;
|
Chris@0
|
334 }
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 return $args;
|
Chris@0
|
338 }
|
Chris@0
|
339
|
Chris@0
|
340 /**
|
Chris@0
|
341 * Converts php types to xml types.
|
Chris@0
|
342 *
|
Chris@0
|
343 * @param mixed $value Value to convert
|
Chris@0
|
344 *
|
Chris@0
|
345 * @return string
|
Chris@0
|
346 *
|
Chris@0
|
347 * @throws RuntimeException When trying to dump object or resource
|
Chris@0
|
348 */
|
Chris@0
|
349 public static function phpToXml($value)
|
Chris@0
|
350 {
|
Chris@0
|
351 switch (true) {
|
Chris@0
|
352 case null === $value:
|
Chris@0
|
353 return 'null';
|
Chris@0
|
354 case true === $value:
|
Chris@0
|
355 return 'true';
|
Chris@0
|
356 case false === $value:
|
Chris@0
|
357 return 'false';
|
Chris@0
|
358 case $value instanceof Parameter:
|
Chris@0
|
359 return '%'.$value.'%';
|
Chris@17
|
360 case \is_object($value) || \is_resource($value):
|
Chris@0
|
361 throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
|
Chris@0
|
362 default:
|
Chris@0
|
363 return (string) $value;
|
Chris@0
|
364 }
|
Chris@0
|
365 }
|
Chris@0
|
366 }
|