Symfony:用console.command标记命令似乎不起作用 [英] Symfony: tagging a command with console.command doesn't seem to work

查看:48
本文介绍了Symfony:用console.command标记命令似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Symfony会尝试在每个捆绑软件目录中扫描名为"Command"的文件夹,并在其中搜索Console \ Command类.

By default Symfony tries to scan every bundle directory for a folder called 'Command' and searches for Console\Command classes in there.

但是,当您想在控制台命令中使用DIC和DI时,有另一种方法可以实现此目的.根据本文用依赖项注入容器加载控制台命令,所以我尝试了.

But when you want to use the DIC and DI in you console commands there is another approach to make this happen. According to this article it should be possible to load your console commands with the dependency injection container, so I tried.

我制作了service.xml:

I made a 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个命令匿名和模式.这些命令在运行应用程序/控制台时可用,但不会调用set方法(例如setDbConfigurationHelper).

As you can see I have configured 2 commands anonymize and schema. The commands are available when running app/console but the set methods (setDbConfigurationHelper for example) are never being called.

我使用Symfony 2.1,但是我在 grep -ris"console \ .command" * 上搜索了完整的Symfony框架代码,但这对于Symfony 2.3都没有任何有用的结果.

I use Symfony 2.1 but I searched the complete Symfony framework code on grep -ris "console\.command" * but that doesn't give any usefull result, either for Symfony 2.3.

是否不再支持标签console.command?如果答案是肯定的,那么您建议使用什么来处理命令类中的依赖项?

Is the tag console.command not supported anymore? And if the answer is yes, what do you recommend to use for handling dependencies in my command classes?

谢谢!

推荐答案

Symfony 2.4

对于 Symfony 2.3 及更低版本,将 Bundle :: registerCommands 覆盖为以下内容:

For Symfony 2.3 and lower versions override Bundle::registerCommands to something like this:

<?php

namespace Acme\DemoBundle;

use Symfony\Component\Console\Application;

// ...

class AcmeDemoBundle extends Bundle
{
    public function registerCommands(Application $application)
    {
        $container = $application->getKernel()->getContainer();

        $application->add($container->get('atlas_cli.command.anonymize'));
        $application->add($container->get('atlas_cli.command.schema'));
    }
}

这篇关于Symfony:用console.command标记命令似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆