X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Fsrc%2FPsy%2FOutput%2FProcOutputPager.php;fp=vendor%2Fpsy%2Fpsysh%2Fsrc%2FPsy%2FOutput%2FProcOutputPager.php;h=bb41c6cc961d414ce08e389b35955df4bda8339c;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/psy/psysh/src/Psy/Output/ProcOutputPager.php b/vendor/psy/psysh/src/Psy/Output/ProcOutputPager.php new file mode 100644 index 000000000..bb41c6cc9 --- /dev/null +++ b/vendor/psy/psysh/src/Psy/Output/ProcOutputPager.php @@ -0,0 +1,103 @@ +stream = $output->getStream(); + $this->cmd = $cmd; + } + + /** + * Writes a message to the output. + * + * @param string $message A message to write to the output + * @param bool $newline Whether to add a newline or not + * + * @throws \RuntimeException When unable to write output (should never happen) + */ + public function doWrite($message, $newline) + { + $pipe = $this->getPipe(); + if (false === @fwrite($pipe, $message . ($newline ? PHP_EOL : ''))) { + // @codeCoverageIgnoreStart + // should never happen + throw new \RuntimeException('Unable to write output.'); + // @codeCoverageIgnoreEnd + } + + fflush($pipe); + } + + /** + * Close the current pager process. + */ + public function close() + { + if (isset($this->pipe)) { + fclose($this->pipe); + } + + if (isset($this->proc)) { + $exit = proc_close($this->proc); + if ($exit !== 0) { + throw new \RuntimeException('Error closing output stream'); + } + } + + unset($this->pipe, $this->proc); + } + + /** + * Get a pipe for paging output. + * + * If no active pager process exists, fork one and return its input pipe. + */ + private function getPipe() + { + if (!isset($this->pipe) || !isset($this->proc)) { + $desc = array(array('pipe', 'r'), $this->stream, fopen('php://stderr', 'w')); + $this->proc = proc_open($this->cmd, $desc, $pipes); + + if (!is_resource($this->proc)) { + throw new \RuntimeException('Error opening output stream'); + } + + $this->pipe = $pipes[0]; + } + + return $this->pipe; + } +}