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\Loader;
14 use Symfony\Component\Config\Util\XmlUtils;
15 use Symfony\Component\DependencyInjection\Alias;
16 use Symfony\Component\DependencyInjection\Argument\BoundArgument;
17 use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
18 use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
19 use Symfony\Component\DependencyInjection\ChildDefinition;
20 use Symfony\Component\DependencyInjection\ContainerBuilder;
21 use Symfony\Component\DependencyInjection\ContainerInterface;
22 use Symfony\Component\DependencyInjection\Definition;
23 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
24 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
25 use Symfony\Component\DependencyInjection\Reference;
26 use Symfony\Component\ExpressionLanguage\Expression;
29 * XmlFileLoader loads XML files service definitions.
31 * @author Fabien Potencier <fabien@symfony.com>
33 class XmlFileLoader extends FileLoader
35 const NS = 'http://symfony.com/schema/dic/services';
40 public function load($resource, $type = null)
42 $path = $this->locator->locate($resource);
44 $xml = $this->parseFileToDOM($path);
46 $this->container->fileExists($path);
48 $defaults = $this->getServiceDefaults($xml, $path);
51 $this->processAnonymousServices($xml, $path, $defaults);
54 $this->parseImports($xml, $path);
57 $this->parseParameters($xml, $path);
60 $this->loadFromExtensions($xml);
64 $this->parseDefinitions($xml, $path, $defaults);
66 $this->instanceof = array();
73 public function supports($resource, $type = null)
75 if (!\is_string($resource)) {
79 if (null === $type && 'xml' === pathinfo($resource, PATHINFO_EXTENSION)) {
83 return 'xml' === $type;
89 * @param \DOMDocument $xml
92 private function parseParameters(\DOMDocument $xml, $file)
94 if ($parameters = $this->getChildren($xml->documentElement, 'parameters')) {
95 $this->container->getParameterBag()->add($this->getArgumentsAsPhp($parameters[0], 'parameter', $file));
102 * @param \DOMDocument $xml
103 * @param string $file
105 private function parseImports(\DOMDocument $xml, $file)
107 $xpath = new \DOMXPath($xml);
108 $xpath->registerNamespace('container', self::NS);
110 if (false === $imports = $xpath->query('//container:imports/container:import')) {
114 $defaultDirectory = \dirname($file);
115 foreach ($imports as $import) {
116 $this->setCurrentDir($defaultDirectory);
117 $this->import($import->getAttribute('resource'), XmlUtils::phpize($import->getAttribute('type')) ?: null, (bool) XmlUtils::phpize($import->getAttribute('ignore-errors')), $file);
122 * Parses multiple definitions.
124 * @param \DOMDocument $xml
125 * @param string $file
127 private function parseDefinitions(\DOMDocument $xml, $file, $defaults)
129 $xpath = new \DOMXPath($xml);
130 $xpath->registerNamespace('container', self::NS);
132 if (false === $services = $xpath->query('//container:services/container:service|//container:services/container:prototype')) {
135 $this->setCurrentDir(\dirname($file));
137 $this->instanceof = array();
138 $this->isLoadingInstanceof = true;
139 $instanceof = $xpath->query('//container:services/container:instanceof');
140 foreach ($instanceof as $service) {
141 $this->setDefinition((string) $service->getAttribute('id'), $this->parseDefinition($service, $file, array()));
144 $this->isLoadingInstanceof = false;
145 foreach ($services as $service) {
146 if (null !== $definition = $this->parseDefinition($service, $file, $defaults)) {
147 if ('prototype' === $service->tagName) {
148 $this->registerClasses($definition, (string) $service->getAttribute('namespace'), (string) $service->getAttribute('resource'), (string) $service->getAttribute('exclude'));
150 $this->setDefinition((string) $service->getAttribute('id'), $definition);
157 * Get service defaults.
161 private function getServiceDefaults(\DOMDocument $xml, $file)
163 $xpath = new \DOMXPath($xml);
164 $xpath->registerNamespace('container', self::NS);
166 if (null === $defaultsNode = $xpath->query('//container:services/container:defaults')->item(0)) {
170 'tags' => $this->getChildren($defaultsNode, 'tag'),
171 'bind' => array_map(function ($v) { return new BoundArgument($v); }, $this->getArgumentsAsPhp($defaultsNode, 'bind', $file)),
174 foreach ($defaults['tags'] as $tag) {
175 if ('' === $tag->getAttribute('name')) {
176 throw new InvalidArgumentException(sprintf('The tag name for tag "<defaults>" in %s must be a non-empty string.', $file));
180 if ($defaultsNode->hasAttribute('autowire')) {
181 $defaults['autowire'] = XmlUtils::phpize($defaultsNode->getAttribute('autowire'));
183 if ($defaultsNode->hasAttribute('public')) {
184 $defaults['public'] = XmlUtils::phpize($defaultsNode->getAttribute('public'));
186 if ($defaultsNode->hasAttribute('autoconfigure')) {
187 $defaults['autoconfigure'] = XmlUtils::phpize($defaultsNode->getAttribute('autoconfigure'));
194 * Parses an individual Definition.
196 * @param \DOMElement $service
197 * @param string $file
198 * @param array $defaults
200 * @return Definition|null
202 private function parseDefinition(\DOMElement $service, $file, array $defaults)
204 if ($alias = $service->getAttribute('alias')) {
205 $this->validateAlias($service, $file);
207 $this->container->setAlias((string) $service->getAttribute('id'), $alias = new Alias($alias));
208 if ($publicAttr = $service->getAttribute('public')) {
209 $alias->setPublic(XmlUtils::phpize($publicAttr));
210 } elseif (isset($defaults['public'])) {
211 $alias->setPublic($defaults['public']);
217 if ($this->isLoadingInstanceof) {
218 $definition = new ChildDefinition('');
219 } elseif ($parent = $service->getAttribute('parent')) {
220 if (!empty($this->instanceof)) {
221 throw new InvalidArgumentException(sprintf('The service "%s" cannot use the "parent" option in the same file where "instanceof" configuration is defined as using both is not supported. Move your child definitions to a separate file.', $service->getAttribute('id')));
224 foreach ($defaults as $k => $v) {
226 // since tags are never inherited from parents, there is no confusion
227 // thus we can safely add them as defaults to ChildDefinition
231 if ($defaults['bind']) {
232 throw new InvalidArgumentException(sprintf('Bound values on service "%s" cannot be inherited from "defaults" when a "parent" is set. Move your child definitions to a separate file.', $service->getAttribute('id')));
237 if (!$service->hasAttribute($k)) {
238 throw new InvalidArgumentException(sprintf('Attribute "%s" on service "%s" cannot be inherited from "defaults" when a "parent" is set. Move your child definitions to a separate file or define this attribute explicitly.', $k, $service->getAttribute('id')));
242 $definition = new ChildDefinition($parent);
244 $definition = new Definition();
246 if (isset($defaults['public'])) {
247 $definition->setPublic($defaults['public']);
249 if (isset($defaults['autowire'])) {
250 $definition->setAutowired($defaults['autowire']);
252 if (isset($defaults['autoconfigure'])) {
253 $definition->setAutoconfigured($defaults['autoconfigure']);
256 $definition->setChanges(array());
259 foreach (array('class', 'public', 'shared', 'synthetic', 'lazy', 'abstract') as $key) {
260 if ($value = $service->getAttribute($key)) {
261 $method = 'set'.$key;
262 $definition->$method(XmlUtils::phpize($value));
266 if ($value = $service->getAttribute('autowire')) {
267 $definition->setAutowired(XmlUtils::phpize($value));
270 if ($value = $service->getAttribute('autoconfigure')) {
271 if (!$definition instanceof ChildDefinition) {
272 $definition->setAutoconfigured(XmlUtils::phpize($value));
273 } elseif ($value = XmlUtils::phpize($value)) {
274 throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try setting autoconfigure="false" for the service.', $service->getAttribute('id')));
278 if ($files = $this->getChildren($service, 'file')) {
279 $definition->setFile($files[0]->nodeValue);
282 if ($deprecated = $this->getChildren($service, 'deprecated')) {
283 $definition->setDeprecated(true, $deprecated[0]->nodeValue ?: null);
286 $definition->setArguments($this->getArgumentsAsPhp($service, 'argument', $file, false, $definition instanceof ChildDefinition));
287 $definition->setProperties($this->getArgumentsAsPhp($service, 'property', $file));
289 if ($factories = $this->getChildren($service, 'factory')) {
290 $factory = $factories[0];
291 if ($function = $factory->getAttribute('function')) {
292 $definition->setFactory($function);
294 if ($childService = $factory->getAttribute('service')) {
295 $class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
297 $class = $factory->hasAttribute('class') ? $factory->getAttribute('class') : null;
300 $definition->setFactory(array($class, $factory->getAttribute('method')));
304 if ($configurators = $this->getChildren($service, 'configurator')) {
305 $configurator = $configurators[0];
306 if ($function = $configurator->getAttribute('function')) {
307 $definition->setConfigurator($function);
309 if ($childService = $configurator->getAttribute('service')) {
310 $class = new Reference($childService, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
312 $class = $configurator->getAttribute('class');
315 $definition->setConfigurator(array($class, $configurator->getAttribute('method')));
319 foreach ($this->getChildren($service, 'call') as $call) {
320 $definition->addMethodCall($call->getAttribute('method'), $this->getArgumentsAsPhp($call, 'argument', $file));
323 $tags = $this->getChildren($service, 'tag');
325 if (!empty($defaults['tags'])) {
326 $tags = array_merge($tags, $defaults['tags']);
329 foreach ($tags as $tag) {
330 $parameters = array();
331 foreach ($tag->attributes as $name => $node) {
332 if ('name' === $name) {
336 if (false !== strpos($name, '-') && false === strpos($name, '_') && !array_key_exists($normalizedName = str_replace('-', '_', $name), $parameters)) {
337 $parameters[$normalizedName] = XmlUtils::phpize($node->nodeValue);
339 // keep not normalized key
340 $parameters[$name] = XmlUtils::phpize($node->nodeValue);
343 if ('' === $tag->getAttribute('name')) {
344 throw new InvalidArgumentException(sprintf('The tag name for service "%s" in %s must be a non-empty string.', (string) $service->getAttribute('id'), $file));
347 $definition->addTag($tag->getAttribute('name'), $parameters);
350 foreach ($this->getChildren($service, 'autowiring-type') as $type) {
351 $definition->addAutowiringType($type->textContent);
354 $bindings = $this->getArgumentsAsPhp($service, 'bind', $file);
355 if (isset($defaults['bind'])) {
356 // deep clone, to avoid multiple process of the same instance in the passes
357 $bindings = array_merge(unserialize(serialize($defaults['bind'])), $bindings);
360 $definition->setBindings($bindings);
363 if ($value = $service->getAttribute('decorates')) {
364 $renameId = $service->hasAttribute('decoration-inner-name') ? $service->getAttribute('decoration-inner-name') : null;
365 $priority = $service->hasAttribute('decoration-priority') ? $service->getAttribute('decoration-priority') : 0;
366 $definition->setDecoratedService($value, $renameId, $priority);
373 * Parses a XML file to a \DOMDocument.
375 * @param string $file Path to a file
377 * @return \DOMDocument
379 * @throws InvalidArgumentException When loading of XML file returns error
381 private function parseFileToDOM($file)
384 $dom = XmlUtils::loadFile($file, array($this, 'validateSchema'));
385 } catch (\InvalidArgumentException $e) {
386 throw new InvalidArgumentException(sprintf('Unable to parse file "%s".', $file), $e->getCode(), $e);
389 $this->validateExtensions($dom, $file);
395 * Processes anonymous services.
397 * @param \DOMDocument $xml
398 * @param string $file
399 * @param array $defaults
401 private function processAnonymousServices(\DOMDocument $xml, $file, $defaults)
403 $definitions = array();
405 $suffix = '~'.ContainerBuilder::hash($file);
407 $xpath = new \DOMXPath($xml);
408 $xpath->registerNamespace('container', self::NS);
410 // anonymous services as arguments/properties
411 if (false !== $nodes = $xpath->query('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]|//container:bind[not(@id)]|//container:factory[not(@service)]|//container:configurator[not(@service)]')) {
412 foreach ($nodes as $node) {
413 if ($services = $this->getChildren($node, 'service')) {
414 // give it a unique name
415 $id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')).$suffix);
416 $node->setAttribute('id', $id);
417 $node->setAttribute('service', $id);
419 $definitions[$id] = array($services[0], $file, false);
420 $services[0]->setAttribute('id', $id);
422 // anonymous services are always private
423 // we could not use the constant false here, because of XML parsing
424 $services[0]->setAttribute('public', 'false');
429 // anonymous services "in the wild"
430 if (false !== $nodes = $xpath->query('//container:services/container:service[not(@id)]')) {
431 foreach ($nodes as $node) {
432 @trigger_error(sprintf('Top-level anonymous services are deprecated since Symfony 3.4, the "id" attribute will be required in version 4.0 in %s at line %d.', $file, $node->getLineNo()), E_USER_DEPRECATED);
434 // give it a unique name
435 $id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $node->getAttribute('class')).$suffix);
436 $node->setAttribute('id', $id);
437 $definitions[$id] = array($node, $file, true);
441 // resolve definitions
442 uksort($definitions, 'strnatcmp');
443 foreach (array_reverse($definitions) as $id => list($domElement, $file, $wild)) {
444 if (null !== $definition = $this->parseDefinition($domElement, $file, $wild ? $defaults : array())) {
445 $this->setDefinition($id, $definition);
448 if (true === $wild) {
449 $tmpDomElement = new \DOMElement('_services', null, self::NS);
450 $domElement->parentNode->replaceChild($tmpDomElement, $domElement);
451 $tmpDomElement->setAttribute('id', $id);
457 * Returns arguments as valid php types.
459 * @param \DOMElement $node
460 * @param string $name
461 * @param string $file
462 * @param bool $lowercase
466 private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase = true, $isChildDefinition = false)
468 $arguments = array();
469 foreach ($this->getChildren($node, $name) as $arg) {
470 if ($arg->hasAttribute('name')) {
471 $arg->setAttribute('key', $arg->getAttribute('name'));
474 // this is used by ChildDefinition to overwrite a specific
475 // argument of the parent definition
476 if ($arg->hasAttribute('index')) {
477 $key = ($isChildDefinition ? 'index_' : '').$arg->getAttribute('index');
478 } elseif (!$arg->hasAttribute('key')) {
479 // Append an empty argument, then fetch its key to overwrite it later
481 $keys = array_keys($arguments);
482 $key = array_pop($keys);
484 $key = $arg->getAttribute('key');
487 $onInvalid = $arg->getAttribute('on-invalid');
488 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
489 if ('ignore' == $onInvalid) {
490 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
491 } elseif ('ignore_uninitialized' == $onInvalid) {
492 $invalidBehavior = ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
493 } elseif ('null' == $onInvalid) {
494 $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
497 switch ($arg->getAttribute('type')) {
499 if (!$arg->getAttribute('id')) {
500 throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="service" has no or empty "id" attribute in "%s".', $name, $file));
502 if ($arg->hasAttribute('strict')) {
503 @trigger_error(sprintf('The "strict" attribute used when referencing the "%s" service is deprecated since Symfony 3.3 and will be removed in 4.0.', $arg->getAttribute('id')), E_USER_DEPRECATED);
506 $arguments[$key] = new Reference($arg->getAttribute('id'), $invalidBehavior);
509 if (!class_exists(Expression::class)) {
510 throw new \LogicException(sprintf('The type="expression" attribute cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'));
513 $arguments[$key] = new Expression($arg->nodeValue);
516 $arguments[$key] = $this->getArgumentsAsPhp($arg, $name, $file, false);
519 $arg = $this->getArgumentsAsPhp($arg, $name, $file, false);
521 $arguments[$key] = new IteratorArgument($arg);
522 } catch (InvalidArgumentException $e) {
523 throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="iterator" only accepts collections of type="service" references in "%s".', $name, $file));
527 if (!$arg->getAttribute('tag')) {
528 throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="tagged" has no or empty "tag" attribute in "%s".', $name, $file));
530 $arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'));
533 $arguments[$key] = $arg->nodeValue;
536 $arguments[$key] = \constant(trim($arg->nodeValue));
539 $arguments[$key] = XmlUtils::phpize($arg->nodeValue);
547 * Get child elements by name.
549 * @param \DOMNode $node
554 private function getChildren(\DOMNode $node, $name)
557 foreach ($node->childNodes as $child) {
558 if ($child instanceof \DOMElement && $child->localName === $name && self::NS === $child->namespaceURI) {
559 $children[] = $child;
567 * Validates a documents XML schema.
569 * @param \DOMDocument $dom
573 * @throws RuntimeException When extension references a non-existent XSD file
575 public function validateSchema(\DOMDocument $dom)
577 $schemaLocations = array('http://symfony.com/schema/dic/services' => str_replace('\\', '/', __DIR__.'/schema/dic/services/services-1.0.xsd'));
579 if ($element = $dom->documentElement->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')) {
580 $items = preg_split('/\s+/', $element);
581 for ($i = 0, $nb = \count($items); $i < $nb; $i += 2) {
582 if (!$this->container->hasExtension($items[$i])) {
586 if (($extension = $this->container->getExtension($items[$i])) && false !== $extension->getXsdValidationBasePath()) {
587 $path = str_replace($extension->getNamespace(), str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);
589 if (!is_file($path)) {
590 throw new RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s"', \get_class($extension), $path));
593 $schemaLocations[$items[$i]] = $path;
600 foreach ($schemaLocations as $namespace => $location) {
601 $parts = explode('/', $location);
602 $locationstart = 'file:///';
603 if (0 === stripos($location, 'phar://')) {
604 $tmpfile = tempnam(sys_get_temp_dir(), 'symfony');
606 copy($location, $tmpfile);
607 $tmpfiles[] = $tmpfile;
608 $parts = explode('/', str_replace('\\', '/', $tmpfile));
611 $locationstart = 'phar:///';
614 $drive = '\\' === \DIRECTORY_SEPARATOR ? array_shift($parts).'/' : '';
615 $location = $locationstart.$drive.implode('/', array_map('rawurlencode', $parts));
617 $imports .= sprintf(' <xsd:import namespace="%s" schemaLocation="%s" />'."\n", $namespace, $location);
621 <?xml version="1.0" encoding="utf-8" ?>
622 <xsd:schema xmlns="http://symfony.com/schema"
623 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
624 targetNamespace="http://symfony.com/schema"
625 elementFormDefault="qualified">
627 <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
633 $disableEntities = libxml_disable_entity_loader(false);
634 $valid = @$dom->schemaValidateSource($source);
635 libxml_disable_entity_loader($disableEntities);
637 foreach ($tmpfiles as $tmpfile) {
645 * Validates an alias.
647 * @param \DOMElement $alias
648 * @param string $file
650 private function validateAlias(\DOMElement $alias, $file)
652 foreach ($alias->attributes as $name => $node) {
653 if (!\in_array($name, array('alias', 'id', 'public'))) {
654 @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);
658 foreach ($alias->childNodes as $child) {
659 if ($child instanceof \DOMElement && self::NS === $child->namespaceURI) {
660 @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);
666 * Validates an extension.
668 * @param \DOMDocument $dom
669 * @param string $file
671 * @throws InvalidArgumentException When no extension is found corresponding to a tag
673 private function validateExtensions(\DOMDocument $dom, $file)
675 foreach ($dom->documentElement->childNodes as $node) {
676 if (!$node instanceof \DOMElement || 'http://symfony.com/schema/dic/services' === $node->namespaceURI) {
680 // can it be handled by an extension?
681 if (!$this->container->hasExtension($node->namespaceURI)) {
682 $extensionNamespaces = array_filter(array_map(function ($ext) { return $ext->getNamespace(); }, $this->container->getExtensions()));
683 throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s). Looked for namespace "%s", found %s', $node->tagName, $file, $node->namespaceURI, $extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none'));
689 * Loads from an extension.
691 * @param \DOMDocument $xml
693 private function loadFromExtensions(\DOMDocument $xml)
695 foreach ($xml->documentElement->childNodes as $node) {
696 if (!$node instanceof \DOMElement || self::NS === $node->namespaceURI) {
700 $values = static::convertDomElementToArray($node);
701 if (!\is_array($values)) {
705 $this->container->loadFromExtension($node->namespaceURI, $values);
710 * Converts a \DOMElement object to a PHP array.
712 * The following rules applies during the conversion:
714 * * Each tag is converted to a key value or an array
715 * if there is more than one "value"
717 * * The content of a tag is set under a "value" key (<foo>bar</foo>)
718 * if the tag also has some nested tags
720 * * The attributes are converted to keys (<foo foo="bar"/>)
722 * * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
724 * @param \DOMElement $element A \DOMElement instance
726 * @return array A PHP array
728 public static function convertDomElementToArray(\DOMElement $element)
730 return XmlUtils::convertDomElementToArray($element);