From a232a232803f51ea57c973476d82a1e664b0d042 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Wed, 9 Nov 2022 11:16:55 +0100 Subject: [PATCH] [Routing] Fix PSR-4 directory loader for abstract classes --- .../Routing/Loader/Psr4DirectoryLoader.php | 2 +- .../SubNamespace/IrrelevantClass.php | 28 +++++++++++++++++++ .../SubNamespace/MyAbstractController.php | 24 ++++++++++++++++ .../SubNamespace/MyChildController.php | 19 +++++++++++++ .../Tests/Loader/Psr4DirectoryLoaderTest.php | 9 ++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/IrrelevantClass.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyAbstractController.php create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyChildController.php diff --git a/src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php b/src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php index 9f262c006ffc2..e4b848065409e 100644 --- a/src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php +++ b/src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php @@ -83,7 +83,7 @@ function (\SplFileInfo $current) { continue; } - if ('php' !== $file->getExtension() || !class_exists($className = $psr4Prefix.'\\'.$file->getBasename('.php'))) { + if ('php' !== $file->getExtension() || !class_exists($className = $psr4Prefix.'\\'.$file->getBasename('.php')) || (new \ReflectionClass($className))->isAbstract()) { continue; } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/IrrelevantClass.php b/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/IrrelevantClass.php new file mode 100644 index 0000000000000..ca3c1bcbddcaf --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/IrrelevantClass.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace; + +use Symfony\Component\HttpFoundation\Response; + +/** + * An irrelevant class. + * + * This fixture is not referenced anywhere. Its presence makes sure, classes without attributes are silently ignored + * when loading routes from a directory. + */ +final class IrrelevantClass +{ + public function irrelevantAction(): Response + { + return new Response(status: Response::HTTP_NO_CONTENT); + } +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyAbstractController.php b/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyAbstractController.php new file mode 100644 index 0000000000000..30d8bbdb65bcc --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyAbstractController.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace; + +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Annotation\Route; + +abstract class MyAbstractController +{ + #[Route('/a/route/from/an/abstract/controller', name: 'from_abstract')] + public function someAction(): Response + { + return new Response(status: Response::HTTP_NO_CONTENT); + } +} diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyChildController.php b/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyChildController.php new file mode 100644 index 0000000000000..a6d0333577079 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/Psr4Controllers/SubNamespace/MyChildController.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace; + +use Symfony\Component\Routing\Annotation\Route; + +#[Route('/my/child/controller', name: 'my_child_controller_')] +final class MyChildController extends MyAbstractController +{ +} diff --git a/src/Symfony/Component/Routing/Tests/Loader/Psr4DirectoryLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/Psr4DirectoryLoaderTest.php index 541d352746c90..2bae59005fa60 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/Psr4DirectoryLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/Psr4DirectoryLoaderTest.php @@ -21,6 +21,7 @@ use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController; use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace\EvenDeeperNamespace\MyOtherController; +use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace\MyChildController; use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace\MyControllerWithATrait; class Psr4DirectoryLoaderTest extends TestCase @@ -56,6 +57,14 @@ public function testTraitController() $this->assertSame(MyControllerWithATrait::class.'::someAction', $route->getDefault('_controller')); } + public function testAbstractController() + { + $route = $this->loadPsr4Controllers()->get('my_child_controller_from_abstract'); + + $this->assertSame('/my/child/controller/a/route/from/an/abstract/controller', $route->getPath()); + $this->assertSame(MyChildController::class.'::someAction', $route->getDefault('_controller')); + } + /** * @dataProvider provideNamespacesThatNeedTrimming */ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy