Skip to content

Commit 2aa63d1

Browse files
bug symfony#45572 [HttpKernel] fix using Target attribute with controller arguments (kbond)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpKernel] fix using Target attribute with controller arguments | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix symfony#45021 | License | MIT | Doc PR | n/a This was supposed to fix symfony#45021 but after adding what I suspected was a failing test, it works as expected. I guess I'm not testing the correct thing? Here's a quick reproducer that shows the problem in a "real app": ```php class HomepageController extends AbstractController { public function __construct(#[Target('request.logger')] private LoggerInterface $logger) { } #[Route('/', name: 'app_homepage')] public function index(#[Target('request.logger')] LoggerInterface $logger): Response { dd( $this->logger->getName(), // "request" (expected) $logger->getName() // "app" (not-expected) ); // ... } } ``` Commits ------- 448cc3a [HttpKernel] fix using Target with controller args
2 parents 2da78ce + 448cc3a commit 2aa63d1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function process(ContainerBuilder $container)
192192
$args[$p->name] = new Reference($erroredId, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE);
193193
} else {
194194
$target = ltrim($target, '\\');
195-
$args[$p->name] = $type ? new TypedReference($target, $type, $invalidBehavior, $p->name) : new Reference($target, $invalidBehavior);
195+
$args[$p->name] = $type ? new TypedReference($target, $type, $invalidBehavior, Target::parseName($p)) : new Reference($target, $invalidBehavior);
196196
}
197197
}
198198
// register the maps as a per-method service-locators

src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ public function testBindWithTarget()
428428
$container = new ContainerBuilder();
429429
$resolver = $container->register('argument_resolver.service')->addArgument([]);
430430

431+
$container->register(ControllerDummy::class, 'bar');
432+
$container->register(ControllerDummy::class.' $imageStorage', 'baz');
433+
431434
$container->register('foo', WithTarget::class)
432435
->setBindings(['string $someApiKey' => new Reference('the_api_key')])
433436
->addTag('controller.service_arguments');
@@ -437,7 +440,11 @@ public function testBindWithTarget()
437440
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
438441
$locator = $container->getDefinition((string) $locator['foo::fooAction']->getValues()[0]);
439442

440-
$expected = ['apiKey' => new ServiceClosureArgument(new Reference('the_api_key'))];
443+
$expected = [
444+
'apiKey' => new ServiceClosureArgument(new Reference('the_api_key')),
445+
'service1' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE, 'imageStorage')),
446+
'service2' => new ServiceClosureArgument(new TypedReference(ControllerDummy::class, ControllerDummy::class, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE, 'service2')),
447+
];
441448
$this->assertEquals($expected, $locator->getArgument(0));
442449
}
443450
}
@@ -513,7 +520,10 @@ class WithTarget
513520
{
514521
public function fooAction(
515522
#[Target('some.api.key')]
516-
string $apiKey
523+
string $apiKey,
524+
#[Target('image.storage')]
525+
ControllerDummy $service1,
526+
ControllerDummy $service2
517527
) {
518528
}
519529
}

0 commit comments

Comments
 (0)