Skip to content

Commit 4952a6f

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: fixed CS [BrowserKit] Corrected HTTP_HOST logic #15398 resolve aliases in factories resolve aliases in factory services Remove invalid CSS white-space value Fix FileSystem tests on Windows [Form] ArrayChoiceList can now deal with a null in choices
2 parents 38d51ec + 2a68260 commit 4952a6f

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

Compiler/ResolveReferencesToAliasesPass.php

Lines changed: 30 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,11 @@ 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()));
47+
48+
if (null !== $factoryService = $definition->getFactoryService(false)) {
49+
$definition->setFactoryService($this->processFactoryService($factoryService));
50+
}
4551
}
4652

4753
foreach ($container->getAliases() as $id => $alias) {
@@ -76,6 +82,30 @@ private function processArguments(array $arguments)
7682
return $arguments;
7783
}
7884

85+
private function processFactoryService($factoryService)
86+
{
87+
if (null === $factoryService) {
88+
return;
89+
}
90+
91+
return $this->getDefinitionId($factoryService);
92+
}
93+
94+
private function processFactory($factory)
95+
{
96+
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
97+
return $factory;
98+
}
99+
100+
$defId = $this->getDefinitionId($id = (string) $factory[0]);
101+
102+
if ($defId !== $id) {
103+
$factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior(), $factory[0]->isStrict(false));
104+
}
105+
106+
return $factory;
107+
}
108+
79109
/**
80110
* Resolves an alias into a definition id.
81111
*

Tests/Compiler/ResolveReferencesToAliasesPassTest.php

Lines changed: 41 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,45 @@ 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+
85+
/**
86+
* @group legacy
87+
*/
88+
public function testResolveFactoryService()
89+
{
90+
$container = new ContainerBuilder();
91+
$container->register('factory', 'Factory');
92+
$container->setAlias('factory_alias', new Alias('factory'));
93+
$foo = new Definition();
94+
$foo->setFactoryService('factory_alias');
95+
$foo->setFactoryMethod('createFoo');
96+
$container->setDefinition('foo', $foo);
97+
98+
$this->process($container);
99+
100+
$this->assertSame('factory', $foo->getFactoryService());
101+
}
102+
62103
protected function process(ContainerBuilder $container)
63104
{
64105
$pass = new ResolveReferencesToAliasesPass();

0 commit comments

Comments
 (0)