Skip to content

Commit 83d0106

Browse files
bug #52805 [Routing] Fix conflicting FQCN aliases with route name (fancyweb)
This PR was merged into the 6.4 branch. Discussion ---------- [Routing] Fix conflicting FQCN aliases with route name | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | #52801 | License | MIT When the route name === the FQCN alias we want to add, `addAlias()` throws. In this case, we can safely ignore the exception and move on. Commits ------- 18494c8 [Routing] Fix conflicting FQCN aliases with route name
2 parents 9144962 + 18494c8 commit 83d0106

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ public function load(mixed $class, string $type = null): RouteCollection
144144

145145
if (1 === $collection->count() - \count($routeNamesBefore)) {
146146
$newRouteName = current(array_diff(array_keys($collection->all()), $routeNamesBefore));
147-
$collection->addAlias(sprintf('%s::%s', $class->name, $method->name), $newRouteName);
147+
if ($newRouteName !== $aliasName = sprintf('%s::%s', $class->name, $method->name)) {
148+
$collection->addAlias($aliasName, $newRouteName);
149+
}
148150
}
149151
}
150152
if (0 === $collection->count() && $class->hasMethod('__invoke')) {
@@ -155,8 +157,14 @@ public function load(mixed $class, string $type = null): RouteCollection
155157
}
156158
}
157159
if ($fqcnAlias && 1 === $collection->count()) {
158-
$collection->addAlias($class->name, $invokeRouteName = key($collection->all()));
159-
$collection->addAlias(sprintf('%s::__invoke', $class->name), $invokeRouteName);
160+
$invokeRouteName = key($collection->all());
161+
if ($invokeRouteName !== $class->name) {
162+
$collection->addAlias($class->name, $invokeRouteName);
163+
}
164+
165+
if ($invokeRouteName !== $aliasName = sprintf('%s::__invoke', $class->name)) {
166+
$collection->addAlias($aliasName, $invokeRouteName);
167+
}
160168
}
161169

162170
if ($this->hasDeprecatedAnnotations) {
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\Attribute\Route;
6+
7+
/**
8+
* @Route("/foobarccc", name="Symfony\Component\Routing\Tests\Fixtures\AnnotationFixtures\InvokableFQCNAliasConflictController")
9+
*/
10+
class InvokableFQCNAliasConflictController
11+
{
12+
public function __invoke()
13+
{
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures;
4+
5+
use Symfony\Component\Routing\Attribute\Route;
6+
7+
#[Route(path: '/foobarccc', name: self::class)]
8+
class InvokableFQCNAliasConflictController
9+
{
10+
public function __invoke()
11+
{
12+
}
13+
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public function testInvokableControllerLoader()
8686
$this->assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableController::__invoke'));
8787
}
8888

89+
public function testInvokableFQCNAliasConflictController()
90+
{
91+
$routes = $this->loader->load($this->getNamespace().'\InvokableFQCNAliasConflictController');
92+
$this->assertCount(1, $routes);
93+
$this->assertEquals('/foobarccc', $routes->get($this->getNamespace().'\InvokableFQCNAliasConflictController')->getPath());
94+
$this->assertNull($routes->getAlias($this->getNamespace().'\InvokableFQCNAliasConflictController'));
95+
$this->assertEquals(new Alias($this->getNamespace().'\InvokableFQCNAliasConflictController'), $routes->getAlias($this->getNamespace().'\InvokableFQCNAliasConflictController::__invoke'));
96+
}
97+
8998
public function testInvokableMethodControllerLoader()
9099
{
91100
$routes = $this->loader->load($this->getNamespace().'\InvokableMethodController');

0 commit comments

Comments
 (0)