【问题标题】:How to display Command output in real time in Symfony 3.4 when calling from a Controller?从 Controller 调用时,如何在 Symfony 3.4 中实时显示命令输出?
【发布时间】:2019-09-04 07:49:53
【问题描述】:

我在我的控制器调用的 Symfony 3.4 应用程序中有一些命令:我想要实现的是实时获取它们的输出并将其打印在屏幕上。

根据他们的文档,我几乎尝试了有关 Symfony 进程的所有内容,但没有任何效果:我总是得到一个空输出(如果我转储内容,它会给我一个空字符串)。

我的命令:

class TestCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('say:hello');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        for ($i = 1; $i <= 5; $i++) {
            $output->writeln(str_pad("$i: Hello Ale", 4096));

            sleep(1);
        }
    }
}

我的控制器:

    /**
     * @Route("/test")
     */
    public function testRoute() {
        $process = new Process(['cd', '/Applications/MAMP/htdocs/MyTestProject/', '&&', 'php bin/console say:hello']);
        $process->start();
        while($process->isRunning()) {
            dump($process->getIncrementalOutput());
        }

        die();
    }

转储的输出:

【问题讨论】:

  • 你试过echo $process-&gt;getIncrementalOutput();吗?
  • 是的,我做到了,但什么也没发生

标签: symfony symfony-3.4 symfony-console


【解决方案1】:

在控制器中尝试使用:

 $process = new Process('php /Applications/MAMP/htdocs/MyTestProject/bin/console say:hello');

 $process->run(function ($type, $buffer) {
    echo $buffer
 });

Getting real-time Process Output

【讨论】:

  • 是的,有很多时间!什么都没发生 :(
  • 没有错误:但我想编辑我之前的答案:我实际上得到了一个输出,但是所有输出都是在执行结束时打印的,而不是实时打印的
  • 尝试在进程执行后添加这个 if (!$process->isSuccessful()) { throw new ProcessFailedException($process); } 回声 $process->getOutput();
  • 您使用什么操作系统/环境?
猜你喜欢
  • 1970-01-01
  • 2020-02-06
  • 2012-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 2019-01-11
相关资源
最近更新 更多