PHP:是否可以获得非阻塞输出缓冲区?

时间:2013-10-01 12:01:20

标签: php console-application

我正在构建一个控制台应用程序,并希望在不修改它的情况下使用已编写的代码。但是,我需要输出命令。有可能做这样的事情:

function func() {
    /* Let's say this function contains already written
        code and it would be easier to rewrite it than modify */
    echo 'Confirm action';
    $input = fgets(fopen('php://stdin', 'r'));
    if ($input == 'y') echo 'okay';
    else echo 'no';
    return 0;
}

$code = func();
// func() returns non zero only on error and confirming/declining an action returns 0.
// I want to know if the action was confirmed.
// Using ob_start() prevents echo from working in the function,
// i.e. the user sees a blank screen waiting for input.

这甚至可能吗?

我用Yii框架写这个。任何想法都赞赏。

1 个答案:

答案 0 :(得分:0)

用popen解决了这个问题,例如:

    $handle = popen('php yiic migrate create '.$name, 'r');
    $output = '';
    while (!feof($handle))
    {
        $read = fread($handle, 2096);
        $output .= $read;
        echo $read;
    }
    $exitCode = pclose($handle);