Skip to content

Commit 6f19dad

Browse files
committed
[Routing] Add FQCN aliases for routes targeting invokable classes
defining one route only
1 parent cb1f853 commit 6f19dad

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

src/Symfony/Component/Routing/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+
6.3
5+
---
6+
7+
* Add FQCN aliases for routes loaded from attributes/annotations targeting invokable classes defining one route only
8+
49
6.2
510
---
611

src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,29 @@ public function load(mixed $class, string $type = null): RouteCollection
125125
return $collection;
126126
}
127127

128+
$invoke = false;
128129
foreach ($class->getMethods() as $method) {
129130
$this->defaultRouteIndex = 0;
130131
foreach ($this->getAnnotations($method) as $annot) {
131132
$this->addRoute($collection, $annot, $globals, $class, $method);
133+
if ('__invoke' === $method->name) {
134+
$invoke = true;
135+
}
132136
}
133137
}
134138

135139
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
136140
$globals = $this->resetGlobals();
137141
foreach ($this->getAnnotations($class) as $annot) {
138142
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
143+
$invoke = true;
139144
}
140145
}
141146

147+
if ($invoke && 1 === $collection->count()) {
148+
$collection->addAlias($class->name, key($collection->all()));
149+
}
150+
142151
return $collection;
143152
}
144153

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
class InvokableMethodController
8+
{
9+
/**
10+
* @Route("/here", name="lol", methods={"GET", "POST"}, schemes={"https"})
11+
*/
12+
public function __invoke()
13+
{
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
4+
5+
use Symfony\Component\Routing\Annotation\Route;
6+
7+
class InvokableMethodController
8+
{
9+
#[Route(path: '/here', name: 'lol', methods: ["GET", "POST"], schemes: ['https'])]
10+
11+
public function __invoke()
12+
{
13+
}
14+
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Tests\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Alias;
1516
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
1617
use Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\AbstractClassController;
1718

@@ -72,6 +73,17 @@ public function testInvokableControllerLoader()
7273
$this->assertEquals('/here', $routes->get('lol')->getPath());
7374
$this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
7475
$this->assertEquals(['https'], $routes->get('lol')->getSchemes());
76+
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableController'));
77+
}
78+
79+
public function testInvokableMethodControllerLoader()
80+
{
81+
$routes = $this->loader->load($this->getNamespace().'\InvokableMethodController');
82+
$this->assertCount(1, $routes);
83+
$this->assertEquals('/here', $routes->get('lol')->getPath());
84+
$this->assertEquals(['GET', 'POST'], $routes->get('lol')->getMethods());
85+
$this->assertEquals(['https'], $routes->get('lol')->getSchemes());
86+
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableMethodController'));
7587
}
7688

7789
public function testInvokableLocalizedControllerLoading()

0 commit comments

Comments
 (0)