|
11 | 11 |
|
12 | 12 | namespace Symfony\Component\DependencyInjection\Compiler; |
13 | 13 |
|
14 | | -use Symfony\Component\DependencyInjection\Alias; |
15 | | -use Symfony\Component\DependencyInjection\Argument\ArgumentInterface; |
16 | 14 | use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException; |
17 | 15 | use Symfony\Component\DependencyInjection\Reference; |
18 | 16 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
22 | 20 | * |
23 | 21 | * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
24 | 22 | */ |
25 | | -class ResolveReferencesToAliasesPass implements CompilerPassInterface |
| 23 | +class ResolveReferencesToAliasesPass extends AbstractRecursivePass |
26 | 24 | { |
27 | | - private $container; |
28 | | - |
29 | 25 | /** |
30 | | - * Processes the ContainerBuilder to replace references to aliases with actual service references. |
31 | | - * |
32 | | - * @param ContainerBuilder $container |
| 26 | + * {@inheritdoc} |
33 | 27 | */ |
34 | 28 | public function process(ContainerBuilder $container) |
35 | 29 | { |
36 | | - $this->container = $container; |
37 | | - |
38 | | - foreach ($container->getDefinitions() as $definition) { |
39 | | - if ($definition->isSynthetic() || $definition->isAbstract()) { |
40 | | - continue; |
41 | | - } |
42 | | - |
43 | | - $definition->setArguments($this->processArguments($definition->getArguments())); |
44 | | - $definition->setMethodCalls($this->processArguments($definition->getMethodCalls())); |
45 | | - $definition->setProperties($this->processArguments($definition->getProperties())); |
46 | | - if (isset($definition->getChanges()['factory'])) { |
47 | | - $definition->setFactory($this->processFactory($definition->getFactory())); |
48 | | - } |
49 | | - } |
| 30 | + parent::process($container); |
50 | 31 |
|
51 | 32 | foreach ($container->getAliases() as $id => $alias) { |
52 | 33 | $aliasId = (string) $alias; |
53 | | - if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) { |
| 34 | + if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) { |
54 | 35 | $container->setAlias($id, $defId)->setPublic($alias->isPublic() && !$alias->isPrivate())->setPrivate($alias->isPrivate()); |
55 | 36 | } |
56 | 37 | } |
57 | 38 | } |
58 | 39 |
|
59 | 40 | /** |
60 | | - * Processes the arguments to replace aliases. |
61 | | - * |
62 | | - * @param array $arguments An array of References |
63 | | - * |
64 | | - * @return array An array of References |
| 41 | + * {@inheritdoc} |
65 | 42 | */ |
66 | | - private function processArguments(array $arguments) |
| 43 | + protected function processValue($value, $isRoot = false) |
67 | 44 | { |
68 | | - foreach ($arguments as $k => $argument) { |
69 | | - if (is_array($argument)) { |
70 | | - $arguments[$k] = $this->processArguments($argument); |
71 | | - } elseif ($argument instanceof ArgumentInterface) { |
72 | | - $argument->setValues($this->processArguments($argument->getValues())); |
73 | | - } elseif ($argument instanceof Reference) { |
74 | | - $defId = $this->getDefinitionId($id = (string) $argument); |
| 45 | + if ($value instanceof Reference) { |
| 46 | + $defId = $this->getDefinitionId($id = (string) $value, $this->container); |
75 | 47 |
|
76 | | - if ($defId !== $id) { |
77 | | - $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior()); |
78 | | - } |
| 48 | + if ($defId !== $id) { |
| 49 | + return new Reference($defId, $value->getInvalidBehavior()); |
79 | 50 | } |
80 | 51 | } |
81 | 52 |
|
82 | | - return $arguments; |
83 | | - } |
84 | | - |
85 | | - private function processFactory($factory) |
86 | | - { |
87 | | - if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) { |
88 | | - return $factory; |
89 | | - } |
90 | | - |
91 | | - $defId = $this->getDefinitionId($id = (string) $factory[0]); |
92 | | - |
93 | | - if ($defId !== $id) { |
94 | | - $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior()); |
95 | | - } |
96 | | - |
97 | | - return $factory; |
| 53 | + return parent::processValue($value); |
98 | 54 | } |
99 | 55 |
|
100 | 56 | /** |
101 | 57 | * Resolves an alias into a definition id. |
102 | 58 | * |
103 | | - * @param string $id The definition or alias id to resolve |
| 59 | + * @param string $id The definition or alias id to resolve |
| 60 | + * @param ContainerBuilder $container |
104 | 61 | * |
105 | 62 | * @return string The definition id with aliases resolved |
106 | 63 | */ |
107 | | - private function getDefinitionId($id) |
| 64 | + private function getDefinitionId($id, $container) |
108 | 65 | { |
109 | 66 | $seen = array(); |
110 | | - while ($this->container->hasAlias($id)) { |
| 67 | + while ($container->hasAlias($id)) { |
111 | 68 | if (isset($seen[$id])) { |
112 | 69 | throw new ServiceCircularReferenceException($id, array_keys($seen)); |
113 | 70 | } |
114 | 71 | $seen[$id] = true; |
115 | | - $id = (string) $this->container->getAlias($id); |
| 72 | + $id = (string) $container->getAlias($id); |
116 | 73 | } |
117 | 74 |
|
118 | 75 | return $id; |
|
0 commit comments