如何在Symfony2应用程序的控制器中执行命令,并在Twig模板中实时打印输出


How to execute a command within a controller of a Symfony2 application and print in real-time the output in a Twig template

我需要在Symfony2应用程序的控制器中执行一个持久的命令,并实时向用户返回终端的输出。

我读过这个:

http://symfony.com/doc/current/components/process.html#getting-实时过程输出

我不知道如何在Twig模板中实时打印终端输出。

编辑:感谢Matteo的代码和用户的评论,最终实现是:

/**
 * @Route("/genera-xxx-r", name="commission_generate_r_xxx")
 * @Method({"GET"})
 */
public function generateRXXXsAction()
{
    //remove time constraints if your script last very long
    set_time_limit(0);        
    $rFolderPath = $this->container->getParameter('xxx_settings')['r_setting_folder_path'];
    $script = 'R --slave -f ' . $rFolderPath . 'main.R';
    $response = new StreamedResponse();
    $process = new Process($script);
    $response->setCallback(function() use ($process) {
        $process->run(function ($type, $buffer) {
            //if you don't want to render a template, please refer to the @Matteo's reply
            echo $this->renderView('AppBundle:Commission:_process.html.twig',
                array(
                    'type' => $type,
                    'buffer' => $buffer
                ));
            //according to @Ilmari Karonen a flush call could fix some buffering issues
            flush();
        });
    });
    $response->setStatusCode(200);
    return $response;
}

如果需要启动一个简单的shell脚本并捕获输出,可以将StreamedResponse与发布的Process回调结合使用。

作为示例,假设您有一个非常简单的bash脚本,如下所示:

loop.sh

for i in {1..500}
do
   echo "Welcome $i times"
done

你可以执行你的行动,比如:

/**
 * @Route("/process", name="_processaction")
 */
public function processAction()
{
    // If your script take a very long time:
    // set_time_limit(0);
    $script='/path-script/.../loop.sh';
    $process = new Process($script);
    $response->setCallback(function() use ($process) {
        $process->run(function ($type, $buffer) {
            if (Process::ERR === $type) {
                echo 'ERR > '.$buffer;
            } else {
                echo 'OUT > '.$buffer;
                echo '<br>';
            }
        });
    });
    $response->setStatusCode(200);
    return $response;
}

根据缓冲区的长度,你可以得到一个输出,比如:

.....
OUT > Welcome 40 times Welcome 41 times 
OUT > Welcome 42 times Welcome 43 times 
OUT > Welcome 44 times Welcome 45 times 
OUT > Welcome 46 times Welcome 47 times 
OUT > Welcome 48 times 
OUT > Welcome 49 times Welcome 50 times 
OUT > Welcome 51 times Welcome 52 times 
OUT > Welcome 53 times 
.....

您可以使用渲染控制器将其封装在页面的一部分中,例如:

<div id="process">
    {{ render(controller(
        'AcmeDemoBundle:Test:processAction'
    )) }}
</div>

更多信息点击这里

希望这能帮助

相关文章: