VaN
VaN

Reputation: 2210

Override Symfony\Component\Console\Commande\Command.php

I am developping some kind of cronjob monitoring on our symfony2 application.

I created a CommandExecution entity with a 'completed' property.

I'm using console events to create and persist this entity.

My service :

kernel.listener.console:
        class: Evo\CronBundle\EventListener\ConsoleListener
        arguments: [@doctrine.orm.entity_manager]
        tags:
            - { name: kernel.event_listener, event: console.command, method: onConsoleCommand }
            - { name: kernel.event_listener, event: console.terminate, method: onConsoleTerminate }
            - { name: kernel.event_listener, event: console.exception, method: onConsoleException }

and the ConsoleListener:onConsoleCommand() and ConsoleListener:onConsoleTerminate() methods called when a command starts and ends to execute :

public function onConsoleCommand(ConsoleCommandEvent $event)
{
    $command = $event->getCommand();
    $commandEntity = $this->em->getRepository('EvoCronBundle:Command')->findOneBy(['name' => $command->getName()]);

    $commandExecution = new CommandExecution();
    $commandExecution->setCommand($commandEntity);

    $this->em->persist($commandExecution);
    $this->em->flush();

    // here I want to pass my entity to the command, so I can get it back in the onConsoleTerminate() method
    $command->setCommandExecution($commandExecution);
}

public function onConsoleTerminate(ConsoleTerminateEvent $event)
{
    $command = $event->getCommand();

    // here, retrieve the commandExecution entity passed in onConsoleCommand() method
    $commandExecution = $command->getCommandExecution();

    $commandExecution->setCompleted(true);

    $this->em->flush();
}

As you can see in these methods, I would like to add a commandExecution property to the Symfony Component Console\Command\Command.php, so I can pass in my commandExecution entity and change its status.

Do I have to override this component ? If yes, how ? Or can I do it in a simpler way ?

Upvotes: 0

Views: 279

Answers (1)

Med
Med

Reputation: 2063

Add the commandExecution property in your ConsoleListener

protected $commandExecution = null;

Then set it in your onConsoleCommand() method

public function onConsoleCommand(ConsoleCommandEvent $event)
{
    $command = $event->getCommand();
    $commandEntity =     $this->em->getRepository('EvoCronBundle:Command')->findOneBy(['name' =>     $command->getName()]);

    $commandExecution = new CommandExecution();
    $commandExecution->setCommand($commandEntity);

    $this->em->persist($commandExecution);
    $this->em->flush();

    $this->commandExecution = $commandExecution;
}

Then you can access to it in your onConsoleTerminate() method

public function onConsoleTerminate(ConsoleTerminateEvent $event)
{
    $command = $event->getCommand();

    // here, retrieve the commandExecution entity passed in onConsoleCommand() method
    $commandExecution = $this->commandExecution;

    $commandExecution->setCompleted(true);

    $this->em->flush();
}

Don't forget to test if commandExecution value is null in onConsoleTerminate() method

Upvotes: 1

Related Questions