Skip to content

Commit 302243d

Browse files
committed
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 #34769 (comment) Commits ------- 62c227e [DI] fix resolving bindings for named TypedReference
2 parents 6611ae6 + 62c227e commit 302243d

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ protected function processValue($value, $isRoot = false)
9595
if ($value instanceof TypedReference && $value->getType() === (string) $value) {
9696
// Already checked
9797
$bindings = $this->container->getDefinition($this->currentId)->getBindings();
98+
$name = $value->getName();
99+
100+
if (isset($name, $bindings[$name = $value.' $'.$name])) {
101+
return $this->getBindingValue($bindings[$name]);
102+
}
98103

99104
if (isset($bindings[$value->getType()])) {
100105
return $this->getBindingValue($bindings[$value->getType()]);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ public function testTypedReferenceSupport()
8080
{
8181
$container = new ContainerBuilder();
8282

83-
$bindings = [CaseSensitiveClass::class => new BoundArgument(new Reference('foo'))];
83+
$bindings = [
84+
CaseSensitiveClass::class => new BoundArgument(new Reference('foo')),
85+
CaseSensitiveClass::class.' $c' => new BoundArgument(new Reference('bar')),
86+
];
8487

8588
// Explicit service id
8689
$definition1 = $container->register('def1', NamedArgumentsDummy::class);
@@ -91,11 +94,16 @@ public function testTypedReferenceSupport()
9194
$definition2->addArgument(new TypedReference(CaseSensitiveClass::class, CaseSensitiveClass::class));
9295
$definition2->setBindings($bindings);
9396

97+
$definition3 = $container->register('def3', NamedArgumentsDummy::class);
98+
$definition3->addArgument(new TypedReference(CaseSensitiveClass::class, CaseSensitiveClass::class, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, 'c'));
99+
$definition3->setBindings($bindings);
100+
94101
$pass = new ResolveBindingsPass();
95102
$pass->process($container);
96103

97104
$this->assertEquals([$typedRef], $container->getDefinition('def1')->getArguments());
98105
$this->assertEquals([new Reference('foo')], $container->getDefinition('def2')->getArguments());
106+
$this->assertEquals([new Reference('bar')], $container->getDefinition('def3')->getArguments());
99107
}
100108

101109
public function testScalarSetter()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy