private static function _timeoutfgets($pipes, &$out, &$err, $timeout = 30)
{
$w = null;
$e = null;
if (defined("PSI_MODE_POPEN") && PSI_MODE_POPEN === true) {
$pipe2 = false;
} else {
$pipe2 = true;
}
while (!(feof($pipes[1]) && (!$pipe2 || feof($pipes[2])))) {
if ($pipe2) {
$read = array($pipes[1], $pipes[2]);
} else {
$read = array($pipes[1]);
}
$n = stream_select($read, $w, $e, $timeout);
if ($n === false) {
error_log('stream_select: failed !');
break;
} elseif ($n === 0) {
error_log('stream_select: timeout expired !');
break;
}
foreach ($read as $r) {
if ($r == $pipes[1]) {
$out .= fread($r, 4096);
} elseif (feof($pipes[1]) && $pipe2 && $r == $pipes[2]) {
//read STDERR after STDOUT
$err .= fread($r, 4096);
}
}
}
}