如何在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

查看:179
本文介绍了如何在Symfony2应用程序的控制器中执行命令并在Twig模板中实时打印输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Symfony2应用程序的控制器内执行一条持久命令,并将终端的输出实时返回给用户.

I need to execute a long-lasting command within a controller of my Symfony2 application and to return to the user in real time the output of the terminal.

我已阅读:

http://symfony.com/doc/current/components/process.html#getting-real-time-process-output

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

I can't figure out how to print in real time the terminal output in a Twig template.

多亏了Matteo的代码和用户的评论,最终的实现是:

Thanks to the Matteo's code and users comments, the final implementation is:

/**
 * @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脚本并捕获输出,则可以使用

If you need lo launch a simple shell script and capture the output, you can use a StreamedResponse in conjunction with the Process callback you posted.

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

As Example, suppose you have a very simple bash script like follow:

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

根据缓冲区的长度,您可以得到类似以下的输出:

And depends on the buffer length you can have an output like:

.....
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 
.....

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

You can wrap this in a portion of a page with a render controller as example:

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

更多信息此处

希望获得帮助

这篇关于如何在Symfony2应用程序的控制器中执行命令并在Twig模板中实时打印输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆