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

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