8000 [Routing] Make Psr4DirectoryLoader skip abstract classes by MatTheCat · Pull Request #48169 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Make Psr4DirectoryLoader skip abstract classes #48169

New issu 8000 e

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

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 7 additions & 4 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ public function setRouteAnnotationClass(string $class)
*/
public function load(mixed $class, string $type = null): RouteCollection
{
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
if (!$class instanceof \ReflectionClass) {
if (!class_exists($class)) {
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
}

$class = new \ReflectionClass($class);
}

$class = new \ReflectionClass($class);
if ($class->isAbstract()) {
throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
}
Expand Down Expand Up @@ -227,7 +230,7 @@ protected function addRoute(RouteCollection $collection, object $annot, array $g

public function supports(mixed $resource, string $type = null): bool
{
return \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
return ($resource instanceof \ReflectionClass || \is_string($resource) && preg_match('/^(?:\\\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)+$/', $resource)) && (!$type || \in_array($type, ['annotation', 'attribute'], true));
}

public function setResolver(LoaderResolverInterface $resolver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function (\SplFileInfo $current) {
continue;
8000 }

if ($class = $this->findClass($file)) {
$refl = new \ReflectionClass($class);
if ($refl->isAbstract()) {
if ($className = $this->findClass($file)) {
$class = new \ReflectionClass($className);
if ($class->isAbstract()) {
continue;
}

Expand Down
13 changes: 9 additions & 4 deletions src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function load(mixed $resource, string $type = null): ?RouteCollection
return new RouteCollection();
}

return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'));
return $this->loadFromDirectory($path, trim($resource['namespace'], '\\'), $type);
}

public function supports(mixed $resource, string $type = null): bool
Expand All @@ -59,7 +59,7 @@ public function forDirectory(string $currentDirectory): static
return $loader;
}

private function loadFromDirectory(string $directory, string $psr4Prefix): RouteCollection
private function loadFromDirectory(string $directory, string $psr4Prefix, string $type): RouteCollection
{
$collection = new RouteCollection();
$collection->addResource(new DirectoryResource($directory, '/\.php$/'));
Expand All @@ -79,15 +79,20 @@ function (\SplFileInfo $current) {
/** @var \SplFileInfo $file */
foreach ($files as $file) {
if ($file->isDir()) {
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename()));
$collection->addCollection($this->loadFromDirectory($file->getPathname(), $psr4Prefix.'\\'.$file->getFilename(), $type));

continue;
}
if ('php' !== $file->getExtension() || !class_exists($className = $psr4Prefix.'\\'.$file->getBasename('.php'))) {
continue;
}

$collection->addCollection($this->import($className, 'attribute'));
$class = new \ReflectionClass($className);
if ($class->isAbstract()) {
continue;
}

$collection->addCollection($this->import($class, $type));
}

return $collection;
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()
{
< 5510 span class='blob-code-inner blob-code-marker ' data-code-marker="+"> $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