8000 bug #48170 [Routing] Fix PSR-4 directory loader for abstract classes … · symfony/symfony@91b2bca · GitHub
[go: up one dir, main page]

Skip to content

Commit 91b2bca

Browse files
bug #48170 [Routing] Fix PSR-4 directory loader for abstract classes (derrabus)
This PR was merged into the 6.2 branch. Discussion ---------- [Routing] Fix PSR-4 directory loader for abstract classes | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #48168, Replaces #48169 | License | MIT | Doc PR | N/A Commits ------- a232a23 [Routing] Fix PSR-4 directory loader for abstract classes
2 parents b02a689 + a232a23 commit 91b2bca

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ function (\SplFileInfo $current) {
8383

8484
continue;
8585
}
86-
if ('php' !== $file->getExtension() || !class_exists($className = $psr4Prefix.'\\'.$file->getBasename('.php'))) {
86+
if ('php' !== $file->getExtension() || !class_exists($className = $psr4Prefix.'\\'.$file->getBasename('.php')) || (new \ReflectionClass($className))->isAbstract()) {
< 8000 /code>
8787
continue;
8888
}
8989

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
16+
/**
17+
* An irrelevant class.
18+
*
19+
* This fixture is not referenced anywhere. Its presence makes sure, classes without attributes are silently ignored
20+
* when loading routes from a directory.
21+
*/
22+
final class IrrelevantClass
23+
{
24+
public function irrelevantAction(): Response
25+
{
26+
return new Response(status: Response::HTTP_NO_CONTENT);
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Component\Routing\Annotation\Route;
16+
17+
abstract class MyAbstractController
18+
{
19+
#[Route('/a/route/from/an/abstract/controller', name: 'from_abstract')]
20+
public function someAction(): Response
21+
{
22+
return new Response(status: Response::HTTP_NO_CONTENT);
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace;
13+
14+
use Symfony\Component\Routing\Annotation\Route;
15+
16+
#[Route('/my/child/controller', name: 'my_child_controller_')]
17+
final class MyChildController extends MyAbstractController
18+
{
19+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Routing\RouteCollection;
2222
use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController;
2323
use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace\EvenDeeperNamespace\MyOtherController;
24+
use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace\MyChildController;
2425
use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\SubNamespace\MyControllerWithATrait;
2526

2627
class Psr4DirectoryLoaderTest extends TestCase
@@ -56,6 +57,14 @@ public function testTraitController()
5657
$this->assertSame(MyControllerWithATrait::class.'::someAction', $route->getDefault('_controller'));
5758
}
5859

60+
public function testAbstractController()
61+
{
62+
$route = $this->loadPsr4Controllers()->get('my_child_controller_from_abstract');
63+
64+
$this->assertSame('/my/child/controller/a/route/from/an/abstract/controller', $route->getPath());
65+
$this->assertSame(MyChildController::class.'::someAction', $route->getDefault('_controller'));
66+
}
67+
5968
/**
6069
* @dataProvider provideNamespacesThatNeedTrimming
6170
*/

0 commit comments

Comments
 (0)
0