|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\DependencyInjection\Dumper; |
|
|
13 |
|
|
|
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 |
|
|
|
19 |
/** |
|
|
20 |
* XmlDumper dumps a service container as an XML string. |
|
|
21 |
* |
|
|
22 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
23 |
* @author Martin HasoĊ <martin.hason@gmail.com> |
|
|
24 |
* |
|
|
25 |
* @api |
|
|
26 |
*/ |
|
|
27 |
class XmlDumper extends Dumper |
|
|
28 |
{ |
|
|
29 |
/** |
|
|
30 |
* @var \DOMDocument |
|
|
31 |
*/ |
|
|
32 |
private $document; |
|
|
33 |
|
|
|
34 |
/** |
|
|
35 |
* Dumps the service container as an XML string. |
|
|
36 |
* |
|
|
37 |
* @param array $options An array of options |
|
|
38 |
* |
|
|
39 |
* @return string An xml string representing of the service container |
|
|
40 |
* |
|
|
41 |
* @api |
|
|
42 |
*/ |
|
|
43 |
public function dump(array $options = array()) |
|
|
44 |
{ |
|
|
45 |
$this->document = new \DOMDocument('1.0', 'utf-8'); |
|
|
46 |
$this->document->formatOutput = true; |
|
|
47 |
|
|
|
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'); |
|
|
51 |
|
|
|
52 |
$this->addParameters($container); |
|
|
53 |
$this->addServices($container); |
|
|
54 |
|
|
|
55 |
$this->document->appendChild($container); |
|
|
56 |
$xml = $this->document->saveXML(); |
|
|
57 |
$this->document = null; |
|
|
58 |
|
|
|
59 |
return $xml; |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Adds parameters. |
|
|
64 |
* |
|
|
65 |
* @param DOMElement $parent |
|
|
66 |
* @return void |
|
|
67 |
*/ |
|
|
68 |
private function addParameters(\DOMElement $parent) |
|
|
69 |
{ |
|
|
70 |
$data = $this->container->getParameterBag()->all(); |
|
|
71 |
if (!$data) { |
|
|
72 |
return; |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
if ($this->container->isFrozen()) { |
|
|
76 |
$data = $this->escape($data); |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
$parameters = $this->document->createElement('parameters'); |
|
|
80 |
$parent->appendChild($parameters); |
|
|
81 |
$this->convertParameters($data, 'parameter', $parameters); |
|
|
82 |
} |
|
|
83 |
|
|
|
84 |
/** |
|
|
85 |
* Adds method calls. |
|
|
86 |
* |
|
|
87 |
* @param array $methodcalls |
|
|
88 |
* @param DOMElement $parent |
|
|
89 |
* @return void |
|
|
90 |
*/ |
|
|
91 |
private function addMethodCalls(array $methodcalls, \DOMElement $parent) |
|
|
92 |
{ |
|
|
93 |
foreach ($methodcalls as $methodcall) { |
|
|
94 |
$call = $this->document->createElement('call'); |
|
|
95 |
$call->setAttribute('method', $methodcall[0]); |
|
|
96 |
if (count($methodcall[1])) { |
|
|
97 |
$this->convertParameters($methodcall[1], 'argument', $call); |
|
|
98 |
} |
|
|
99 |
$parent->appendChild($call); |
|
|
100 |
} |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
/** |
|
|
104 |
* Adds a service. |
|
|
105 |
* |
|
|
106 |
* @param Definition $definition |
|
|
107 |
* @param string $id |
|
|
108 |
* @param DOMElement $parent |
|
|
109 |
* @return void |
|
|
110 |
*/ |
|
|
111 |
private function addService($definition, $id, \DOMElement $parent) |
|
|
112 |
{ |
|
|
113 |
$service = $this->document->createElement('service'); |
|
|
114 |
if (null !== $id) { |
|
|
115 |
$service->setAttribute('id', $id); |
|
|
116 |
} |
|
|
117 |
if ($definition->getClass()) { |
|
|
118 |
$service->setAttribute('class', $definition->getClass()); |
|
|
119 |
} |
|
|
120 |
if ($definition->getFactoryMethod()) { |
|
|
121 |
$service->setAttribute('factory-method', $definition->getFactoryMethod()); |
|
|
122 |
} |
|
|
123 |
if ($definition->getFactoryService()) { |
|
|
124 |
$service->setAttribute('factory-service', $definition->getFactoryService()); |
|
|
125 |
} |
|
|
126 |
if (ContainerInterface::SCOPE_CONTAINER !== $scope = $definition->getScope()) { |
|
|
127 |
$service->setAttribute('scope', $scope); |
|
|
128 |
} |
|
|
129 |
if (!$definition->isPublic()) { |
|
|
130 |
$service->setAttribute('public', 'false'); |
|
|
131 |
} |
|
|
132 |
|
|
|
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); |
|
|
139 |
} |
|
|
140 |
$service->appendChild($tag); |
|
|
141 |
} |
|
|
142 |
} |
|
|
143 |
|
|
|
144 |
if ($definition->getFile()) { |
|
|
145 |
$file = $this->document->createElement('file', $definition->getFile()); |
|
|
146 |
$service->appendChild($file); |
|
|
147 |
} |
|
|
148 |
|
|
|
149 |
if ($parameters = $definition->getArguments()) { |
|
|
150 |
$this->convertParameters($parameters, 'argument', $service); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
if ($parameters = $definition->getProperties()) { |
|
|
154 |
$this->convertParameters($parameters, 'property', $service, 'name'); |
|
|
155 |
} |
|
|
156 |
|
|
|
157 |
$this->addMethodCalls($definition->getMethodCalls(), $service); |
|
|
158 |
|
|
|
159 |
if ($callable = $definition->getConfigurator()) { |
|
|
160 |
$configurator = $this->document->createElement('configurator'); |
|
|
161 |
if (is_array($callable)) { |
|
|
162 |
$configurator->setAttribute((is_object($callable[0]) && $callable[0] instanceof Reference ? 'service' : 'class'), $callable[0]); |
|
|
163 |
$configurator->setAttribute('method', $callable[1]); |
|
|
164 |
} else { |
|
|
165 |
$configurator->setAttribute('function', $callable); |
|
|
166 |
} |
|
|
167 |
$service->appendChild($configurator); |
|
|
168 |
} |
|
|
169 |
|
|
|
170 |
$parent->appendChild($service); |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
/** |
|
|
174 |
* Adds a service alias. |
|
|
175 |
* |
|
|
176 |
* @param string $alias |
|
|
177 |
* @param string $id |
|
|
178 |
* @param DOMElement $parent |
|
|
179 |
* @return void |
|
|
180 |
*/ |
|
|
181 |
private function addServiceAlias($alias, $id, \DOMElement $parent) |
|
|
182 |
{ |
|
|
183 |
$service = $this->document->createElement('service'); |
|
|
184 |
$service->setAttribute('id', $alias); |
|
|
185 |
$service->setAttribute('alias', $id); |
|
|
186 |
if (!$id->isPublic()) { |
|
|
187 |
$service->setAttribute('public', 'false'); |
|
|
188 |
} |
|
|
189 |
$parent->appendChild($service); |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
/** |
|
|
193 |
* Adds services. |
|
|
194 |
* |
|
|
195 |
* @param DOMElement $parent |
|
|
196 |
* @return void |
|
|
197 |
*/ |
|
|
198 |
private function addServices(\DOMElement $parent) |
|
|
199 |
{ |
|
|
200 |
$definitions = $this->container->getDefinitions(); |
|
|
201 |
if (!$definitions) { |
|
|
202 |
return; |
|
|
203 |
} |
|
|
204 |
|
|
|
205 |
$services = $this->document->createElement('services'); |
|
|
206 |
foreach ($definitions as $id => $definition) { |
|
|
207 |
$this->addService($definition, $id, $services); |
|
|
208 |
} |
|
|
209 |
|
|
|
210 |
foreach ($this->container->getAliases() as $alias => $id) { |
|
|
211 |
$this->addServiceAlias($alias, $id, $services); |
|
|
212 |
} |
|
|
213 |
$parent->appendChild($services); |
|
|
214 |
} |
|
|
215 |
|
|
|
216 |
/** |
|
|
217 |
* Converts parameters. |
|
|
218 |
* |
|
|
219 |
* @param array $parameters |
|
|
220 |
* @param string $type |
|
|
221 |
* @param DOMElement $parent |
|
|
222 |
* @param string $keyAttribute |
|
|
223 |
* @return void |
|
|
224 |
*/ |
|
|
225 |
private function convertParameters($parameters, $type, \DOMElement $parent, $keyAttribute = 'key') |
|
|
226 |
{ |
|
|
227 |
$withKeys = array_keys($parameters) !== range(0, count($parameters) - 1); |
|
|
228 |
foreach ($parameters as $key => $value) { |
|
|
229 |
$element = $this->document->createElement($type); |
|
|
230 |
if ($withKeys) { |
|
|
231 |
$element->setAttribute($keyAttribute, $key); |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
if (is_array($value)) { |
|
|
235 |
$element->setAttribute('type', 'collection'); |
|
|
236 |
$this->convertParameters($value, $type, $element, 'key'); |
|
|
237 |
} else if (is_object($value) && $value instanceof Reference) { |
|
|
238 |
$element->setAttribute('type', 'service'); |
|
|
239 |
$element->setAttribute('id', (string) $value); |
|
|
240 |
$behaviour = $value->getInvalidBehavior(); |
|
|
241 |
if ($behaviour == ContainerInterface::NULL_ON_INVALID_REFERENCE) { |
|
|
242 |
$element->setAttribute('on-invalid', 'null'); |
|
|
243 |
} else if ($behaviour == ContainerInterface::IGNORE_ON_INVALID_REFERENCE) { |
|
|
244 |
$element->setAttribute('on-invalid', 'ignore'); |
|
|
245 |
} |
|
|
246 |
} else if (is_object($value) && $value instanceof Definition) { |
|
|
247 |
$element->setAttribute('type', 'service'); |
|
|
248 |
$this->addService($value, null, $element); |
|
|
249 |
} else { |
|
|
250 |
if (in_array($value, array('null', 'true', 'false'), true)) { |
|
|
251 |
$element->setAttribute('type', 'string'); |
|
|
252 |
} |
|
|
253 |
$text = $this->document->createTextNode(self::phpToXml($value)); |
|
|
254 |
$element->appendChild($text); |
|
|
255 |
} |
|
|
256 |
$parent->appendChild($element); |
|
|
257 |
} |
|
|
258 |
} |
|
|
259 |
|
|
|
260 |
/** |
|
|
261 |
* Escapes arguments |
|
|
262 |
* |
|
|
263 |
* @param array $arguments |
|
|
264 |
* @return array |
|
|
265 |
*/ |
|
|
266 |
private function escape($arguments) |
|
|
267 |
{ |
|
|
268 |
$args = array(); |
|
|
269 |
foreach ($arguments as $k => $v) { |
|
|
270 |
if (is_array($v)) { |
|
|
271 |
$args[$k] = $this->escape($v); |
|
|
272 |
} elseif (is_string($v)) { |
|
|
273 |
$args[$k] = str_replace('%', '%%', $v); |
|
|
274 |
} else { |
|
|
275 |
$args[$k] = $v; |
|
|
276 |
} |
|
|
277 |
} |
|
|
278 |
|
|
|
279 |
return $args; |
|
|
280 |
} |
|
|
281 |
|
|
|
282 |
/** |
|
|
283 |
* Converts php types to xml types. |
|
|
284 |
* |
|
|
285 |
* @param mixed $value Value to convert |
|
|
286 |
* @throws \RuntimeException When trying to dump object or resource |
|
|
287 |
*/ |
|
|
288 |
static public function phpToXml($value) |
|
|
289 |
{ |
|
|
290 |
switch (true) { |
|
|
291 |
case null === $value: |
|
|
292 |
return 'null'; |
|
|
293 |
case true === $value: |
|
|
294 |
return 'true'; |
|
|
295 |
case false === $value: |
|
|
296 |
return 'false'; |
|
|
297 |
case is_object($value) && $value instanceof Parameter: |
|
|
298 |
return '%'.$value.'%'; |
|
|
299 |
case is_object($value) || is_resource($value): |
|
|
300 |
throw new \RuntimeException('Unable to dump a service container if a parameter is an object or a resource.'); |
|
|
301 |
default: |
|
|
302 |
return (string) $value; |
|
|
303 |
} |
|
|
304 |
} |
|
|
305 |
} |