AddValidatorInitializersPass.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\Validator\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Robin Chalas <robin.chalas@gmail.com>
  17. */
  18. class AddValidatorInitializersPass implements CompilerPassInterface
  19. {
  20. private $builderService;
  21. private $initializerTag;
  22. public function __construct(string $builderService = 'validator.builder', string $initializerTag = 'validator.initializer')
  23. {
  24. $this->builderService = $builderService;
  25. $this->initializerTag = $initializerTag;
  26. }
  27. public function process(ContainerBuilder $container)
  28. {
  29. if (!$container->hasDefinition($this->builderService)) {
  30. return;
  31. }
  32. $initializers = [];
  33. foreach ($container->findTaggedServiceIds($this->initializerTag, true) as $id => $attributes) {
  34. $initializers[] = new Reference($id);
  35. }
  36. $container->getDefinition($this->builderService)->addMethodCall('addObjectInitializers', [$initializers]);
  37. }
  38. }