Skip to content

[Routing] Add FQCN and FQCN::method aliases when applicable #50084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add FQCN and FQCN::method aliases for routes loaded from attributes/annotations when applicable

6.2
---

Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,36 @@ public function load(mixed $class, string $type = null): RouteCollection
return $collection;
}

$fqcnAlias = false;
foreach ($class->getMethods() as $method) {
$this->defaultRouteIndex = 0;
$routeNamesBefore = array_keys($collection->all());
foreach ($this->getAnnotations($method) as $annot) {
$this->addRoute($collection, $annot, $globals, $class, $method);
if ('__invoke' === $method->name) {
$fqcnAlias = true;
}
}

if (1 === $collection->count() - \count($routeNamesBefore)) {
$newRouteName = current(array_diff(array_keys($collection->all()), $routeNamesBefore));
$collection->addAlias(sprintf('%s::%s', $class->name, $method->name), $newRouteName);
}
}

if (0 === $collection->count() && $class->hasMethod('__invoke')) {
$globals = $this->resetGlobals();
foreach ($this->getAnnotations($class) as $annot) {
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
$fqcnAlias = true;
}
}

if ($fqcnAlias && 1 === $collection->count()) {
$collection->addAlias($class->name, $invokeRouteName = key($collection->all()));
$collection->addAlias(sprintf('%s::__invoke', $class->name), $invokeRouteName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That means we add an "FQCN::__invoke" alias for controllers like this:

#[Route(path: '/foo')]
final class FooController
{
    public function __invoke():
    {
    }
}

}

return $collection;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;

use Symfony\Component\Routing\Annotation\Route;

class InvokableMethodController
{
/**
* @Route("/here", name="lol", methods={"GET", "POST"}, schemes={"https"})
*/
public function __invoke()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;

use Symfony\Component\Routing\Annotation\Route;

class InvokableMethodController
{
#[Route(path: '/here', name: 'lol', methods: ["GET", "POST"], schemes: ['https'])]
public function __invoke()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Routing\Tests\Loader;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Alias;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\AbstractClassController;

Expand Down Expand Up @@ -55,6 +56,7 @@ public function testSimplePathRoute()
$routes = $this->loader->load($this->getNamespace().'\ActionPathController');
$this->assertCount(1, $routes);
$this->assertEquals('/path', $routes->get('action')->getPath());
$this->assertEquals(new Alias('action'), $routes->getAlias($this->getNamespace().'\ActionPathController::action'));
}

public function testRequirementsWithoutPlaceholderName()
Expand All @@ -72,6 +74,19 @@ public function testInvokableControllerLoader()
$this->assertEquals('/here', $routes->get('lol')->getPath());
$this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
$this->assertEquals(['https'], $routes->get('lol')->getSchemes());
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableController'));
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableController::__invoke'));
}

public function testInvokableMethodControllerLoader()
{
$routes = $this->loader->load($this->getNamespace().'\InvokableMethodController');
$this->assertCount(1, $routes);
$this->assertEquals('/here', $routes->get('lol')->getPath());
$this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
$this->assertEquals(['https'], $routes->get('lol')->getSchemes());
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableMethodController'));
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableMethodController::__invoke'));
}

public function testInvokableLocalizedControllerLoading()
Expand Down Expand Up @@ -119,6 +134,8 @@ public function testMethodActionControllers()
$this->assertSame(['put', 'post'], array_keys($routes->all()));
$this->assertEquals('/the/path', $routes->get('put')->getPath());
$this->assertEquals('/the/path', $routes->get('post')->getPath());
$this->assertEquals(new Alias('post'), $routes->getAlias($this->getNamespace().'\MethodActionControllers::post'));
$this->assertEquals(new Alias('put'), $routes->getAlias($this->getNamespace().'\MethodActionControllers::put'));
}

public function testInvokableClassRouteLoadWithMethodAnnotation()
Expand Down