0

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

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.

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>

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.

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.

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?

Thanks!!

2
  • what are the additional strings you pass into your services for? May 23, 2013 at 10:27
  • The strings are the helper's alliasses (optional). May 23, 2013 at 10:41

3 Answers 3

3

The console.command tag in has been introduces in Symfony 2.4

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'));
    }
}
6
  • Okay Wouter, can you explain me why the article I mentioned is talking about the console.command tag? Currently I have it exactly implemented the way you propose but in my opinion it's not eager enough. Thanks anyway. May 24, 2013 at 5:49
  • I think because that article is written on March 15th 2010, when Symfony2 was still in heavy development.
    – Wouter J
    May 24, 2013 at 6:03
  • also, the conventions in that article aren't correct anymore.
    – Wouter J
    May 24, 2013 at 13:05
  • Hi Wouter, thanks for your answer and comments. I picked your solution since it will use DIC in the right way. But I confirm that Nifr his solution is the 'easiest' one. Therefore I will accept your since it fits more to my needs. May 27, 2013 at 8:16
  • 1
    As of Symfony 2.4: WE DO HAVE a tag called console.command, yay! Oct 16, 2013 at 8:34
1

The quickest solution to have your services available is just extending Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand in your Command.

use Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand;

class AnonymizeCommand extends ContainerAwareCommand
{ 
   // ...

You will then have access to the container as usual and can pull in dependencies with

$myService = $this->container->get('service-name');

Yes i know injecting the whole container is usually not recommended for performance reasons and various others...

To debug tags within your container ( available since 2.2)

app/console container:debug --tags

debug a single tag with

app/console container:debug --tag=doctrine.event_listener

search for command related tags with grep ( or findstr if you are on windows )

app/console container:debug --tags | grep command

If you are using symfony2 you are not able to debug with these commands. In that case you should give the following 2 bundles a try. Both provide information about the container in the Profiler.

5
  • Thanks but I was aware of using the ContainerAwareCommand classes and then using the whole container but like you said I don't like this approach cause it makes it harder testable. Also I am running Symfony 2.1. May 23, 2013 at 10:38
  • updated the answer with hint to bundles able to debug the container. please check :) May 23, 2013 at 11:00
  • Thanks! I installed both bundles, and as far I can see the service atlas_cli.command.schema is loaded. But the bundles you mentioned doesn't look like they can handle tags? May 23, 2013 at 11:33
  • Damn i though they were able - haven't used them for a while. thinking about another solution for you with 2.1. May 23, 2013 at 15:18
  • Yes me too, but I see that commands are added in the registerCommands function of the bundle class. The problem is that from there you don't have access to the containerBuilder so you cannot find any tagged services.. May 23, 2013 at 16:58
0

The component which searches for console.command and adds them is AddConsoleCommandPass:

public function build(ContainerBuilder $container) {
    parent::build($container); // TODO: Change the autogenerated stub
    $container->addCompilerPass(new AddConsoleCommandPass());
}

This is automatically done if you add the FrameworkBundle in your kernel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.