vendor/symfony/dependency-injection/Loader/Configurator/DefaultsConfigurator.php line 63

  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\Configurator;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  13. /**
  14.  * @author Nicolas Grekas <p@tchwork.com>
  15.  */
  16. class DefaultsConfigurator extends AbstractServiceConfigurator
  17. {
  18.     use Traits\AutoconfigureTrait;
  19.     use Traits\AutowireTrait;
  20.     use Traits\BindTrait;
  21.     use Traits\PublicTrait;
  22.     public const FACTORY 'defaults';
  23.     private ?string $path;
  24.     public function __construct(ServicesConfigurator $parentDefinition $definitionstring $path null)
  25.     {
  26.         parent::__construct($parent$definitionnull, []);
  27.         $this->path $path;
  28.     }
  29.     /**
  30.      * Adds a tag for this definition.
  31.      *
  32.      * @return $this
  33.      *
  34.      * @throws InvalidArgumentException when an invalid tag name or attribute is provided
  35.      */
  36.     final public function tag(string $name, array $attributes = []): static
  37.     {
  38.         if ('' === $name) {
  39.             throw new InvalidArgumentException('The tag name in "_defaults" must be a non-empty string.');
  40.         }
  41.         $this->validateAttributes($name$attributes);
  42.         $this->definition->addTag($name$attributes);
  43.         return $this;
  44.     }
  45.     /**
  46.      * Defines an instanceof-conditional to be applied to following service definitions.
  47.      */
  48.     final public function instanceof(string $fqcn): InstanceofConfigurator
  49.     {
  50.         return $this->parent->instanceof($fqcn);
  51.     }
  52.     private function validateAttributes(string $tag, array $attributes, array $path = []): void
  53.     {
  54.         foreach ($attributes as $name => $value) {
  55.             if (\is_array($value)) {
  56.                 $this->validateAttributes($tag$value, [...$path$name]);
  57.             } elseif (!\is_scalar($value ?? '')) {
  58.                 $name implode('.', [...$path$name]);
  59.                 throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type or an array of scalar-type.'$tag$name));
  60.             }
  61.         }
  62.     }
  63. }