8000 bug #17866 [DependencyInjection] replace alias in factories (xabbuh) · symfony/symfony@6190058 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6190058

Browse files
committed
bug #17866 [DependencyInjection] replace alias in factories (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [DependencyInjection] replace alias in factories | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17864 | License | MIT | Doc PR | Commits ------- b43b79b [DependencyInjection] replace alias in factories
2 parents 6bb1c6d + b43b79b commit 6190058

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ private function updateReferences($container, $currentId, $newId)
9797
);
9898

9999
$definition->setFactoryService($this->updateFactoryServiceReference($definition->getFactoryService(), $currentId, $newId));
100+
$definition->setFactory($this->updateFactoryReference($definition->getFactory(), $currentId, $newId));
100101
}
101102
}
102103

@@ -133,4 +134,17 @@ private function updateFactoryServiceReference($factoryService, $currentId, $new
133134

134135
return $currentId === $factoryService ? $newId : $currentId;
135136
}
137+
138+
private function updateFactoryReference($factory, $currentId, $newId)
139+
{
140+
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
141+
return $factory;
142+
}
143+
144+
if ($currentId === (string) $factory[0]) {
145+
$factory[0] = new Reference($newId, $factory[0]->getInvalidBehavior());
146+
}
147+
148+
return $factory;
149+
}
136150
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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
{
@@ -25,6 +26,8 @@ public function testProcess()
2526
$aDefinition->setFactoryService('b');
2627
$aDefinition->setFactoryMethod('createA');
2728

29+
$aDefinition->setFactory(array(new Reference('b'), 'createA'));
30+
2831
$bDefinition = new Definition('\stdClass');
2932
$bDefinition->setPublic(false);
3033
$container->setDefinition('b', $bDefinition);
@@ -41,7 +44,11 @@ public function testProcess()
4144
$container->has('b_alias') && !$container->hasAlias('b_alias'),
4245
'->process() replaces alias to actual.'
4346
);
47+
4448
$this->assertSame('b_alias', $aDefinition->getFactoryService());
49+
50+
$resolvedFactory = $aDefinition->getFactory();
51+
$this->assertSame('b_alias', (string) $resolvedFactory[0]);
4552
}
4653

4754
/**

0 commit comments

Comments
 (0)
0