【发布时间】:2013-05-23 10:02:08
【问题描述】:
默认情况下,Symfony 会尝试扫描每个捆绑目录以查找名为“Command”的文件夹,并在其中搜索 Console\Command 类。
但是,当您想在控制台命令中使用 DIC 和 DI 时,还有另一种方法可以实现这一点。根据this article,应该可以使用依赖注入容器加载控制台命令,所以我尝试了。
我做了一个service.xml:
<services>
<service id="atlas_cli.helper.anonymize" class="AtlasCliBundle\Services\AnonymizerHelperService" public="false" />
<service id="atlas_cli.helper.dbconfiguration" class="AtlasCliBundle\Services\ConfigurationHelperService" public="false" />
<service id="atlas_cli.helper.schemadump" class="AtlasCliBundle\Services\SchemaDumpHelperService" public="false" />
<service id="atlas_cli.helperset" class="Symfony\Component\Console\Helper\HelperSet" />
<service id="atlas_cli.command.anonymize" class="AtlasCliBundle\Command\AnonymizeCommand" public="true">
<tag name="console.command" />
<call method="setHelperSet">
<argument type="service" id="atlas_cli.helperset" />
</call>
<call method="setAnonymizeHelper">
<argument type="service" id="atlas_cli.helper.anonymize" />
<argument type="string">dbanonymizer</argument>
</call>
<call method="setDbConfigurationHelper">
<argument type="service" id="atlas_cli.helper.dbconfiguration" />
<argument type="string">dbconfiguration</argument>
</call>
</service>
<service id="atlas_cli.command.schema" class="AtlasCliBundle\Command\SchemaCommand" public="true">
<tag name="console.command" />
<call method="setHelperSet">
<argument type="service" id="atlas_cli.helperset" />
</call>
<call method="setDbConfigurationHelper">
<argument type="service" id="atlas_cli.helper.dbconfiguration" />
<argument type="string">dbconfiguration</argument>
</call>
<call method="setSchemadumpHelper">
<argument type="service" id="atlas_cli.helper.schemadump" />
<argument type="string">schemadump</argument>
</call>
</service>
</services>
如您所见,我配置了 2 个命令匿名化和模式。这些命令在运行 app/console 时可用,但永远不会调用 set 方法(例如 setDbConfigurationHelper)。
我使用 Symfony 2.1,但我在 grep -ris "console\.command" * 上搜索了完整的 Symfony 框架代码,但对于 Symfony 2.3 来说,这并没有给出任何有用的结果。
标签 console.command 不再支持了吗?如果答案是肯定的,您建议使用什么来处理我的命令类中的依赖关系?
谢谢!!
【问题讨论】:
-
您传递到服务中的附加字符串是什么?
-
字符串是助手的别名(可选)。
标签: symfony dependency-injection