From 94bc1f7d3b5aa0f93887102da608bc6e6aed6002 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 2 Feb 2020 17:25:17 +0100 Subject: [PATCH] [HttpKernel] allow using public aliases to reference controllers --- .../FrameworkBundle/Kernel/MicroKernelTrait.php | 2 +- src/Symfony/Component/HttpKernel/CHANGELOG.md | 5 +++++ .../RegisterControllerArgumentLocatorsPass.php | 11 +++++++++++ ...emoveEmptyControllerArgumentLocatorsPass.php | 5 +++++ ...gisterControllerArgumentLocatorsPassTest.php | 17 +++++++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php b/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php index f0308e327a..be74a7ad6c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php @@ -141,7 +141,7 @@ trait MicroKernelTrait AbstractConfigurator::$valuePreProcessor = $valuePreProcessor; } - $container->setAlias(static::class, 'kernel'); + $container->setAlias(static::class, 'kernel')->setPublic(true); }); } diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index adcbe66f66..17b7122023 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.1.0 +----- + + * allowed using public aliases to reference controllers + 5.0.0 ----- diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php index 475b5d756a..f7ddb870f1 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php @@ -53,6 +53,13 @@ class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface $parameterBag = $container->getParameterBag(); $controllers = []; + $publicAliases = []; + foreach ($container->getAliases() as $id => $alias) { + if ($alias->isPublic()) { + $publicAliases[(string) $alias][] = $id; + } + } + foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) { $def = $container->getDefinition($id); $def->setPublic(true); @@ -182,6 +189,10 @@ class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface // register the maps as a per-method service-locators if ($args) { $controllers[$id.'::'.$r->name] = ServiceLocatorTagPass::register($container, $args); + + foreach ($publicAliases[$id] ?? [] as $alias) { + $controllers[$alias.'::'.$r->name] = clone $controllers[$id.'::'.$r->name]; + } } } } diff --git a/src/Symfony/Component/HttpKernel/DependencyInjection/RemoveEmptyControllerArgumentLocatorsPass.php b/src/Symfony/Component/HttpKernel/DependencyInjection/RemoveEmptyControllerArgumentLocatorsPass.php index 596b6188f6..71bb23bbf8 100644 --- a/src/Symfony/Component/HttpKernel/DependencyInjection/RemoveEmptyControllerArgumentLocatorsPass.php +++ b/src/Symfony/Component/HttpKernel/DependencyInjection/RemoveEmptyControllerArgumentLocatorsPass.php @@ -43,6 +43,11 @@ class RemoveEmptyControllerArgumentLocatorsPass implements CompilerPassInterface // any methods listed for call-at-instantiation cannot be actions $reason = false; list($id, $action) = explode('::', $controller); + + if ($container->hasAlias($id)) { + continue; + } + $controllerDef = $container->getDefinition($id); foreach ($controllerDef->getMethodCalls() as list($method)) { if (0 === strcasecmp($action, $method)) { diff --git a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php index 0746643036..2cda6c4f56 100644 --- a/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php @@ -379,6 +379,23 @@ class RegisterControllerArgumentLocatorsPassTest extends TestCase $this->assertInstanceOf(Reference::class, $locatorArgument); } + + public function testAlias() + { + $container = new ContainerBuilder(); + $resolver = $container->register('argument_resolver.service')->addArgument([]); + + $container->register('foo', RegisterTestController::class) + ->addTag('controller.service_arguments'); + + $container->setAlias(RegisterTestController::class, 'foo')->setPublic(true); + + $pass = new RegisterControllerArgumentLocatorsPass(); + $pass->process($container); + + $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0); + $this->assertSame([RegisterTestController::class.'::fooAction', 'foo::fooAction'], array_keys($locator)); + } } class RegisterTestController