vendor/symfony/dependency-injection/Loader/XmlFileLoader.php line 379

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