Fix bug in style changes for the Use cases on the live site.
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ResolveReferencesToAliasesPass.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\DependencyInjection\Compiler;
13
14 use Symfony\Component\DependencyInjection\Alias;
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
18
19 /**
20  * Replaces all references to aliases with references to the actual service.
21  *
22  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
23  */
24 class ResolveReferencesToAliasesPass implements CompilerPassInterface
25 {
26     private $container;
27
28     /**
29      * Processes the ContainerBuilder to replace references to aliases with actual service references.
30      *
31      * @param ContainerBuilder $container
32      */
33     public function process(ContainerBuilder $container)
34     {
35         $this->container = $container;
36
37         foreach ($container->getDefinitions() as $definition) {
38             if ($definition->isSynthetic() || $definition->isAbstract()) {
39                 continue;
40             }
41
42             $definition->setArguments($this->processArguments($definition->getArguments()));
43             $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
44             $definition->setProperties($this->processArguments($definition->getProperties()));
45             $definition->setFactory($this->processFactory($definition->getFactory()));
46             $definition->setFactoryService($this->processFactoryService($definition->getFactoryService(false)), false);
47         }
48
49         foreach ($container->getAliases() as $id => $alias) {
50             $aliasId = (string) $alias;
51             if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
52                 $container->setAlias($id, new Alias($defId, $alias->isPublic()));
53             }
54         }
55     }
56
57     /**
58      * Processes the arguments to replace aliases.
59      *
60      * @param array $arguments An array of References
61      *
62      * @return array An array of References
63      */
64     private function processArguments(array $arguments)
65     {
66         foreach ($arguments as $k => $argument) {
67             if (is_array($argument)) {
68                 $arguments[$k] = $this->processArguments($argument);
69             } elseif ($argument instanceof Reference) {
70                 $defId = $this->getDefinitionId($id = (string) $argument);
71
72                 if ($defId !== $id) {
73                     $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict(false));
74                 }
75             }
76         }
77
78         return $arguments;
79     }
80
81     private function processFactoryService($factoryService)
82     {
83         if (null === $factoryService) {
84             return;
85         }
86
87         return $this->getDefinitionId($factoryService);
88     }
89
90     private function processFactory($factory)
91     {
92         if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
93             return $factory;
94         }
95
96         $defId = $this->getDefinitionId($id = (string) $factory[0]);
97
98         if ($defId !== $id) {
99             $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict(false));
100         }
101
102         return $factory;
103     }
104
105     /**
106      * Resolves an alias into a definition id.
107      *
108      * @param string $id The definition or alias id to resolve
109      *
110      * @return string The definition id with aliases resolved
111      */
112     private function getDefinitionId($id)
113     {
114         $seen = array();
115         while ($this->container->hasAlias($id)) {
116             if (isset($seen[$id])) {
117                 throw new ServiceCircularReferenceException($id, array_keys($seen));
118             }
119             $seen[$id] = true;
120             $id = (string) $this->container->getAlias($id);
121         }
122
123         return $id;
124     }
125 }