From 18494c890110c7198339b58c58466b8fd7e568a2 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Wed, 29 Nov 2023 16:21:45 +0100 Subject: [PATCH] [Routing] Fix conflicting FQCN aliases with route name --- .../Routing/Loader/AttributeClassLoader.php | 14 +++++++++++--- .../InvokableFQCNAliasConflictController.php | 15 +++++++++++++++ .../InvokableFQCNAliasConflictController.php | 13 +++++++++++++ .../Tests/Loader/AttributeClassLoaderTestCase.php | 9 +++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/InvokableFQCNAliasConflictController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/AttributeFixtures/InvokableFQCNAliasConflictController.php diff --git a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php index cd12b87155a40..679b0c84557bb 100644 --- a/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AttributeClassLoader.php @@ -144,7 +144,9 @@ public function load(mixed $class, string $type = null): RouteCollection 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 ($newRouteName !== $aliasName = sprintf('%s::%s', $class->name, $method->name)) { + $collection->addAlias($aliasName, $newRouteName); + } } } if (0 === $collection->count() && $class->hasMethod('__invoke')) { @@ -155,8 +157,14 @@ public function load(mixed $class, string $type = null): RouteCollection } } if ($fqcnAlias && 1 === $collection->count()) { - $collection->addAlias($class->name, $invokeRouteName = key($collection->all())); - $collection->addAlias(sprintf('%s::__invoke', $class->name), $invokeRouteName); + $invokeRouteName = key($collection->all()); + if ($invokeRouteName !== $class->name) { + $collection->addAlias($class->name, $invokeRouteName); + } + + if ($invokeRouteName !== $aliasName = sprintf('%s::__invoke', $class->name)) { + $collection->addAlias($aliasName, $invokeRouteName); + } } if ($this->hasDeprecatedAnnotations) { diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/InvokableFQCNAliasConflictController.php b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/InvokableFQCNAliasConflictController.php new file mode 100644 index 0000000000000..1155b87daeaf5 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/AnnotationFixtures/InvokableFQCNAliasConflictController.php @@ -0,0 +1,15 @@ +assertEquals(new Alias('lol'), $routes->getAlias($this->getNamespace().'\InvokableController::__invoke')); } + public function testInvokableFQCNAliasConflictController() + { + $routes = $this->loader->load($this->getNamespace().'\InvokableFQCNAliasConflictController'); + $this->assertCount(1, $routes); + $this->assertEquals('/foobarccc', $routes->get($this->getNamespace().'\InvokableFQCNAliasConflictController')->getPath()); + $this->assertNull($routes->getAlias($this->getNamespace().'\InvokableFQCNAliasConflictController')); + $this->assertEquals(new Alias($this->getNamespace().'\InvokableFQCNAliasConflictController'), $routes->getAlias($this->getNamespace().'\InvokableFQCNAliasConflictController::__invoke')); + } + public function testInvokableMethodControllerLoader() { $routes = $this->loader->load($this->getNamespace().'\InvokableMethodController');