Skip to content

Commit b43b79b

Browse files
committed
[DependencyInjection] replace alias in factories
1 parent fd827b3 commit b43b79b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ReplaceAliasByActualDefinitionPass.php

+15
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ private function updateReferences($container, $currentId, $newId)
9595
$definition->setProperties(
9696
$this->updateArgumentReferences($definition->getProperties(), $currentId, $newId)
9797
);
98+
99+
$definition->setFactory($this->updateFactoryReference($definition->getFactory(), $currentId, $newId));
98100
}
99101
}
100102

@@ -122,4 +124,17 @@ private function updateArgumentReferences(array $arguments, $currentId, $newId)
122124

123125
return $arguments;
124126
}
127+
128+
private function updateFactoryReference($factory, $currentId, $newId)
129+
{
130+
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
131+
return $factory;
132+
}
133+
134+
if ($currentId === (string) $factory[0]) {
135+
$factory[0] = new Reference($newId, $factory[0]->getInvalidBehavior());
136+
}
137+
138+
return $factory;
139+
}
125140
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ReplaceAliasByActualDefinitionPassTest.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
use Symfony\Component\DependencyInjection\Compiler\ReplaceAliasByActualDefinitionPass;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Reference;
1718

1819
class ReplaceAliasByActualDefinitionPassTest extends \PHPUnit_Framework_TestCase
1920
{
2021
public function testProcess()
2122
{
2223
$container = new ContainerBuilder();
2324

24-
$container->register('a', '\stdClass');
25+
$aDefinition = $container->register('a', '\stdClass');
26+
$aDefinition->setFactory(array(new Reference('b'), 'createA'));
2527

2628
$bDefinition = new Definition('\stdClass');
2729
$bDefinition->setPublic(false);
@@ -39,6 +41,9 @@ public function testProcess()
3941
$container->has('b_alias') && !$container->hasAlias('b_alias'),
4042
'->process() replaces alias to actual.'
4143
);
44+
45+
$resolvedFactory = $aDefinition->getFactory();
46+
$this->assertSame('b_alias', (string) $resolvedFactory[0]);
4247
}
4348

4449
/**

0 commit comments

Comments
 (0)