Working with Symfony 5.3 I would like to find all classes within the project which use a custom annotation @MyAnnotation
.
While I have found countless tutorials and a lot of information about annotations, they all only show how to check if a given object or method uses some annotation. For example it is no problem to use a kernel event subscriber to test if the current controller uses a annotation or not.
But I was not able to find a way, to automatically find / discover all classes which use a given annotation. I assume that Doctrine itself does the same to find all entities which use its @Entity
annotation. But how is this done?
I tried to check the Doctrine code to see how hit works, but I did not found out.
Is there some build in features? Or is there some giveMeAllClassesInProject()
method I could use to test each class manually?
Traversing all classes within the project would not be practical. I do not have to actively tell Doctrine that MyBundle
is installed in the project and that it can find its entities at /vendor/me/my-bundle/src/Entity
but Doctrine does find these entities.
Of course the src/Entity/
part is standard and pretty sure belongs to the Doctrines default configuration, but Doctrine still need to know which bundles are installed, where to find them, where to find the project src/
dir, etc. So there needs to be be some giveMeAlleSrcDirsInProject()
method or something in this direction, doesn't it?
Additionally even if Symfony would deliver all src/
dirs, traversing them and checking all entity classes for some annotation on every request would be pretty inefficient. So this information is surely cached somehow. But when is this information gathered, etc.
In short: Is there any standard procedure on how to implement annotations like @Entity
?