4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\DependencyInjection;
14 use Symfony\Component\DependencyInjection\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
17 use Symfony\Component\DependencyInjection\Reference;
20 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21 * @author Robin Chalas <robin.chalas@gmail.com>
23 class AddConstraintValidatorsPass implements CompilerPassInterface
25 private $validatorFactoryServiceId;
26 private $constraintValidatorTag;
28 public function __construct($validatorFactoryServiceId = 'validator.validator_factory', $constraintValidatorTag = 'validator.constraint_validator')
30 $this->validatorFactoryServiceId = $validatorFactoryServiceId;
31 $this->constraintValidatorTag = $constraintValidatorTag;
34 public function process(ContainerBuilder $container)
36 if (!$container->hasDefinition($this->validatorFactoryServiceId)) {
40 $validators = array();
41 foreach ($container->findTaggedServiceIds($this->constraintValidatorTag, true) as $id => $attributes) {
42 $definition = $container->getDefinition($id);
44 if (isset($attributes[0]['alias'])) {
45 $validators[$attributes[0]['alias']] = new Reference($id);
48 $validators[$definition->getClass()] = new Reference($id);
52 ->getDefinition($this->validatorFactoryServiceId)
53 ->replaceArgument(0, ServiceLocatorTagPass::register($container, $validators))