Skip to content

Commit

Permalink
bug #48170 [Routing] Fix PSR-4 directory loader for abstract classes …
Browse files Browse the repository at this point in the history
…(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
  • Loading branch information
nicolas-grekas committed Nov 9, 2022
2 parents b02a689 + a232a23 commit 91b2bca
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
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
@@ -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);
}
}
@@ -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);
}
}
@@ -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
{
}
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

0 comments on commit 91b2bca

Please sign in to comment.