AddConstraintValidatorsPass.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Bundle\FrameworkBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  14. class AddConstraintValidatorsPass implements CompilerPassInterface
  15. {
  16. public function process(ContainerBuilder $container)
  17. {
  18. if (!$container->hasDefinition('validator.validator_factory')) {
  19. return;
  20. }
  21. $validators = array();
  22. foreach ($container->findTaggedServiceIds('validator.constraint_validator') as $id => $attributes) {
  23. if (isset($attributes[0]['alias'])) {
  24. $validators[$attributes[0]['alias']] = $id;
  25. }
  26. $definition = $container->getDefinition($id);
  27. if (!$definition->isPublic()) {
  28. throw new InvalidArgumentException(sprintf('The service "%s" must be public as it can be lazy-loaded.', $id));
  29. }
  30. if ($definition->isAbstract()) {
  31. throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as it can be lazy-loaded.', $id));
  32. }
  33. $validators[$definition->getClass()] = $id;
  34. }
  35. $container->getDefinition('validator.validator_factory')->replaceArgument(1, $validators);
  36. }
  37. }