Skip to content

[Routing] Fix PSR-4 directory loader for abstract classes #48170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down