Php Symfony命令行程序从服务输出

Php Symfony命令行程序从服务输出,php,symfony,Php,Symfony,我有一个Symfony服务,它可以处理文件并处理其信息。我从控制器调用此服务,并与命令类分开调用。实际的服务需要很长时间才能运行,我想在它处理文件时在命令行上显示一些状态输出。在我的服务中不添加echo命令的情况下,实现这一点的最佳方法是什么 编辑 这似乎是解决方案:这似乎是解决方案:有如下命令 $output->write('Blah blah blah'); $output->writeLn('Blah blah blah'); // Above with a line bre

我有一个Symfony服务,它可以处理文件并处理其信息。我从控制器调用此服务,并与命令类分开调用。实际的服务需要很长时间才能运行,我想在它处理文件时在命令行上显示一些状态输出。在我的服务中不添加echo命令的情况下,实现这一点的最佳方法是什么

编辑
这似乎是解决方案:

这似乎是解决方案:

有如下命令

$output->write('Blah blah blah');

$output->writeLn('Blah blah blah'); // Above with a line break
你也可以添加颜色和进度条,可能还有其他我从来没用过的东西。

更新

您可以使用
eventdispatcher
服务更新服务中事件的命令。 例如

你指挥

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');
        }
    });

    //....
}
很明显,你和你的服务都有很多,但这给了你如何把它们联系在一起的基本方法

这里可以看到一个实际的使用示例

命令-


服务-

是的,我知道这一点,但是,我调用的服务可以从我的应用程序的不同区域运行,因此我无法使用$output命令添加服务。解决方案如我所列-添加记录器,然后将记录器输出挂接到控制台。如果您在不同的服务中进行处理,则可以使用事件调度器服务。我将以这种方式更新我的答案。比我的方法优雅得多:)老实说,直到我在Sylius PR上看到它,我才想到它。我所有的无命令的东西都充满了肮脏的
echo…\n”
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)
    );

    //...
}