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\Loader;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Config\Resource\FileResource;
|
Chris@0
|
15 use Symfony\Component\Config\Util\XmlUtils;
|
Chris@0
|
16 use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
Chris@0
|
17 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
18 use Symfony\Component\DependencyInjection\Alias;
|
Chris@0
|
19 use Symfony\Component\DependencyInjection\Definition;
|
Chris@0
|
20 use Symfony\Component\DependencyInjection\Reference;
|
Chris@0
|
21 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
22 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
23 use Symfony\Component\ExpressionLanguage\Expression;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * XmlFileLoader loads XML files service definitions.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
29 */
|
Chris@0
|
30 class XmlFileLoader extends FileLoader
|
Chris@0
|
31 {
|
Chris@0
|
32 const NS = 'http://symfony.com/schema/dic/services';
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * {@inheritdoc}
|
Chris@0
|
36 */
|
Chris@0
|
37 public function load($resource, $type = null)
|
Chris@0
|
38 {
|
Chris@0
|
39 $path = $this->locator->locate($resource);
|
Chris@0
|
40
|
Chris@0
|
41 $xml = $this->parseFileToDOM($path);
|
Chris@0
|
42
|
Chris@0
|
43 $this->container->addResource(new FileResource($path));
|
Chris@0
|
44
|
Chris@0
|
45 // anonymous services
|
Chris@0
|
46 $this->processAnonymousServices($xml, $path);
|
Chris@0
|
47
|
Chris@0
|
48 // imports
|
Chris@0
|
49 $this->parseImports($xml, $path);
|
Chris@0
|
50
|
Chris@0
|
51 // parameters
|
Chris@0
|
52 $this->parseParameters($xml);
|
Chris@0
|
53
|
Chris@0
|
54 // extensions
|
Chris@0
|
55 $this->loadFromExtensions($xml);
|
Chris@0
|
56
|
Chris@0
|
57 // services
|
Chris@0
|
58 $this->parseDefinitions($xml, $path);
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function supports($resource, $type = null)
|
Chris@0
|
65 {
|
Chris@0
|
66 return is_string($resource) && 'xml' === pathinfo($resource, PATHINFO_EXTENSION);
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Parses parameters.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @param \DOMDocument $xml
|
Chris@0
|
73 */
|
Chris@0
|
74 private function parseParameters(\DOMDocument $xml)
|
Chris@0
|
75 {
|
Chris@0
|
76 if ($parameters = $this->getChildren($xml->documentElement, 'parameters')) {
|
Chris@0
|
77 $this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter'));
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Parses imports.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param \DOMDocument $xml
|
Chris@0
|
85 * @param string $file
|
Chris@0
|
86 */
|
Chris@0
|
87 private function parseImports(\DOMDocument $xml, $file)
|
Chris@0
|
88 {
|
Chris@0
|
89 $xpath = new \DOMXPath($xml);
|
Chris@0
|
90 $xpath->registerNamespace('container', self::NS);
|
Chris@0
|
91
|
Chris@0
|
92 if (false === $imports = $xpath->query('//container:imports/container:import')) {
|
Chris@0
|
93 return;
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 $defaultDirectory = dirname($file);
|
Chris@0
|
97 foreach ($imports as $import) {
|
Chris@0
|
98 $this->setCurrentDir($defaultDirectory);
|
Chris@0
|
99 $this->import($import->getAttribute('resource'), null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Parses multiple definitions.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @param \DOMDocument $xml
|
Chris@0
|
107 * @param string $file
|
Chris@0
|
108 */
|
Chris@0
|
109 private function parseDefinitions(\DOMDocument $xml, $file)
|
Chris@0
|
110 {
|
Chris@0
|
111 $xpath = new \DOMXPath($xml);
|
Chris@0
|
112 $xpath->registerNamespace('container', self::NS);
|
Chris@0
|
113
|
Chris@0
|
114 if (false === $services = $xpath->query('//container:services/container:service')) {
|
Chris@0
|
115 return;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 foreach ($services as $service) {
|
Chris@0
|
119 if (null !== $definition = $this->parseDefinition($service, $file)) {
|
Chris@0
|
120 $this->container->setDefinition((string) $service->getAttribute('id'), $definition);
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 /**
|
Chris@0
|
126 * Parses an individual Definition.
|
Chris@0
|
127 *
|
Chris@0
|
128 * @param \DOMElement $service
|
Chris@0
|
129 * @param string $file
|
Chris@0
|
130 *
|
Chris@0
|
131 * @return Definition|null
|
Chris@0
|
132 */
|
Chris@0
|
133 private function parseDefinition(\DOMElement $service, $file)
|
Chris@0
|
134 {
|
Chris@0
|
135 if ($alias = $service->getAttribute('alias')) {
|
Chris@0
|
136 $this->validateAlias($service, $file);
|
Chris@0
|
137
|
Chris@0
|
138 $public = true;
|
Chris@0
|
139 if ($publicAttr = $service->getAttribute('public')) {
|
Chris@0
|
140 $public = XmlUtils::phpize($publicAttr);
|
Chris@0
|
141 }
|
Chris@0
|
142 $this->container->setAlias((string) $service->getAttribute('id'), new Alias($alias, $public));
|
Chris@0
|
143
|
Chris@0
|
144 return;
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 if ($parent = $service->getAttribute('parent')) {
|
Chris@0
|
148 $definition = new DefinitionDecorator($parent);
|
Chris@0
|
149 } else {
|
Chris@0
|
150 $definition = new Definition();
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 foreach (array('class', 'shared', 'public', 'synthetic', 'lazy', 'abstract') as $key) {
|
Chris@0
|
154 if ($value = $service->getAttribute($key)) {
|
Chris@0
|
155 $method = 'set'.$key;
|
Chris@0
|
156 $definition->$method(XmlUtils::phpize($value));
|
Chris@0
|
157 }
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 if ($value = $service->getAttribute('autowire')) {
|
Chris@0
|
161 $definition->setAutowired(XmlUtils::phpize($value));
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 if ($files = $this->getChildren($service, 'file')) {
|
Chris@0
|
165 $definition->setFile($files[0]->nodeValue);
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 if ($deprecated = $this->getChildren($service, 'deprecated')) {
|
Chris@0
|
169 $definition->setDeprecated(true, $deprecated[0]->nodeValue ?: null);
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 $definition->setArguments($this->getArgumentsAsPhp($service, 'argument'));
|
Chris@0
|
173 $definition->setProperties($this->getArgumentsAsPhp($service, 'property'));
|
Chris@0
|
174
|
Chris@0
|
175 if ($factories = $this->getChildren($service, 'factory')) {
|
Chris@0
|
176 $factory = $factories[0];
|
Chris@0
|
177 if ($function = $factory->getAttribute('function')) {
|
Chris@0
|
178 $definition->setFactory($function);
|
Chris@0
|
179 } else {
|
Chris@0
|
180 $factoryService = $this->getChildren($factory, 'service');
|
Chris@0
|
181
|
Chris@0
|
182 if (isset($factoryService[0])) {
|
Chris@0
|
183 $class = $this->parseDefinition($factoryService[0], $file);
|
Chris@0
|
184 } elseif ($childService = $factory->getAttribute('service')) {
|
Chris@0
|
185 $class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
|
Chris@0
|
186 } else {
|
Chris@0
|
187 $class = $factory->getAttribute('class');
|
Chris@0
|
188 }
|
Chris@0
|
189
|
Chris@0
|
190 $definition->setFactory(array($class, $factory->getAttribute('method')));
|
Chris@0
|
191 }
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 if ($configurators = $this->getChildren($service, 'configurator')) {
|
Chris@0
|
195 $configurator = $configurators[0];
|
Chris@0
|
196 if ($function = $configurator->getAttribute('function')) {
|
Chris@0
|
197 $definition->setConfigurator($function);
|
Chris@0
|
198 } else {
|
Chris@0
|
199 $configuratorService = $this->getChildren($configurator, 'service');
|
Chris@0
|
200
|
Chris@0
|
201 if (isset($configuratorService[0])) {
|
Chris@0
|
202 $class = $this->parseDefinition($configuratorService[0], $file);
|
Chris@0
|
203 } elseif ($childService = $configurator->getAttribute('service')) {
|
Chris@0
|
204 $class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
|
Chris@0
|
205 } else {
|
Chris@0
|
206 $class = $configurator->getAttribute('class');
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 $definition->setConfigurator(array($class, $configurator->getAttribute('method')));
|
Chris@0
|
210 }
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 foreach ($this->getChildren($service, 'call') as $call) {
|
Chris@0
|
214 $definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument'));
|
Chris@0
|
215 }
|
Chris@0
|
216
|
Chris@0
|
217 foreach ($this->getChildren($service, 'tag') as $tag) {
|
Chris@0
|
218 $parameters = array();
|
Chris@0
|
219 foreach ($tag->attributes as $name => $node) {
|
Chris@0
|
220 if ('name' === $name) {
|
Chris@0
|
221 continue;
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 if (false !== strpos($name, '-') && false === strpos($name, '_') && !array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
|
Chris@0
|
225 $parameters[$normalizedName] = XmlUtils::phpize($node->nodeValue);
|
Chris@0
|
226 }
|
Chris@0
|
227 // keep not normalized key
|
Chris@0
|
228 $parameters[$name] = XmlUtils::phpize($node->nodeValue);
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 if ('' === $tag->getAttribute('name')) {
|
Chris@0
|
232 throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', (string) $service->getAttribute('id'), $file));
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 $definition->addTag($tag->getAttribute('name'), $parameters);
|
Chris@0
|
236 }
|
Chris@0
|
237
|
Chris@0
|
238 foreach ($this->getChildren($service, 'autowiring-type') as $type) {
|
Chris@0
|
239 $definition->addAutowiringType($type->textContent);
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 if ($value = $service->getAttribute('decorates')) {
|
Chris@0
|
243 $renameId = $service->hasAttribute('decoration-inner-name') ? $service->getAttribute('decoration-inner-name') : null;
|
Chris@0
|
244 $priority = $service->hasAttribute('decoration-priority') ? $service->getAttribute('decoration-priority') : 0;
|
Chris@0
|
245 $definition->setDecoratedService($value, $renameId, $priority);
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 return $definition;
|
Chris@0
|
249 }
|
Chris@0
|
250
|
Chris@0
|
251 /**
|
Chris@0
|
252 * Parses a XML file to a \DOMDocument.
|
Chris@0
|
253 *
|
Chris@0
|
254 * @param string $file Path to a file
|
Chris@0
|
255 *
|
Chris@0
|
256 * @return \DOMDocument
|
Chris@0
|
257 *
|
Chris@0
|
258 * @throws InvalidArgumentException When loading of XML file returns error
|
Chris@0
|
259 */
|
Chris@0
|
260 private function parseFileToDOM($file)
|
Chris@0
|
261 {
|
Chris@0
|
262 try {
|
Chris@0
|
263 $dom = XmlUtils::loadFile($file, array($this, 'validateSchema'));
|
Chris@0
|
264 } catch (\InvalidArgumentException $e) {
|
Chris@0
|
265 throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e);
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 $this->validateExtensions($dom, $file);
|
Chris@0
|
269
|
Chris@0
|
270 return $dom;
|
Chris@0
|
271 }
|
Chris@0
|
272
|
Chris@0
|
273 /**
|
Chris@0
|
274 * Processes anonymous services.
|
Chris@0
|
275 *
|
Chris@0
|
276 * @param \DOMDocument $xml
|
Chris@0
|
277 * @param string $file
|
Chris@0
|
278 */
|
Chris@0
|
279 private function processAnonymousServices(\DOMDocument $xml, $file)
|
Chris@0
|
280 {
|
Chris@0
|
281 $definitions = array();
|
Chris@0
|
282 $count = 0;
|
Chris@0
|
283
|
Chris@0
|
284 $xpath = new \DOMXPath($xml);
|
Chris@0
|
285 $xpath->registerNamespace('container', self::NS);
|
Chris@0
|
286
|
Chris@0
|
287 // anonymous services as arguments/properties
|
Chris@0
|
288 if (false !== $nodes = $xpath->query('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]')) {
|
Chris@0
|
289 foreach ($nodes as $node) {
|
Chris@0
|
290 // give it a unique name
|
Chris@0
|
291 $id = sprintf('%s_%d', hash('sha256', $file), ++$count);
|
Chris@0
|
292 $node->setAttribute('id', $id);
|
Chris@0
|
293
|
Chris@0
|
294 if ($services = $this->getChildren($node, 'service')) {
|
Chris@0
|
295 $definitions[$id] = array($services[0], $file, false);
|
Chris@0
|
296 $services[0]->setAttribute('id', $id);
|
Chris@0
|
297
|
Chris@0
|
298 // anonymous services are always private
|
Chris@0
|
299 // we could not use the constant false here, because of XML parsing
|
Chris@0
|
300 $services[0]->setAttribute('public', 'false');
|
Chris@0
|
301 }
|
Chris@0
|
302 }
|
Chris@0
|
303 }
|
Chris@0
|
304
|
Chris@0
|
305 // anonymous services "in the wild"
|
Chris@0
|
306 if (false !== $nodes = $xpath->query('//container:services/container:service[not(@id)]')) {
|
Chris@0
|
307 foreach ($nodes as $node) {
|
Chris@0
|
308 // give it a unique name
|
Chris@0
|
309 $id = sprintf('%s_%d', hash('sha256', $file), ++$count);
|
Chris@0
|
310 $node->setAttribute('id', $id);
|
Chris@0
|
311 $definitions[$id] = array($node, $file, true);
|
Chris@0
|
312 }
|
Chris@0
|
313 }
|
Chris@0
|
314
|
Chris@0
|
315 // resolve definitions
|
Chris@0
|
316 krsort($definitions);
|
Chris@0
|
317 foreach ($definitions as $id => list($domElement, $file, $wild)) {
|
Chris@0
|
318 if (null !== $definition = $this->parseDefinition($domElement, $file)) {
|
Chris@0
|
319 $this->container->setDefinition($id, $definition);
|
Chris@0
|
320 }
|
Chris@0
|
321
|
Chris@0
|
322 if (true === $wild) {
|
Chris@0
|
323 $tmpDomElement = new \DOMElement('_services', null, self::NS);
|
Chris@0
|
324 $domElement->parentNode->replaceChild($tmpDomElement, $domElement);
|
Chris@0
|
325 $tmpDomElement->setAttribute('id', $id);
|
Chris@0
|
326 } else {
|
Chris@0
|
327 $domElement->parentNode->removeChild($domElement);
|
Chris@0
|
328 }
|
Chris@0
|
329 }
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 /**
|
Chris@0
|
333 * Returns arguments as valid php types.
|
Chris@0
|
334 *
|
Chris@0
|
335 * @param \DOMElement $node
|
Chris@0
|
336 * @param string $name
|
Chris@0
|
337 * @param bool $lowercase
|
Chris@0
|
338 *
|
Chris@0
|
339 * @return mixed
|
Chris@0
|
340 */
|
Chris@0
|
341 private function getArgumentsAsPhp(\DOMElement $node, $name, $lowercase = true)
|
Chris@0
|
342 {
|
Chris@0
|
343 $arguments = array();
|
Chris@0
|
344 foreach ($this->getChildren($node, $name) as $arg) {
|
Chris@0
|
345 if ($arg->hasAttribute('name')) {
|
Chris@0
|
346 $arg->setAttribute('key', $arg->getAttribute('name'));
|
Chris@0
|
347 }
|
Chris@0
|
348
|
Chris@0
|
349 // this is used by DefinitionDecorator to overwrite a specific
|
Chris@0
|
350 // argument of the parent definition
|
Chris@0
|
351 if ($arg->hasAttribute('index')) {
|
Chris@0
|
352 $key = 'index_'.$arg->getAttribute('index');
|
Chris@0
|
353 } elseif (!$arg->hasAttribute('key')) {
|
Chris@0
|
354 // Append an empty argument, then fetch its key to overwrite it later
|
Chris@0
|
355 $arguments[] = null;
|
Chris@0
|
356 $keys = array_keys($arguments);
|
Chris@0
|
357 $key = array_pop($keys);
|
Chris@0
|
358 } else {
|
Chris@0
|
359 $key = $arg->getAttribute('key');
|
Chris@0
|
360
|
Chris@0
|
361 // parameter keys are case insensitive
|
Chris@0
|
362 if ('parameter' == $name && $lowercase) {
|
Chris@0
|
363 $key = strtolower($key);
|
Chris@0
|
364 }
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 switch ($arg->getAttribute('type')) {
|
Chris@0
|
368 case 'service':
|
Chris@0
|
369 $onInvalid = $arg->getAttribute('on-invalid');
|
Chris@0
|
370 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
Chris@0
|
371 if ('ignore' == $onInvalid) {
|
Chris@0
|
372 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
|
Chris@0
|
373 } elseif ('null' == $onInvalid) {
|
Chris@0
|
374 $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 $arguments[$key] = new Reference($arg->getAttribute('id'), $invalidBehavior);
|
Chris@0
|
378 break;
|
Chris@0
|
379 case 'expression':
|
Chris@0
|
380 $arguments[$key] = new Expression($arg->nodeValue);
|
Chris@0
|
381 break;
|
Chris@0
|
382 case 'collection':
|
Chris@0
|
383 $arguments[$key] = $this->getArgumentsAsPhp($arg, $name, false);
|
Chris@0
|
384 break;
|
Chris@0
|
385 case 'string':
|
Chris@0
|
386 $arguments[$key] = $arg->nodeValue;
|
Chris@0
|
387 break;
|
Chris@0
|
388 case 'constant':
|
Chris@0
|
389 $arguments[$key] = constant(trim($arg->nodeValue));
|
Chris@0
|
390 break;
|
Chris@0
|
391 default:
|
Chris@0
|
392 $arguments[$key] = XmlUtils::phpize($arg->nodeValue);
|
Chris@0
|
393 }
|
Chris@0
|
394 }
|
Chris@0
|
395
|
Chris@0
|
396 return $arguments;
|
Chris@0
|
397 }
|
Chris@0
|
398
|
Chris@0
|
399 /**
|
Chris@0
|
400 * Get child elements by name.
|
Chris@0
|
401 *
|
Chris@0
|
402 * @param \DOMNode $node
|
Chris@0
|
403 * @param mixed $name
|
Chris@0
|
404 *
|
Chris@0
|
405 * @return array
|
Chris@0
|
406 */
|
Chris@0
|
407 private function getChildren(\DOMNode $node, $name)
|
Chris@0
|
408 {
|
Chris@0
|
409 $children = array();
|
Chris@0
|
410 foreach ($node->childNodes as $child) {
|
Chris@0
|
411 if ($child instanceof \DOMElement && $child->localName === $name && $child->namespaceURI === self::NS) {
|
Chris@0
|
412 $children[] = $child;
|
Chris@0
|
413 }
|
Chris@0
|
414 }
|
Chris@0
|
415
|
Chris@0
|
416 return $children;
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 /**
|
Chris@0
|
420 * Validates a documents XML schema.
|
Chris@0
|
421 *
|
Chris@0
|
422 * @param \DOMDocument $dom
|
Chris@0
|
423 *
|
Chris@0
|
424 * @return bool
|
Chris@0
|
425 *
|
Chris@0
|
426 * @throws RuntimeException When extension references a non-existent XSD file
|
Chris@0
|
427 */
|
Chris@0
|
428 public function validateSchema(\DOMDocument $dom)
|
Chris@0
|
429 {
|
Chris@0
|
430 $schemaLocations = array('http://symfony.com/schema/dic/services' => str_replace('\\', '/', __DIR__.'/schema/dic/services/services-1.0.xsd'));
|
Chris@0
|
431
|
Chris@0
|
432 if ($element = $dom->documentElement->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')) {
|
Chris@0
|
433 $items = preg_split('/\s+/', $element);
|
Chris@0
|
434 for ($i = 0, $nb = count($items); $i < $nb; $i += 2) {
|
Chris@0
|
435 if (!$this->container->hasExtension($items[$i])) {
|
Chris@0
|
436 continue;
|
Chris@0
|
437 }
|
Chris@0
|
438
|
Chris@0
|
439 if (($extension = $this->container->getExtension($items[$i])) && false !== $extension->getXsdValidationBasePath()) {
|
Chris@0
|
440 $path = str_replace($extension->getNamespace(), str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);
|
Chris@0
|
441
|
Chris@0
|
442 if (!is_file($path)) {
|
Chris@0
|
443 throw new RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s"', get_class($extension), $path));
|
Chris@0
|
444 }
|
Chris@0
|
445
|
Chris@0
|
446 $schemaLocations[$items[$i]] = $path;
|
Chris@0
|
447 }
|
Chris@0
|
448 }
|
Chris@0
|
449 }
|
Chris@0
|
450
|
Chris@0
|
451 $tmpfiles = array();
|
Chris@0
|
452 $imports = '';
|
Chris@0
|
453 foreach ($schemaLocations as $namespace => $location) {
|
Chris@0
|
454 $parts = explode('/', $location);
|
Chris@0
|
455 if (0 === stripos($location, 'phar://')) {
|
Chris@0
|
456 $tmpfile = tempnam(sys_get_temp_dir(), 'sf2');
|
Chris@0
|
457 if ($tmpfile) {
|
Chris@0
|
458 copy($location, $tmpfile);
|
Chris@0
|
459 $tmpfiles[] = $tmpfile;
|
Chris@0
|
460 $parts = explode('/', str_replace('\\', '/', $tmpfile));
|
Chris@0
|
461 }
|
Chris@0
|
462 }
|
Chris@0
|
463 $drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
|
Chris@0
|
464 $location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
|
Chris@0
|
465
|
Chris@0
|
466 $imports .= sprintf(' <xsd:import namespace="%s" schemaLocation="%s" />'."\n", $namespace, $location);
|
Chris@0
|
467 }
|
Chris@0
|
468
|
Chris@0
|
469 $source = <<<EOF
|
Chris@0
|
470 <?xml version="1.0" encoding="utf-8" ?>
|
Chris@0
|
471 <xsd:schema xmlns="http://symfony.com/schema"
|
Chris@0
|
472 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
Chris@0
|
473 targetNamespace="http://symfony.com/schema"
|
Chris@0
|
474 elementFormDefault="qualified">
|
Chris@0
|
475
|
Chris@0
|
476 <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
|
Chris@0
|
477 $imports
|
Chris@0
|
478 </xsd:schema>
|
Chris@0
|
479 EOF
|
Chris@0
|
480 ;
|
Chris@0
|
481
|
Chris@0
|
482 $disableEntities = libxml_disable_entity_loader(false);
|
Chris@0
|
483 $valid = @$dom->schemaValidateSource($source);
|
Chris@0
|
484 libxml_disable_entity_loader($disableEntities);
|
Chris@0
|
485
|
Chris@0
|
486 foreach ($tmpfiles as $tmpfile) {
|
Chris@0
|
487 @unlink($tmpfile);
|
Chris@0
|
488 }
|
Chris@0
|
489
|
Chris@0
|
490 return $valid;
|
Chris@0
|
491 }
|
Chris@0
|
492
|
Chris@0
|
493 /**
|
Chris@0
|
494 * Validates an alias.
|
Chris@0
|
495 *
|
Chris@0
|
496 * @param \DOMElement $alias
|
Chris@0
|
497 * @param string $file
|
Chris@0
|
498 */
|
Chris@0
|
499 private function validateAlias(\DOMElement $alias, $file)
|
Chris@0
|
500 {
|
Chris@0
|
501 foreach ($alias->attributes as $name => $node) {
|
Chris@0
|
502 if (!in_array($name, array('alias', 'id', 'public'))) {
|
Chris@0
|
503 @trigger_error(sprintf('Using the attribute "%s" is deprecated for the service "%s" which is defined as an alias in "%s". Allowed attributes for service aliases are "alias", "id" and "public". The XmlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.', $name, $alias->getAttribute('id'), $file), E_USER_DEPRECATED);
|
Chris@0
|
504 }
|
Chris@0
|
505 }
|
Chris@0
|
506
|
Chris@0
|
507 foreach ($alias->childNodes as $child) {
|
Chris@0
|
508 if ($child instanceof \DOMElement && $child->namespaceURI === self::NS) {
|
Chris@0
|
509 @trigger_error(sprintf('Using the element "%s" is deprecated for the service "%s" which is defined as an alias in "%s". The XmlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported elements.', $child->localName, $alias->getAttribute('id'), $file), E_USER_DEPRECATED);
|
Chris@0
|
510 }
|
Chris@0
|
511 }
|
Chris@0
|
512 }
|
Chris@0
|
513
|
Chris@0
|
514 /**
|
Chris@0
|
515 * Validates an extension.
|
Chris@0
|
516 *
|
Chris@0
|
517 * @param \DOMDocument $dom
|
Chris@0
|
518 * @param string $file
|
Chris@0
|
519 *
|
Chris@0
|
520 * @throws InvalidArgumentException When no extension is found corresponding to a tag
|
Chris@0
|
521 */
|
Chris@0
|
522 private function validateExtensions(\DOMDocument $dom, $file)
|
Chris@0
|
523 {
|
Chris@0
|
524 foreach ($dom->documentElement->childNodes as $node) {
|
Chris@0
|
525 if (!$node instanceof \DOMElement || 'http://symfony.com/schema/dic/services' === $node->namespaceURI) {
|
Chris@0
|
526 continue;
|
Chris@0
|
527 }
|
Chris@0
|
528
|
Chris@0
|
529 // can it be handled by an extension?
|
Chris@0
|
530 if (!$this->container->hasExtension($node->namespaceURI)) {
|
Chris@0
|
531 $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getNamespace(); }, $this->container->getExtensions()));
|
Chris@0
|
532 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
533 'There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s',
|
Chris@0
|
534 $node->tagName,
|
Chris@0
|
535 $file,
|
Chris@0
|
536 $node->namespaceURI,
|
Chris@0
|
537 $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'
|
Chris@0
|
538 ));
|
Chris@0
|
539 }
|
Chris@0
|
540 }
|
Chris@0
|
541 }
|
Chris@0
|
542
|
Chris@0
|
543 /**
|
Chris@0
|
544 * Loads from an extension.
|
Chris@0
|
545 *
|
Chris@0
|
546 * @param \DOMDocument $xml
|
Chris@0
|
547 */
|
Chris@0
|
548 private function loadFromExtensions(\DOMDocument $xml)
|
Chris@0
|
549 {
|
Chris@0
|
550 foreach ($xml->documentElement->childNodes as $node) {
|
Chris@0
|
551 if (!$node instanceof \DOMElement || $node->namespaceURI === self::NS) {
|
Chris@0
|
552 continue;
|
Chris@0
|
553 }
|
Chris@0
|
554
|
Chris@0
|
555 $values = static::convertDomElementToArray($node);
|
Chris@0
|
556 if (!is_array($values)) {
|
Chris@0
|
557 $values = array();
|
Chris@0
|
558 }
|
Chris@0
|
559
|
Chris@0
|
560 $this->container->loadFromExtension($node->namespaceURI, $values);
|
Chris@0
|
561 }
|
Chris@0
|
562 }
|
Chris@0
|
563
|
Chris@0
|
564 /**
|
Chris@0
|
565 * Converts a \DomElement object to a PHP array.
|
Chris@0
|
566 *
|
Chris@0
|
567 * The following rules applies during the conversion:
|
Chris@0
|
568 *
|
Chris@0
|
569 * * Each tag is converted to a key value or an array
|
Chris@0
|
570 * if there is more than one "value"
|
Chris@0
|
571 *
|
Chris@0
|
572 * * The content of a tag is set under a "value" key (<foo>bar</foo>)
|
Chris@0
|
573 * if the tag also has some nested tags
|
Chris@0
|
574 *
|
Chris@0
|
575 * * The attributes are converted to keys (<foo foo="bar"/>)
|
Chris@0
|
576 *
|
Chris@0
|
577 * * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
|
Chris@0
|
578 *
|
Chris@0
|
579 * @param \DomElement $element A \DomElement instance
|
Chris@0
|
580 *
|
Chris@0
|
581 * @return array A PHP array
|
Chris@0
|
582 */
|
Chris@0
|
583 public static function convertDomElementToArray(\DOMElement $element)
|
Chris@0
|
584 {
|
Chris@0
|
585 return XmlUtils::convertDomElementToArray($element);
|
Chris@0
|
586 }
|
Chris@0
|
587 }
|