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 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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Make Psr4DirectoryLoader skip abstract classes
  • Loading branch information
MatTheCat committed Nov 9, 2022
commit 61619cef19d7dfb17a6d5aba1b87dad36c862e21
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;
}

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
7 changes: 6 additions & 1 deletion src/Symfony/Component/Routing/Loader/Psr4DirectoryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ function (\SplFileInfo $current) {
continue;
}

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

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

return $collection;
Expand Down
0