8000 feature #35560 [HttpKernel] allow using public aliases to reference c… · symfony/symfony@59f0980 · GitHub
[go: up one dir, main page]

Skip to content

Commit 59f0980

Browse files
committed
feature #35560 [HttpKernel] allow using public aliases to reference controllers (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpKernel] allow using public aliases to reference controllers | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - This PR allows referencing a controller with an alias when needed. The use case I'm targetting is using `@Route` annotations on methods of the `App\Kernel` and have them work. This PR allows it. Sidekick of symfony/recipes#735 Commits ------- 94bc1f7 [HttpKernel] allow using public aliases to reference controllers
2 parents ff4892b + 94bc1f7 commit 59f0980

File tree

5 files changed

+39
-1
lines changed

5 files changed

+39
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
141141
AbstractConfigurator::$valuePreProcessor = $valuePreProcessor;
142142
}
143143

144-
$container->setAlias(static::class, 'kernel');
144+
$container->setAlias(static::class, 'kernel')->setPublic(true);
145145
});
146146
}
147147

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.1.0
5+
-----
6+
7+
* allowed using public aliases to reference controllers
8+
49
5.0.0
510
-----
611

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ public function process(ContainerBuilder $container)
5353
$parameterBag = $container->getParameterBag();
5454
$controllers = [];
5555

56+
$publicAliases = [];
57+
foreach ($container->getAliases() as $id => $alias) {
58+
if ($alias->isPublic()) {
59+
$publicAliases[(string) $alias][] = $id;
60+
}
61+
}
62+
5663
foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) {
5764
$def = $container->getDefinition($id);
5865
$def->setPublic(true);
@@ -182,6 +189,10 @@ public function process(ContainerBuilder $container)
182189
// register the maps as a per-method service-locators
183190
if ($args) {
184191
$controllers[$id.'::'.$r->name] = ServiceLocatorTagPass::register($container, $args);
192+
193+
foreach ($publicAliases[$id] ?? [] as $alias) {
194+
$controllers[$alias.'::'.$r->name] = clone $controllers[$id.'::'.$r->name];
195+
}
185196
}
186197
}
187198
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public function process(ContainerBuilder $container)
4343
// any methods listed for call-at-instantiation cannot be actions
4444
$reason = false;
4545
list($id, $action) = explode('::', $controller);
46+
47+
if ($container->hasAlias($id)) {
48+
continue;
49+
}
50+
4651
$controllerDef = $container->getDefinition($id);
4752
foreach ($controllerDef->getMethodCalls() as list($method)) {
4853
if (0 === strcasecmp($action, $method)) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,23 @@ public function testNotTaggedControllerServiceReceivesLocatorArgument()
379379

380380
$this->assertInstanceOf(Reference::class, $locatorArgument);
381381
}
382+
383+
public function testAlias()
384+
{
385+
$container = new ContainerBuilder();
386+
$resolver = $container->register('argument_resolver.service')->addArgument([]);
387+
388+
$container->register('foo', RegisterTestController::class)
389+
->addTag('controller.service_arguments');
390+
391+
$container->setAlias(RegisterTestController::class, 'foo')->setPublic(true);
392+
393+
$pass = new RegisterControllerArgumentLocatorsPass();
394+
$pass->process($container);
395+
396+
$locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0);
397+
$this->assertSame([RegisterTestController::class.'::fooAction', 'foo::fooAction'], array_keys($locator));
398+
}
382399
}
383400

384401
class RegisterTestController

0 commit comments

Comments
 (0)
0