8000 bug #17554 [DependencyInjection] resolve aliases in factories (xabbuh) · symfony/symfony@da16d15 · GitHub
[go: up one dir, main page]

Skip to content

Commit da16d15

Browse files
committed
bug #17554 [DependencyInjection] resolve aliases in factories (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [DependencyInjection] resolve aliases in factories | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | FriendsOfSymfony/FOSUserBundle#2048 | License | MIT | Doc PR | Commits ------- fde10e7 resolve aliases in factories
2 parents 8967076 + fde10e7 commit da16d15

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\Alias;
15+
use Symfony\Component\DependencyInjection\Definition;
1516
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1617
use Symfony\Component\DependencyInjection\Reference;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -42,6 +43,7 @@ public function process(ContainerBuilder $container)
4243
$definition->setArguments($this->processArguments($definition->getArguments()));
4344
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
4445
$definition->setProperties($this->processArguments($definition->getProperties()));
46+
$definition->setFactory($this->processFactory($definition->getFactory()));
4547
}
4648

4749
foreach ($container->getAliases() as $id => $alias) {
@@ -76,6 +78,21 @@ private function processArguments(array $arguments)
7678
return $arguments;
7779
}
7880

81+
private function processFactory($factory)
82+
{
83+
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
84+
return $factory;
85+
}
86+
87+
$defId = $this->getDefinitionId($id = (string) $factory[0]);
88+
89+
if ($defId !== $id) {
90+
$factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict());
91+
}
92+
93+
return $factory;
94+
}
95+
7996
/**
8097
* Resolves an alias into a definition id.
8198
*

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

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

1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

14+
use Symfony\Component\DependencyInjection\Alias;
15+
use Symfony\Component\DependencyInjection\Definition;
1416
use Symfony\Component\DependencyInjection\Reference;
1517
use Symfony\Component\DependencyInjection\Compiler\ResolveReferencesToAliasesPass;
1618
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -59,6 +61,27 @@ public function testAliasCircularReference()
5961
$this->process($container);
6062
}
6163

64+
public function testResolveFactory()
65+
{
66+
$container = new ContainerBuilder();
67+
$container->register('factory', 'Factory');
68+
$container->setAlias('factory_alias', new Alias('factory'));
69+
$foo = new Definition();
70+
$foo->setFactory(array(new Reference('factory_alias'), 'createFoo'));
71+
$container->setDefinition('foo', $foo);
72+
$bar = new Definition();
73+
$bar->setFactory(array('Factory', 'createFoo'));
74+
$container->setDefinition('bar', $bar);
75+
76+
$this->process($container);
77+
78+
$resolvedFooFactory = $container->getDefinition('foo')->getFactory();
79+
$resolvedBarFactory = $container->getDefinition('bar')->getFactory();
80+
81+
$this->assertSame('factory', (string) $resolvedFooFactory[0]);
82+
$this->assertSame('Factory', (string) $resolvedBarFactory[0]);
83+
}
84+
6285
protected function process(ContainerBuilder $container)
6386
{
6487
$pass = new ResolveReferencesToAliasesPass();

0 commit comments

Comments
 (0)
0