Michal Otfinowski
Michal Otfinowski

Reputation: 21

get_declared_classes in symfony 3.4

I have app in Symfony 3.4 and I need to get all classes registered in my AppBundle namespace. I use get_declared_classes(). I've created custom namespace AppBundle\MyCustom and I keep there some classes. The problem is that symfony caches get_declared_classes() and it does not cache my custom namespace. When I use get_declared_classes() for the first time (after remove of cache files) I get all my custom classes, but I am getting a problem when I run script for next time. Any ideas?

here is repo with a problem: https://github.com/webostin/getdeclaredclasses

Upvotes: 0

Views: 532

Answers (1)

Padam87
Padam87

Reputation: 1029

The problem is that symfony caches get_declared_classes()

This is a PHP function, Symfony has no control over it.

The problem is might related to autoloading. On the 1st load, Symfony has to build the cache, so includes your class. With a warm cache this is not necessary, so your class is not autoloaded. get_declared_classes wont include your classes.

You can observe this behavior by putting $class = new MyTool(); in the 1st line of your execute method. This will trigger the autoloader, and your class will show up in declared classes.

Solution: It seems you want to get all classes implementing an interface. You can use DI for this.

Create a custom tag, and a manager class, and then use the manager class to access all tagged services in you command.

More here: http://symfony.com/doc/current/service_container/tags.html#creating-custom-tags

Upvotes: 1

Related Questions