bug #34776 [DI] fix resolving bindings for named TypedReference (nicolas-grekas)
This PR was merged into the 4.3 branch.
Discussion
----------
[DI] fix resolving bindings for named TypedReference
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
As spotted in https://github.com/symfony/symfony/pull/34769#issuecomment-561064156
Commits
-------
62c227e368
[DI] fix resolving bindings for named TypedReference
This commit is contained in:
commit
302243dd39
@ -95,6 +95,11 @@ class ResolveBindingsPass extends AbstractRecursivePass
|
||||
if ($value instanceof TypedReference && $value->getType() === (string) $value) {
|
||||
// Already checked
|
||||
$bindings = $this->container->getDefinition($this->currentId)->getBindings();
|
||||
$name = $value->getName();
|
||||
|
||||
if (isset($name, $bindings[$name = $value.' $'.$name])) {
|
||||
return $this->getBindingValue($bindings[$name]);
|
||||
}
|
||||
|
||||
if (isset($bindings[$value->getType()])) {
|
||||
return $this->getBindingValue($bindings[$value->getType()]);
|
||||
|
@ -80,7 +80,10 @@ class ResolveBindingsPassTest extends TestCase
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
|
||||
$bindings = [CaseSensitiveClass::class => new BoundArgument(new Reference('foo'))];
|
||||
$bindings = [
|
||||
CaseSensitiveClass::class => new BoundArgument(new Reference('foo')),
|
||||
CaseSensitiveClass::class.' $c' => new BoundArgument(new Reference('bar')),
|
||||
];
|
||||
|
||||
// Explicit service id
|
||||
$definition1 = $container->register('def1', NamedArgumentsDummy::class);
|
||||
@ -91,11 +94,16 @@ class ResolveBindingsPassTest extends TestCase
|
||||
$definition2->addArgument(new TypedReference(CaseSensitiveClass::class, CaseSensitiveClass::class));
|
||||
$definition2->setBindings($bindings);
|
||||
|
||||
$definition3 = $container->register('def3', NamedArgumentsDummy::class);
|
||||
$definition3->addArgument(new TypedReference(CaseSensitiveClass::class, CaseSensitiveClass::class, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, 'c'));
|
||||
$definition3->setBindings($bindings);
|
||||
|
||||
$pass = new ResolveBindingsPass();
|
||||
$pass->process($container);
|
||||
|
||||
$this->assertEquals([$typedRef], $container->getDefinition('def1')->getArguments());
|
||||
$this->assertEquals([new Reference('foo')], $container->getDefinition('def2')->getArguments());
|
||||
$this->assertEquals([new Reference('bar')], $container->getDefinition('def3')->getArguments());
|
||||
}
|
||||
|
||||
public function testScalarSetter()
|
||||
|
Reference in New Issue
Block a user