207a1ad49d487ff7eaf1d3444a659aca8f18f597
[yaffs-website] / vendor / symfony / validator / DependencyInjection / AddConstraintValidatorsPass.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Validator\DependencyInjection;
13
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;
18
19 /**
20  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
21  * @author Robin Chalas <robin.chalas@gmail.com>
22  */
23 class AddConstraintValidatorsPass implements CompilerPassInterface
24 {
25     private $validatorFactoryServiceId;
26     private $constraintValidatorTag;
27
28     public function __construct($validatorFactoryServiceId = 'validator.validator_factory', $constraintValidatorTag = 'validator.constraint_validator')
29     {
30         $this->validatorFactoryServiceId = $validatorFactoryServiceId;
31         $this->constraintValidatorTag = $constraintValidatorTag;
32     }
33
34     public function process(ContainerBuilder $container)
35     {
36         if (!$container->hasDefinition($this->validatorFactoryServiceId)) {
37             return;
38         }
39
40         $validators = array();
41         foreach ($container->findTaggedServiceIds($this->constraintValidatorTag, true) as $id => $attributes) {
42             $definition = $container->getDefinition($id);
43
44             if (isset($attributes[0]['alias'])) {
45                 $validators[$attributes[0]['alias']] = new Reference($id);
46             }
47
48             $validators[$definition->getClass()] = new Reference($id);
49         }
50
51         $container
52             ->getDefinition($this->validatorFactoryServiceId)
53             ->replaceArgument(0, ServiceLocatorTagPass::register($container, $validators))
54         ;
55     }
56 }