vendor/symfony/dependency-injection/Loader/YamlFileLoader.php line 180

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