覆盖 Symfony\Component\Console\Commande\Command.php
Override Symfony\Component\Console\Commande\Command.php
我正在我们的 symfony2 应用程序上开发某种 cronjob 监控。
我创建了一个带有 'completed' 属性 的 CommandExecution 实体。
我正在使用 console events 创建并保留此实体。
我的服务:
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 }
和ConsoleListener:onConsoleCommand() 和ConsoleListener:onConsoleTerminate() 方法在命令开始和结束执行时调用:
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();
}
正如你在这些方法中看到的,我想添加一个 commandExecution 属性 到 Symfony 组件 Console\Command\Command.php,这样我就可以传入我的 commandExecution 实体和更改其状态。
我必须重写这个组件吗?如果是,如何?或者我可以用更简单的方法来做吗?
在您的 ConsoleListener
中添加 commandExecution
属性
protected $commandExecution = null;
然后在你的onConsoleCommand()
方法中设置它
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;
}
然后你可以在你的onConsoleTerminate()
方法中访问它
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();
}
不要忘记在 onConsoleTerminate()
方法中测试 commandExecution
值是否为空
我正在我们的 symfony2 应用程序上开发某种 cronjob 监控。
我创建了一个带有 'completed' 属性 的 CommandExecution 实体。
我正在使用 console events 创建并保留此实体。
我的服务:
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 }
和ConsoleListener:onConsoleCommand() 和ConsoleListener:onConsoleTerminate() 方法在命令开始和结束执行时调用:
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();
}
正如你在这些方法中看到的,我想添加一个 commandExecution 属性 到 Symfony 组件 Console\Command\Command.php,这样我就可以传入我的 commandExecution 实体和更改其状态。
我必须重写这个组件吗?如果是,如何?或者我可以用更简单的方法来做吗?
在您的 ConsoleListener
commandExecution
属性
protected $commandExecution = null;
然后在你的onConsoleCommand()
方法中设置它
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;
}
然后你可以在你的onConsoleTerminate()
方法中访问它
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();
}
不要忘记在 onConsoleTerminate()
方法中测试 commandExecution
值是否为空