Skip to content

Commit b701ca3

Browse files
bug #53985 [HttpKernel] Allow tagged controllers in ControllerResolver (marein)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [HttpKernel] Allow tagged controllers in ControllerResolver | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #52471 | License | MIT If the request attribute `_check_controller_is_allowed` is set, only `TemplateController`, children of `AbstractController` and controllers that have the attribute `#[AsController]` are allowed by default. This change also adds controllers tagged with `controller.service_arguments` to the mix. It allows them to be used with different fragment renderers (e.g. `esi` and `ssi`) without having to add any of the above conditions. This pull request is a follow up of [this discussion](#52471 (comment)). Commits ------- 7d2eb5a [HttpKernel] Allow tagged controllers in ControllerResolver
2 parents 2e4de58 + 7d2eb5a commit b701ca3

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function process(ContainerBuilder $container)
4545

4646
$parameterBag = $container->getParameterBag();
4747
$controllers = [];
48+
$controllerClasses = [];
4849

4950
$publicAliases = [];
5051
foreach ($container->getAliases() as $id => $alias) {
@@ -74,6 +75,8 @@ public function process(ContainerBuilder $container)
7475
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
7576
}
7677

78+
$controllerClasses[] = $class;
79+
7780
// get regular public methods
7881
$methods = [];
7982
$arguments = [];
@@ -227,5 +230,10 @@ public function process(ContainerBuilder $container)
227230
}
228231

229232
$container->setAlias('argument_resolver.controller_locator', (string) $controllerLocatorRef);
233+
234+
if ($container->hasDefinition('controller_resolver')) {
235+
$container->getDefinition('controller_resolver')
236+
->addMethodCall('allowControllers', [array_unique($controllerClasses)]);
237+
}
230238
}
231239
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,30 @@ public function testTaggedIteratorAndTaggedLocatorAttributes()
557557
$this->assertTrue($argLocator->has('foo'));
558558
$this->assertSame('bar', $argLocator->get('foo'));
559559
}
560+
561+
public function testTaggedControllersAreRegisteredInControllerResolver()
562+
{
563+
$container = new ContainerBuilder();
564+
$container->register('argument_resolver.service')->addArgument([]);
565+
$controllerResolver = $container->register('controller_resolver');
566+
567+
$container->register('foo', RegisterTestController::class)
568+
->addTag('controller.service_arguments')
569+
;
570+
571+
// duplicates should be removed
572+
$container->register('bar', RegisterTestController::class)
573+
->addTag('controller.service_arguments')
574+
;
575+
576+
// services with no tag should be ignored
577+
$container->register('baz', ControllerDummy::class);
578+
579+
$pass = new RegisterControllerArgumentLocatorsPass();
580+
$pass->process($container);
581+
582+
$this->assertSame([['allowControllers', [[RegisterTestController::class]]]], $controllerResolver->getMethodCalls());
583+
}
560584
}
561585

562586
class RegisterTestController

0 commit comments

Comments
 (0)