|
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\Reference; |
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17 |
|
18 /** |
|
19 * Replaces all references to aliases with references to the actual service. |
|
20 * |
|
21 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
22 */ |
|
23 class ResolveReferencesToAliasesPass implements CompilerPassInterface |
|
24 { |
|
25 private $container; |
|
26 |
|
27 /** |
|
28 * Processes the ContainerBuilder to replace references to aliases with actual service references. |
|
29 * |
|
30 * @param ContainerBuilder $container |
|
31 */ |
|
32 public function process(ContainerBuilder $container) |
|
33 { |
|
34 $this->container = $container; |
|
35 |
|
36 foreach ($container->getDefinitions() as $definition) { |
|
37 if ($definition->isSynthetic() || $definition->isAbstract()) { |
|
38 continue; |
|
39 } |
|
40 |
|
41 $definition->setArguments($this->processArguments($definition->getArguments())); |
|
42 $definition->setMethodCalls($this->processArguments($definition->getMethodCalls())); |
|
43 $definition->setProperties($this->processArguments($definition->getProperties())); |
|
44 } |
|
45 |
|
46 foreach ($container->getAliases() as $id => $alias) { |
|
47 $aliasId = (string) $alias; |
|
48 if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) { |
|
49 $container->setAlias($id, new Alias($defId, $alias->isPublic())); |
|
50 } |
|
51 } |
|
52 } |
|
53 |
|
54 /** |
|
55 * Processes the arguments to replace aliases |
|
56 * |
|
57 * @param array $arguments An array of References |
|
58 * @return array An array of References |
|
59 */ |
|
60 private function processArguments(array $arguments) |
|
61 { |
|
62 foreach ($arguments as $k => $argument) { |
|
63 if (is_array($argument)) { |
|
64 $arguments[$k] = $this->processArguments($argument); |
|
65 } elseif ($argument instanceof Reference) { |
|
66 $defId = $this->getDefinitionId($id = (string) $argument); |
|
67 |
|
68 if ($defId !== $id) { |
|
69 $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict()); |
|
70 } |
|
71 } |
|
72 } |
|
73 |
|
74 return $arguments; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Resolve an alias into a definition id |
|
79 * |
|
80 * @param string $id The definition or alias id to resolve |
|
81 * @return string The definition id with aliases resolved |
|
82 */ |
|
83 private function getDefinitionId($id) |
|
84 { |
|
85 while ($this->container->hasAlias($id)) { |
|
86 $id = (string) $this->container->getAlias($id); |
|
87 } |
|
88 |
|
89 return $id; |
|
90 } |
|
91 } |