2013-10-01 41 views
0

我正在构建控制台应用程序,并希望使用已编写的代码而不进行修改。但是,我需要一个命令的输出。是否有可能这样做:PHP:是否有可能获得非阻塞输出缓冲区?

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框架。任何想法赞赏。

回答

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