vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php line 354

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\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
  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. use Symfony\Component\Yaml\Exception\ParseException;
  25. use Symfony\Component\Yaml\Parser as YamlParser;
  26. use Symfony\Component\Yaml\Tag\TaggedValue;
  27. use Symfony\Component\Yaml\Yaml;
  28. /**
  29.  * YamlFileLoader loads YAML files service definitions.
  30.  *
  31.  * @author Fabien Potencier <fabien@symfony.com>
  32.  */
  33. class YamlFileLoader extends FileLoader
  34. {
  35.     private static $serviceKeywords = [
  36.         'alias' => 'alias',
  37.         'parent' => 'parent',
  38.         'class' => 'class',
  39.         'shared' => 'shared',
  40.         'synthetic' => 'synthetic',
  41.         'lazy' => 'lazy',
  42.         'public' => 'public',
  43.         'abstract' => 'abstract',
  44.         'deprecated' => 'deprecated',
  45.         'factory' => 'factory',
  46.         'file' => 'file',
  47.         'arguments' => 'arguments',
  48.         'properties' => 'properties',
  49.         'configurator' => 'configurator',
  50.         'calls' => 'calls',
  51.         'tags' => 'tags',
  52.         'decorates' => 'decorates',
  53.         'decoration_inner_name' => 'decoration_inner_name',
  54.         'decoration_priority' => 'decoration_priority',
  55.         'autowire' => 'autowire',
  56.         'autowiring_types' => 'autowiring_types',
  57.         'autoconfigure' => 'autoconfigure',
  58.         'bind' => 'bind',
  59.     ];
  60.     private static $prototypeKeywords = [
  61.         'resource' => 'resource',
  62.         'namespace' => 'namespace',
  63.         'exclude' => 'exclude',
  64.         'parent' => 'parent',
  65.         'shared' => 'shared',
  66.         'lazy' => 'lazy',
  67.         'public' => 'public',
  68.         'abstract' => 'abstract',
  69.         'deprecated' => 'deprecated',
  70.         'factory' => 'factory',
  71.         'arguments' => 'arguments',
  72.         'properties' => 'properties',
  73.         'configurator' => 'configurator',
  74.         'calls' => 'calls',
  75.         'tags' => 'tags',
  76.         'autowire' => 'autowire',
  77.         'autoconfigure' => 'autoconfigure',
  78.         'bind' => 'bind',
  79.     ];
  80.     private static $instanceofKeywords = [
  81.         'shared' => 'shared',
  82.         'lazy' => 'lazy',
  83.         'public' => 'public',
  84.         'properties' => 'properties',
  85.         'configurator' => 'configurator',
  86.         'calls' => 'calls',
  87.         'tags' => 'tags',
  88.         'autowire' => 'autowire',
  89.     ];
  90.     private static $defaultsKeywords = [
  91.         'public' => 'public',
  92.         'tags' => 'tags',
  93.         'autowire' => 'autowire',
  94.         'autoconfigure' => 'autoconfigure',
  95.         'bind' => 'bind',
  96.     ];
  97.     private $yamlParser;
  98.     private $anonymousServicesCount;
  99.     private $anonymousServicesSuffix;
  100.     /**
  101.      * {@inheritdoc}
  102.      */
  103.     public function load($resource$type null)
  104.     {
  105.         $path $this->locator->locate($resource);
  106.         $content $this->loadFile($path);
  107.         $this->container->fileExists($path);
  108.         // empty file
  109.         if (null === $content) {
  110.             return;
  111.         }
  112.         // imports
  113.         $this->parseImports($content$path);
  114.         // parameters
  115.         if (isset($content['parameters'])) {
  116.             if (!\is_array($content['parameters'])) {
  117.                 throw new InvalidArgumentException(sprintf('The "parameters" key should contain an array in "%s". Check your YAML syntax.'$path));
  118.             }
  119.             foreach ($content['parameters'] as $key => $value) {
  120.                 $this->container->setParameter($key$this->resolveServices($value$pathtrue));
  121.             }
  122.         }
  123.         // extensions
  124.         $this->loadFromExtensions($content);
  125.         // services
  126.         $this->anonymousServicesCount 0;
  127.         $this->anonymousServicesSuffix '~'.ContainerBuilder::hash($path);
  128.         $this->setCurrentDir(\dirname($path));
  129.         try {
  130.             $this->parseDefinitions($content$path);
  131.         } finally {
  132.             $this->instanceof = [];
  133.         }
  134.     }
  135.     /**
  136.      * {@inheritdoc}
  137.      */
  138.     public function supports($resource$type null)
  139.     {
  140.         if (!\is_string($resource)) {
  141.             return false;
  142.         }
  143.         if (null === $type && \in_array(pathinfo($resource, \PATHINFO_EXTENSION), ['yaml''yml'], true)) {
  144.             return true;
  145.         }
  146.         return \in_array($type, ['yaml''yml'], true);
  147.     }
  148.     /**
  149.      * Parses all imports.
  150.      *
  151.      * @param string $file
  152.      */
  153.     private function parseImports(array $content$file)
  154.     {
  155.         if (!isset($content['imports'])) {
  156.             return;
  157.         }
  158.         if (!\is_array($content['imports'])) {
  159.             throw new InvalidArgumentException(sprintf('The "imports" key should contain an array in "%s". Check your YAML syntax.'$file));
  160.         }
  161.         $defaultDirectory = \dirname($file);
  162.         foreach ($content['imports'] as $import) {
  163.             if (!\is_array($import)) {
  164.                 $import = ['resource' => $import];
  165.             }
  166.             if (!isset($import['resource'])) {
  167.                 throw new InvalidArgumentException(sprintf('An import should provide a resource in "%s". Check your YAML syntax.'$file));
  168.             }
  169.             $this->setCurrentDir($defaultDirectory);
  170.             $this->import($import['resource'], isset($import['type']) ? $import['type'] : null, isset($import['ignore_errors']) ? (bool) $import['ignore_errors'] : false$file);
  171.         }
  172.     }
  173.     /**
  174.      * Parses definitions.
  175.      *
  176.      * @param string $file
  177.      */
  178.     private function parseDefinitions(array $content$file)
  179.     {
  180.         if (!isset($content['services'])) {
  181.             return;
  182.         }
  183.         if (!\is_array($content['services'])) {
  184.             throw new InvalidArgumentException(sprintf('The "services" key should contain an array in "%s". Check your YAML syntax.'$file));
  185.         }
  186.         if (\array_key_exists('_instanceof'$content['services'])) {
  187.             $instanceof $content['services']['_instanceof'];
  188.             unset($content['services']['_instanceof']);
  189.             if (!\is_array($instanceof)) {
  190.                 throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', \gettype($instanceof), $file));
  191.             }
  192.             $this->instanceof = [];
  193.             $this->isLoadingInstanceof true;
  194.             foreach ($instanceof as $id => $service) {
  195.                 if (!$service || !\is_array($service)) {
  196.                     throw new InvalidArgumentException(sprintf('Type definition "%s" must be a non-empty array within "_instanceof" in "%s". Check your YAML syntax.'$id$file));
  197.                 }
  198.                 if (\is_string($service) && === strpos($service'@')) {
  199.                     throw new InvalidArgumentException(sprintf('Type definition "%s" cannot be an alias within "_instanceof" in "%s". Check your YAML syntax.'$id$file));
  200.                 }
  201.                 $this->parseDefinition($id$service$file, []);
  202.             }
  203.         }
  204.         $this->isLoadingInstanceof false;
  205.         $defaults $this->parseDefaults($content$file);
  206.         foreach ($content['services'] as $id => $service) {
  207.             $this->parseDefinition($id$service$file$defaults);
  208.         }
  209.     }
  210.     /**
  211.      * @param string $file
  212.      *
  213.      * @return array
  214.      *
  215.      * @throws InvalidArgumentException
  216.      */
  217.     private function parseDefaults(array &$content$file)
  218.     {
  219.         if (!\array_key_exists('_defaults'$content['services'])) {
  220.             return [];
  221.         }
  222.         $defaults $content['services']['_defaults'];
  223.         unset($content['services']['_defaults']);
  224.         if (!\is_array($defaults)) {
  225.             throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', \gettype($defaults), $file));
  226.         }
  227.         foreach ($defaults as $key => $default) {
  228.             if (!isset(self::$defaultsKeywords[$key])) {
  229.                 throw new InvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".'$key$fileimplode('", "'self::$defaultsKeywords)));
  230.             }
  231.         }
  232.         if (isset($defaults['tags'])) {
  233.             if (!\is_array($tags $defaults['tags'])) {
  234.                 throw new InvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in "%s". Check your YAML syntax.'$file));
  235.             }
  236.             foreach ($tags as $tag) {
  237.                 if (!\is_array($tag)) {
  238.                     $tag = ['name' => $tag];
  239.                 }
  240.                 if (!isset($tag['name'])) {
  241.                     throw new InvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".'$file));
  242.                 }
  243.                 $name $tag['name'];
  244.                 unset($tag['name']);
  245.                 if (!\is_string($name) || '' === $name) {
  246.                     throw new InvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in "%s".'$file));
  247.                 }
  248.                 foreach ($tag as $attribute => $value) {
  249.                     if (!is_scalar($value) && null !== $value) {
  250.                         throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in "%s". Check your YAML syntax.'$name$attribute$file));
  251.                     }
  252.                 }
  253.             }
  254.         }
  255.         if (isset($defaults['bind'])) {
  256.             if (!\is_array($defaults['bind'])) {
  257.                 throw new InvalidArgumentException(sprintf('Parameter "bind" in "_defaults" must be an array in "%s". Check your YAML syntax.'$file));
  258.             }
  259.             $defaults['bind'] = array_map(function ($v) { return new BoundArgument($v); }, $this->resolveServices($defaults['bind'], $file));
  260.         }
  261.         return $defaults;
  262.     }
  263.     /**
  264.      * @return bool
  265.      */
  266.     private function isUsingShortSyntax(array $service)
  267.     {
  268.         foreach ($service as $key => $value) {
  269.             if (\is_string($key) && ('' === $key || '$' !== $key[0])) {
  270.                 return false;
  271.             }
  272.         }
  273.         return true;
  274.     }
  275.     /**
  276.      * Parses a definition.
  277.      *
  278.      * @param string       $id
  279.      * @param array|string $service
  280.      * @param string       $file
  281.      *
  282.      * @throws InvalidArgumentException When tags are invalid
  283.      */
  284.     private function parseDefinition($id$service$file, array $defaults)
  285.     {
  286.         if (preg_match('/^_[a-zA-Z0-9_]*$/'$id)) {
  287.             @trigger_error(sprintf('Service names that start with an underscore are deprecated since Symfony 3.3 and will be reserved in 4.0. Rename the "%s" service or define it in XML instead.'$id), \E_USER_DEPRECATED);
  288.         }
  289.         if (\is_string($service) && === strpos($service'@')) {
  290.             $this->container->setAlias($id$alias = new Alias(substr($service1)));
  291.             if (isset($defaults['public'])) {
  292.                 $alias->setPublic($defaults['public']);
  293.             }
  294.             return;
  295.         }
  296.         if (\is_array($service) && $this->isUsingShortSyntax($service)) {
  297.             $service = ['arguments' => $service];
  298.         }
  299.         if (null === $service) {
  300.             $service = [];
  301.         }
  302.         if (!\is_array($service)) {
  303.             throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but "%s" found for service "%s" in "%s". Check your YAML syntax.', \gettype($service), $id$file));
  304.         }
  305.         $this->checkDefinition($id$service$file);
  306.         if (isset($service['alias'])) {
  307.             $this->container->setAlias($id$alias = new Alias($service['alias']));
  308.             if (isset($service['public'])) {
  309.                 $alias->setPublic($service['public']);
  310.             } elseif (isset($defaults['public'])) {
  311.                 $alias->setPublic($defaults['public']);
  312.             }
  313.             foreach ($service as $key => $value) {
  314.                 if (!\in_array($key, ['alias''public'])) {
  315.                     @trigger_error(sprintf('The configuration key "%s" is unsupported for the service "%s" which is defined as an alias in "%s". Allowed configuration keys for service aliases are "alias" and "public". The YamlFileLoader will raise an exception in Symfony 4.0, instead of silently ignoring unsupported attributes.'$key$id$file), \E_USER_DEPRECATED);
  316.                 }
  317.             }
  318.             return;
  319.         }
  320.         if ($this->isLoadingInstanceof) {
  321.             $definition = new ChildDefinition('');
  322.         } elseif (isset($service['parent'])) {
  323.             if (!empty($this->instanceof)) {
  324.                 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.'$id));
  325.             }
  326.             foreach ($defaults as $k => $v) {
  327.                 if ('tags' === $k) {
  328.                     // since tags are never inherited from parents, there is no confusion
  329.                     // thus we can safely add them as defaults to ChildDefinition
  330.                     continue;
  331.                 }
  332.                 if ('bind' === $k) {
  333.                     throw new InvalidArgumentException(sprintf('Attribute "bind" on service "%s" cannot be inherited from "_defaults" when a "parent" is set. Move your child definitions to a separate file.'$id));
  334.                 }
  335.                 if (!isset($service[$k])) {
  336.                     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$id));
  337.                 }
  338.             }
  339.             $definition = new ChildDefinition($service['parent']);
  340.         } else {
  341.             $definition = new Definition();
  342.             if (isset($defaults['public'])) {
  343.                 $definition->setPublic($defaults['public']);
  344.             }
  345.             if (isset($defaults['autowire'])) {
  346.                 $definition->setAutowired($defaults['autowire']);
  347.             }
  348.             if (isset($defaults['autoconfigure'])) {
  349.                 $definition->setAutoconfigured($defaults['autoconfigure']);
  350.             }
  351.             $definition->setChanges([]);
  352.         }
  353.         if (isset($service['class'])) {
  354.             $definition->setClass($service['class']);
  355.         }
  356.         if (isset($service['shared'])) {
  357.             $definition->setShared($service['shared']);
  358.         }
  359.         if (isset($service['synthetic'])) {
  360.             $definition->setSynthetic($service['synthetic']);
  361.         }
  362.         if (isset($service['lazy'])) {
  363.             $definition->setLazy($service['lazy']);
  364.         }
  365.         if (isset($service['public'])) {
  366.             $definition->setPublic($service['public']);
  367.         }
  368.         if (isset($service['abstract'])) {
  369.             $definition->setAbstract($service['abstract']);
  370.         }
  371.         if (\array_key_exists('deprecated'$service)) {
  372.             $definition->setDeprecated(true$service['deprecated']);
  373.         }
  374.         if (isset($service['factory'])) {
  375.             $definition->setFactory($this->parseCallable($service['factory'], 'factory'$id$file));
  376.         }
  377.         if (isset($service['file'])) {
  378.             $definition->setFile($service['file']);
  379.         }
  380.         if (isset($service['arguments'])) {
  381.             $definition->setArguments($this->resolveServices($service['arguments'], $file));
  382.         }
  383.         if (isset($service['properties'])) {
  384.             $definition->setProperties($this->resolveServices($service['properties'], $file));
  385.         }
  386.         if (isset($service['configurator'])) {
  387.             $definition->setConfigurator($this->parseCallable($service['configurator'], 'configurator'$id$file));
  388.         }
  389.         if (isset($service['calls'])) {
  390.             if (!\is_array($service['calls'])) {
  391.                 throw new InvalidArgumentException(sprintf('Parameter "calls" must be an array for service "%s" in "%s". Check your YAML syntax.'$id$file));
  392.             }
  393.             foreach ($service['calls'] as $call) {
  394.                 if (isset($call['method'])) {
  395.                     $method $call['method'];
  396.                     $args = isset($call['arguments']) ? $this->resolveServices($call['arguments'], $file) : [];
  397.                 } else {
  398.                     $method $call[0];
  399.                     $args = isset($call[1]) ? $this->resolveServices($call[1], $file) : [];
  400.                 }
  401.                 if (!\is_array($args)) {
  402.                     throw new InvalidArgumentException(sprintf('The second parameter for function call "%s" must be an array of its arguments for service "%s" in "%s". Check your YAML syntax.'$method$id$file));
  403.                 }
  404.                 $definition->addMethodCall($method$args);
  405.             }
  406.         }
  407.         $tags = isset($service['tags']) ? $service['tags'] : [];
  408.         if (!\is_array($tags)) {
  409.             throw new InvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in "%s". Check your YAML syntax.'$id$file));
  410.         }
  411.         if (isset($defaults['tags'])) {
  412.             $tags array_merge($tags$defaults['tags']);
  413.         }
  414.         foreach ($tags as $tag) {
  415.             if (!\is_array($tag)) {
  416.                 $tag = ['name' => $tag];
  417.             }
  418.             if (!isset($tag['name'])) {
  419.                 throw new InvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".'$id$file));
  420.             }
  421.             $name $tag['name'];
  422.             unset($tag['name']);
  423.             if (!\is_string($name) || '' === $name) {
  424.                 throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.'$id$file));
  425.             }
  426.             foreach ($tag as $attribute => $value) {
  427.                 if (!is_scalar($value) && null !== $value) {
  428.                     throw new InvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in "%s". Check your YAML syntax.'$id$name$attribute$file));
  429.                 }
  430.             }
  431.             $definition->addTag($name$tag);
  432.         }
  433.         if (isset($service['decorates'])) {
  434.             if ('' !== $service['decorates'] && '@' === $service['decorates'][0]) {
  435.                 throw new InvalidArgumentException(sprintf('The value of the "decorates" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").'$id$service['decorates'], substr($service['decorates'], 1)));
  436.             }
  437.             $renameId = isset($service['decoration_inner_name']) ? $service['decoration_inner_name'] : null;
  438.             $priority = isset($service['decoration_priority']) ? $service['decoration_priority'] : 0;
  439.             $definition->setDecoratedService($service['decorates'], $renameId$priority);
  440.         }
  441.         if (isset($service['autowire'])) {
  442.             $definition->setAutowired($service['autowire']);
  443.         }
  444.         if (isset($service['autowiring_types'])) {
  445.             if (\is_string($service['autowiring_types'])) {
  446.                 $definition->addAutowiringType($service['autowiring_types']);
  447.             } else {
  448.                 if (!\is_array($service['autowiring_types'])) {
  449.                     throw new InvalidArgumentException(sprintf('Parameter "autowiring_types" must be a string or an array for service "%s" in "%s". Check your YAML syntax.'$id$file));
  450.                 }
  451.                 foreach ($service['autowiring_types'] as $autowiringType) {
  452.                     if (!\is_string($autowiringType)) {
  453.                         throw new InvalidArgumentException(sprintf('A "autowiring_types" attribute must be of type string for service "%s" in "%s". Check your YAML syntax.'$id$file));
  454.                     }
  455.                     $definition->addAutowiringType($autowiringType);
  456.                 }
  457.             }
  458.         }
  459.         if (isset($defaults['bind']) || isset($service['bind'])) {
  460.             // deep clone, to avoid multiple process of the same instance in the passes
  461.             $bindings = isset($defaults['bind']) ? unserialize(serialize($defaults['bind'])) : [];
  462.             if (isset($service['bind'])) {
  463.                 if (!\is_array($service['bind'])) {
  464.                     throw new InvalidArgumentException(sprintf('Parameter "bind" must be an array for service "%s" in "%s". Check your YAML syntax.'$id$file));
  465.                 }
  466.                 $bindings array_merge($bindings$this->resolveServices($service['bind'], $file));
  467.             }
  468.             $definition->setBindings($bindings);
  469.         }
  470.         if (isset($service['autoconfigure'])) {
  471.             if (!$definition instanceof ChildDefinition) {
  472.                 $definition->setAutoconfigured($service['autoconfigure']);
  473.             } elseif ($service['autoconfigure']) {
  474.                 throw new InvalidArgumentException(sprintf('The service "%s" cannot have a "parent" and also have "autoconfigure". Try setting "autoconfigure: false" for the service.'$id));
  475.             }
  476.         }
  477.         if (\array_key_exists('namespace'$service) && !\array_key_exists('resource'$service)) {
  478.             throw new InvalidArgumentException(sprintf('A "resource" attribute must be set when the "namespace" attribute is set for service "%s" in "%s". Check your YAML syntax.'$id$file));
  479.         }
  480.         if (\array_key_exists('resource'$service)) {
  481.             if (!\is_string($service['resource'])) {
  482.                 throw new InvalidArgumentException(sprintf('A "resource" attribute must be of type string for service "%s" in "%s". Check your YAML syntax.'$id$file));
  483.             }
  484.             $exclude = isset($service['exclude']) ? $service['exclude'] : null;
  485.             $namespace = isset($service['namespace']) ? $service['namespace'] : $id;
  486.             $this->registerClasses($definition$namespace$service['resource'], $exclude);
  487.         } else {
  488.             $this->setDefinition($id$definition);
  489.         }
  490.     }
  491.     /**
  492.      * Parses a callable.
  493.      *
  494.      * @param string|array $callable  A callable
  495.      * @param string       $parameter A parameter (e.g. 'factory' or 'configurator')
  496.      * @param string       $id        A service identifier
  497.      * @param string       $file      A parsed file
  498.      *
  499.      * @throws InvalidArgumentException When errors occur
  500.      *
  501.      * @return string|array A parsed callable
  502.      */
  503.     private function parseCallable($callable$parameter$id$file)
  504.     {
  505.         if (\is_string($callable)) {
  506.             if ('' !== $callable && '@' === $callable[0]) {
  507.                 throw new InvalidArgumentException(sprintf('The value of the "%s" option for the "%s" service must be the id of the service without the "@" prefix (replace "%s" with "%s").'$parameter$id$callablesubstr($callable1)));
  508.             }
  509.             if (false !== strpos($callable':') && false === strpos($callable'::')) {
  510.                 $parts explode(':'$callable);
  511.                 return [$this->resolveServices('@'.$parts[0], $file), $parts[1]];
  512.             }
  513.             return $callable;
  514.         }
  515.         if (\is_array($callable)) {
  516.             if (isset($callable[0]) && isset($callable[1])) {
  517.                 return [$this->resolveServices($callable[0], $file), $callable[1]];
  518.             }
  519.             if ('factory' === $parameter && isset($callable[1]) && null === $callable[0]) {
  520.                 return $callable;
  521.             }
  522.             throw new InvalidArgumentException(sprintf('Parameter "%s" must contain an array with two elements for service "%s" in "%s". Check your YAML syntax.'$parameter$id$file));
  523.         }
  524.         throw new InvalidArgumentException(sprintf('Parameter "%s" must be a string or an array for service "%s" in "%s". Check your YAML syntax.'$parameter$id$file));
  525.     }
  526.     /**
  527.      * Loads a YAML file.
  528.      *
  529.      * @param string $file
  530.      *
  531.      * @return array The file content
  532.      *
  533.      * @throws InvalidArgumentException when the given file is not a local file or when it does not exist
  534.      */
  535.     protected function loadFile($file)
  536.     {
  537.         if (!class_exists('Symfony\Component\Yaml\Parser')) {
  538.             throw new RuntimeException('Unable to load YAML config files as the Symfony Yaml Component is not installed.');
  539.         }
  540.         if (!stream_is_local($file)) {
  541.             throw new InvalidArgumentException(sprintf('This is not a local file "%s".'$file));
  542.         }
  543.         if (!file_exists($file)) {
  544.             throw new InvalidArgumentException(sprintf('The file "%s" does not exist.'$file));
  545.         }
  546.         if (null === $this->yamlParser) {
  547.             $this->yamlParser = new YamlParser();
  548.         }
  549.         $prevErrorHandler set_error_handler(function ($level$message$script$line) use ($file, &$prevErrorHandler) {
  550.             $message = \E_USER_DEPRECATED === $level preg_replace('/ on line \d+/'' in "'.$file.'"$0'$message) : $message;
  551.             return $prevErrorHandler $prevErrorHandler($level$message$script$line) : false;
  552.         });
  553.         try {
  554.             $configuration $this->yamlParser->parseFile($fileYaml::PARSE_CONSTANT Yaml::PARSE_CUSTOM_TAGS);
  555.         } catch (ParseException $e) {
  556.             throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML: '$file).$e->getMessage(), 0$e);
  557.         } finally {
  558.             restore_error_handler();
  559.         }
  560.         return $this->validate($configuration$file);
  561.     }
  562.     /**
  563.      * Validates a YAML file.
  564.      *
  565.      * @param mixed  $content
  566.      * @param string $file
  567.      *
  568.      * @return array
  569.      *
  570.      * @throws InvalidArgumentException When service file is not valid
  571.      */
  572.     private function validate($content$file)
  573.     {
  574.         if (null === $content) {
  575.             return $content;
  576.         }
  577.         if (!\is_array($content)) {
  578.             throw new InvalidArgumentException(sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.'$file));
  579.         }
  580.         foreach ($content as $namespace => $data) {
  581.             if (\in_array($namespace, ['imports''parameters''services'])) {
  582.                 continue;
  583.             }
  584.             if (!$this->container->hasExtension($namespace)) {
  585.                 $extensionNamespaces array_filter(array_map(function ($ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
  586.                 throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".'$namespace$file$namespace$extensionNamespaces sprintf('"%s"'implode('", "'$extensionNamespaces)) : 'none'));
  587.             }
  588.         }
  589.         return $content;
  590.     }
  591.     /**
  592.      * Resolves services.
  593.      *
  594.      * @param mixed  $value
  595.      * @param string $file
  596.      * @param bool   $isParameter
  597.      *
  598.      * @return array|string|Reference|ArgumentInterface
  599.      */
  600.     private function resolveServices($value$file$isParameter false)
  601.     {
  602.         if ($value instanceof TaggedValue) {
  603.             $argument $value->getValue();
  604.             if ('iterator' === $value->getTag()) {
  605.                 if (!\is_array($argument)) {
  606.                     throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts sequences in "%s".'$file));
  607.                 }
  608.                 $argument $this->resolveServices($argument$file$isParameter);
  609.                 try {
  610.                     return new IteratorArgument($argument);
  611.                 } catch (InvalidArgumentException $e) {
  612.                     throw new InvalidArgumentException(sprintf('"!iterator" tag only accepts arrays of "@service" references in "%s".'$file));
  613.                 }
  614.             }
  615.             if ('tagged' === $value->getTag()) {
  616.                 if (!\is_string($argument) || !$argument) {
  617.                     throw new InvalidArgumentException(sprintf('"!tagged" tag only accepts non empty string in "%s".'$file));
  618.                 }
  619.                 return new TaggedIteratorArgument($argument);
  620.             }
  621.             if ('service' === $value->getTag()) {
  622.                 if ($isParameter) {
  623.                     throw new InvalidArgumentException(sprintf('Using an anonymous service in a parameter is not allowed in "%s".'$file));
  624.                 }
  625.                 $isLoadingInstanceof $this->isLoadingInstanceof;
  626.                 $this->isLoadingInstanceof false;
  627.                 $instanceof $this->instanceof;
  628.                 $this->instanceof = [];
  629.                 $id sprintf('%d_%s', ++$this->anonymousServicesCountpreg_replace('/^.*\\\\/''', isset($argument['class']) ? $argument['class'] : '').$this->anonymousServicesSuffix);
  630.                 $this->parseDefinition($id$argument$file, []);
  631.                 if (!$this->container->hasDefinition($id)) {
  632.                     throw new InvalidArgumentException(sprintf('Creating an alias using the tag "!service" is not allowed in "%s".'$file));
  633.                 }
  634.                 $this->container->getDefinition($id)->setPublic(false);
  635.                 $this->isLoadingInstanceof $isLoadingInstanceof;
  636.                 $this->instanceof $instanceof;
  637.                 return new Reference($id);
  638.             }
  639.             throw new InvalidArgumentException(sprintf('Unsupported tag "!%s".'$value->getTag()));
  640.         }
  641.         if (\is_array($value)) {
  642.             foreach ($value as $k => $v) {
  643.                 $value[$k] = $this->resolveServices($v$file$isParameter);
  644.             }
  645.         } elseif (\is_string($value) && === strpos($value'@=')) {
  646.             if (!class_exists(Expression::class)) {
  647.                 throw new \LogicException(sprintf('The "@=" expression syntax cannot be used without the ExpressionLanguage component. Try running "composer require symfony/expression-language".'));
  648.             }
  649.             return new Expression(substr($value2));
  650.         } elseif (\is_string($value) && === strpos($value'@')) {
  651.             if (=== strpos($value'@@')) {
  652.                 $value substr($value1);
  653.                 $invalidBehavior null;
  654.             } elseif (=== strpos($value'@!')) {
  655.                 $value substr($value2);
  656.                 $invalidBehavior ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE;
  657.             } elseif (=== strpos($value'@?')) {
  658.                 $value substr($value2);
  659.                 $invalidBehavior ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  660.             } else {
  661.                 $value substr($value1);
  662.                 $invalidBehavior ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  663.             }
  664.             if ('=' === substr($value, -1)) {
  665.                 @trigger_error(sprintf('The "=" suffix that used to disable strict references in Symfony 2.x is deprecated since Symfony 3.3 and will be unsupported in 4.0. Remove it in "%s".'$value), \E_USER_DEPRECATED);
  666.                 $value substr($value0, -1);
  667.             }
  668.             if (null !== $invalidBehavior) {
  669.                 $value = new Reference($value$invalidBehavior);
  670.             }
  671.         }
  672.         return $value;
  673.     }
  674.     /**
  675.      * Loads from Extensions.
  676.      */
  677.     private function loadFromExtensions(array $content)
  678.     {
  679.         foreach ($content as $namespace => $values) {
  680.             if (\in_array($namespace, ['imports''parameters''services'])) {
  681.                 continue;
  682.             }
  683.             if (!\is_array($values) && null !== $values) {
  684.                 $values = [];
  685.             }
  686.             $this->container->loadFromExtension($namespace$values);
  687.         }
  688.     }
  689.     /**
  690.      * Checks the keywords used to define a service.
  691.      *
  692.      * @param string $id         The service name
  693.      * @param array  $definition The service definition to check
  694.      * @param string $file       The loaded YAML file
  695.      */
  696.     private function checkDefinition($id, array $definition$file)
  697.     {
  698.         if ($throw $this->isLoadingInstanceof) {
  699.             $keywords self::$instanceofKeywords;
  700.         } elseif ($throw = (isset($definition['resource']) || isset($definition['namespace']))) {
  701.             $keywords self::$prototypeKeywords;
  702.         } else {
  703.             $keywords self::$serviceKeywords;
  704.         }
  705.         foreach ($definition as $key => $value) {
  706.             if (!isset($keywords[$key])) {
  707.                 if ($throw) {
  708.                     throw new InvalidArgumentException(sprintf('The configuration key "%s" is unsupported for definition "%s" in "%s". Allowed configuration keys are "%s".'$key$id$fileimplode('", "'$keywords)));
  709.                 }
  710.                 @trigger_error(sprintf('The configuration key "%s" is unsupported for service definition "%s" in "%s". Allowed configuration keys are "%s". The YamlFileLoader object will raise an exception instead in Symfony 4.0 when detecting an unsupported service configuration key.'$key$id$fileimplode('", "'$keywords)), \E_USER_DEPRECATED);
  711.             }
  712.         }
  713.     }
  714. }