DefaultsConfigurator.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. public const FACTORY = 'defaults';
  19. use Traits\AutoconfigureTrait;
  20. use Traits\AutowireTrait;
  21. use Traits\BindTrait;
  22. use Traits\PublicTrait;
  23. private $path;
  24. public function __construct(ServicesConfigurator $parent, Definition $definition, string $path = null)
  25. {
  26. parent::__construct($parent, $definition, null, []);
  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 = []): self
  37. {
  38. if ('' === $name) {
  39. throw new InvalidArgumentException('The tag name in "_defaults" must be a non-empty string.');
  40. }
  41. foreach ($attributes as $attribute => $value) {
  42. if (null !== $value && !is_scalar($value)) {
  43. throw new InvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type.', $name, $attribute));
  44. }
  45. }
  46. $this->definition->addTag($name, $attributes);
  47. return $this;
  48. }
  49. /**
  50. * Defines an instanceof-conditional to be applied to following service definitions.
  51. */
  52. final public function instanceof(string $fqcn): InstanceofConfigurator
  53. {
  54. return $this->parent->instanceof($fqcn);
  55. }
  56. }