Skip to content

Commit 049fb54

Browse files
[DI] Resolve nested refs to aliases
1 parent 1a9e402 commit 049fb54

File tree

1 file changed

+16
-59
lines changed

1 file changed

+16
-59
lines changed

Compiler/ResolveReferencesToAliasesPass.php

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14-
use Symfony\Component\DependencyInjection\Alias;
15-
use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
1614
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1715
use Symfony\Component\DependencyInjection\Reference;
1816
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -22,97 +20,56 @@
2220
*
2321
* @author Johannes M. Schmitt <[email protected]>
2422
*/
25-
class ResolveReferencesToAliasesPass implements CompilerPassInterface
23+
class ResolveReferencesToAliasesPass extends AbstractRecursivePass
2624
{
27-
private $container;
28-
2925
/**
30-
* Processes the ContainerBuilder to replace references to aliases with actual service references.
31-
*
32-
* @param ContainerBuilder $container
26+
* {@inheritdoc}
3327
*/
3428
public function process(ContainerBuilder $container)
3529
{
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);
5031

5132
foreach ($container->getAliases() as $id => $alias) {
5233
$aliasId = (string) $alias;
53-
if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
34+
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
5435
$container->setAlias($id, $defId)->setPublic($alias->isPublic() && !$alias->isPrivate())->setPrivate($alias->isPrivate());
5536
}
5637
}
5738
}
5839

5940
/**
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}
6542
*/
66-
private function processArguments(array $arguments)
43+
protected function processValue($value, $isRoot = false)
6744
{
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);
7547

76-
if ($defId !== $id) {
77-
$arguments[$k] = new Reference($defId, $argument->getInvalidBehavior());
78-
}
48+
if ($defId !== $id) {
49+
return new Reference($defId, $value->getInvalidBehavior());
7950
}
8051
}
8152

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);
9854
}
9955

10056
/**
10157
* Resolves an alias into a definition id.
10258
*
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
10461
*
10562
* @return string The definition id with aliases resolved
10663
*/
107-
private function getDefinitionId($id)
64+
private function getDefinitionId($id, $container)
10865
{
10966
$seen = array();
110-
while ($this->container->hasAlias($id)) {
67+
while ($container->hasAlias($id)) {
11168
if (isset($seen[$id])) {
11269
throw new ServiceCircularReferenceException($id, array_keys($seen));
11370
}
11471
$seen[$id] = true;
115-
$id = (string) $this->container->getAlias($id);
72+
$id = (string) $container->getAlias($id);
11673
}
11774

11875
return $id;

0 commit comments

Comments
 (0)