CommonFunctions::_timeoutfgets PHP Method

_timeoutfgets() private static method

get the content of stdout/stderr with the option to set a timeout for reading
private static _timeoutfgets ( array $pipes, &$out, &$err, integer $timeout = 30 ) : void
$pipes array array of file pointers for stdin, stdout, stderr (proc_open())
$timeout integer timeout value in seconds (default value is 30)
return void
    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);
                }
            }
        }
    }