Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I listen to console events in symfony?

Tags:

symfony

I'm trying to hook into symfonys console events with the symfony standard edition (2.3), but it just won't work.

I created a listener according to their example and follow the guides on event registration:

namespace Acme\DemoBundle\EventListener;

use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;

class AcmeCommandListener
{
    public function onConsoleCommand(ConsoleCommandEvent $event) {
        // get the output instance
        $output = $event->getOutput();

        // get the command to be executed
        $command = $event->getCommand();

        // write something about the command
        $output->writeln(sprintf('Before running command <info>%s</info>', $command->getName()));
    }
}

and someone on the mailing list told me to register it as event in the service container. So I did this:

services:
    kernel.listener.command_dispatch:
        class: Acme\DemoBundle\EventListener\AcmeCommandListener
        tags:
            - { name: kernel.event_listener, event: console.command }

But obviously the tagging is not correct and I can't find the correct names for that. How would I do that?

like image 211
acme Avatar asked Sep 04 '13 15:09

acme


2 Answers

Platform\EventListener\Console\InitListener:
    tags:
        - { name: kernel.event_listener, event: console.command, priority: 1024 }

<?php 
class CustomListener
{
    public function onConsoleCommand(ConsoleCommandEvent $event): void
    {
        //do somehting
    }
}

?>
like image 52
Andrey Korolyov Avatar answered Oct 08 '22 10:10

Andrey Korolyov


So, I finally got it. The above code in the original post ist fully working, but I defined my services.yml within my bundle not in the application config app/config.yml. This means, the configuration was never loaded. I had to import the configuration via container extensions:

# Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php
namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

class AcmeDemoExtension extends Extension
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

and

# Acme/DemoBundle/DependencyInjection/Configuration.php
namespace Acme\DemoBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('headwork_legacy');
        return $treeBuilder;
    }
}

Though I guess you can even leave out the $configuration = new Configuration(); part and the Configuration class.

like image 40
acme Avatar answered Oct 08 '22 10:10

acme