简体   繁体   English

Symfony 3.4中的get_declared_classes

[英]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. 我在Symfony 3.4中有应用程序,并且需要在AppBundle命名空间中注册所有类。 I use get_declared_classes() . 我使用get_declared_classes() I've created custom namespace AppBundle\\MyCustom and I keep there some classes. 我已经创建了自定义名称空间AppBundle\\MyCustom并保留了一些类。 The problem is that symfony caches get_declared_classes() and it does not cache my custom namespace. 问题是symfony缓存了get_declared_classes() ,并且没有缓存我的自定义名称空间。 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. 当我第一次使用get_declared_classes()时(删除缓存文件之后),我得到了所有自定义类,但是下次运行脚本时遇到了问题。 Any ideas? 有任何想法吗?

here is repo with a problem: https://github.com/webostin/getdeclaredclasses 这是一个有问题的仓库: https : //github.com/webostin/getdeclaredclasses

The problem is that symfony caches get_declared_classes()

This is a PHP function, Symfony has no control over it. 这是一个PHP函数,Symfony无法控制它。

The problem is might related to autoloading. 该问题可能与自动加载有关。 On the 1st load, Symfony has to build the cache, so includes your class. 第一次加载时,Symfony必须构建缓存,因此包括您的类。 With a warm cache this is not necessary, so your class is not autoloaded. 使用热缓存,这不是必需的,因此不会自动加载您的类。 get_declared_classes wont include your classes. get_declared_classes包含您的课程。

You can observe this behavior by putting $class = new MyTool(); 您可以通过将$class = new MyTool();观察到此行为$class = new MyTool(); in the 1st line of your execute method. 在您的execute方法的第一行。 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. 您可以为此使用DI。

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 此处更多信息: http : //symfony.com/doc/current/service_container/tags.html#creating-custom-tags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM