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

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