Support for Events in n98-magerun

Published by Christian Münch on

In version 1.77.0 we added the Symfony EventManager compontent to n98-magerun.
The event manager let you hook in the core commands.

Currently there are three core events which are available:

console.command – Before a command runs

console.terminate – After a command execution

console.exception – If a command stops with an exception

How can i register an observer?

n98-magerun support registration of Symfony subscriber objects.
This can easily done by a config.

You should define your subscriber class in config node “event -> subscriber”.

event:
  subscriber:
    - My\Namespace\MyEventSubscriber

The subscriber class must implement EventSubscriberInterface.

Example:

<?php

namespace My\Namespace;

use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MyEventSubscriber implements EventSubscriberInterface
{   
    /** 
    * Returns an array of event names this subscriber wants to listen to.
    *
    * @return array The event names to listen to
    *
    * @api
    */
    public static function getSubscribedEvents()
    {   
        return array('console.command' => 'logCommandName');
    }

    public function logCommandName(ConsoleEvent $event)
    {
        $event->getOutput()->writeln(
            $event->getCommand()->getName()
        );
    }
}

The system is simple. All subscribers return a list of subscribed events.
If a event is dispatched all subsribers will be called automatically.
In our example we should see the name of any command in our terminal.
This offers you new features like a output logger.

We will add more events in the future which let you extend the core functions of n98-magerun.

Happy Coding!

Categories: Magerun

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.