Skip to content

Commit

Permalink
feature #35560 [HttpKernel] allow using public aliases to reference c…
Browse files Browse the repository at this point in the history
…ontrollers (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
  • Loading branch information
fabpot committed Feb 3, 2020
2 parents ff4892b + 94bc1f7 commit 59f0980
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Expand Up @@ -141,7 +141,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
AbstractConfigurator::$valuePreProcessor = $valuePreProcessor;
}

$container->setAlias(static::class, 'kernel');
$container->setAlias(static::class, 'kernel')->setPublic(true);
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* allowed using public aliases to reference controllers

5.0.0
-----

Expand Down
Expand Up @@ -53,6 +53,13 @@ public function process(ContainerBuilder $container)
$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);
Expand Down Expand Up @@ -182,6 +189,10 @@ public function process(ContainerBuilder $container)
// 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];
}
}
}
}
Expand Down
Expand Up @@ -43,6 +43,11 @@ public function process(ContainerBuilder $container)
// 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)) {
Expand Down
Expand Up @@ -379,6 +379,23 @@ public function testNotTaggedControllerServiceReceivesLocatorArgument()

$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
Expand Down

0 comments on commit 59f0980

Please sign in to comment.