我有一个Symfony服务,它处理文件并处理其信息.我从控制器调用此服务,并与命令类分开调用.实际服务需要很长时间才能运行,我想在处理文件时在命令行上显示一些状态输出.如果不在我的服务中添加echo命令,最好的方法是什么?
编辑这似乎是解决方案:http://symfony.com/blog/new-in-symfony-2-4-show-logs-in-console
有像这样的命令
$output->write('Blah blah blah');
$output->writeLn('Blah blah blah'); // Above with a line break
Run Code Online (Sandbox Code Playgroud)
你也可以添加颜色和进度条以及其他我从未使用过的东西. http://symfony.com/doc/current/components/console/introduction.html#coloring-the-output
UPDATE
您可以使用该EventDisptcher服务更新服务中事件的命令.例如...
你命令
protected function execute(InputInterface $input, OutputInterface $output)
{
//....
$dispatcher = $this->getContainer->get('event_dispatcher');
$dispatcher->addListener(
'an.event.that.you.have.set.up',
function (GenericEvent $event) use ($output) {
$output->writeLn('<info>This event has happened</info');
}
});
//....
}
Run Code Online (Sandbox Code Playgroud)
你的服务
protected $dispatcher;
//....
public function __construct(EventDispatcherInterface $dispatcher, ...)
{
$this->dispatcher = $dispatcher;
//...
}
public function someFunction()
{
//...
$variable = 'something you are using');
$dispatcher->dispatch(
'an.event.that.you.have.set.up',
new GenericEvent($variable)
);
//...
}
Run Code Online (Sandbox Code Playgroud)
显然,你指挥你的服务会有更多的东西,但这就是如何将它们联系在一起的基础.
这里可以看到一个实际的用例.