Skip to content

[Routing] Psr4DirectoryLoader issue with abstract classes #48168

Closed
@MatTheCat

Description

@MatTheCat

Symfony version(s) affected

6.2-beta

Description

The new Psr4DirectoryLoader only supports attribute or annotation type so it will use the AnnotationClassLoader which don’t want to deal with abstract classes:

if ($class->isAbstract()) {
throw new \InvalidArgumentException(sprintf('Annotations from class "%s" cannot be read as it is abstract.', $class->getName()));
}

(See 946ccb6 for rationale.)

If AnnotationDirectoryLoader skips them, Psr4DirectoryLoader does not meaning it will crash on abstract classes.

How to reproduce

Configure your routes to use the PSR-4 route loader, eg.

<?php

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

return static function (RoutingConfigurator $routes)
{
    $routes->import(['path' => '../src/Controller', 'namespace' => 'App\Controller'], 'annotation');
};

and make one of your controllers extends an abstract one (it does not need to define any method).

Refresh the cache to see the error.

Possible Solution

Psr4DirectoryLoader should skip abstract classes. As it means it will need to create an instance of ReflectionClass it could be useful to make AnnotationClassLoader::load accept these.

Additional Context

Spotted from #48161

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions