annotate vendor/psy/psysh/src/Psy/Output/ProcOutputPager.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Output;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Output\StreamOutput;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * ProcOutputPager class.
Chris@0 18 *
Chris@0 19 * A ProcOutputPager instance wraps a regular StreamOutput's stream. Rather
Chris@0 20 * than writing directly to the stream, it shells out to a pager process and
Chris@0 21 * gives that process the stream as stdout. This means regular *nix commands
Chris@0 22 * like `less` and `more` can be used to page large amounts of output.
Chris@0 23 */
Chris@0 24 class ProcOutputPager extends StreamOutput implements OutputPager
Chris@0 25 {
Chris@0 26 private $proc;
Chris@0 27 private $pipe;
Chris@0 28 private $stream;
Chris@0 29 private $cmd;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Constructor.
Chris@0 33 *
Chris@0 34 * @param StreamOutput $output
Chris@0 35 * @param string $cmd Pager process command (default: 'less -R -S -F -X')
Chris@0 36 */
Chris@0 37 public function __construct(StreamOutput $output, $cmd = 'less -R -S -F -X')
Chris@0 38 {
Chris@0 39 $this->stream = $output->getStream();
Chris@0 40 $this->cmd = $cmd;
Chris@0 41 }
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Writes a message to the output.
Chris@0 45 *
Chris@0 46 * @param string $message A message to write to the output
Chris@0 47 * @param bool $newline Whether to add a newline or not
Chris@0 48 *
Chris@0 49 * @throws \RuntimeException When unable to write output (should never happen)
Chris@0 50 */
Chris@0 51 public function doWrite($message, $newline)
Chris@0 52 {
Chris@0 53 $pipe = $this->getPipe();
Chris@0 54 if (false === @fwrite($pipe, $message . ($newline ? PHP_EOL : ''))) {
Chris@0 55 // @codeCoverageIgnoreStart
Chris@0 56 // should never happen
Chris@0 57 throw new \RuntimeException('Unable to write output.');
Chris@0 58 // @codeCoverageIgnoreEnd
Chris@0 59 }
Chris@0 60
Chris@0 61 fflush($pipe);
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Close the current pager process.
Chris@0 66 */
Chris@0 67 public function close()
Chris@0 68 {
Chris@0 69 if (isset($this->pipe)) {
Chris@0 70 fclose($this->pipe);
Chris@0 71 }
Chris@0 72
Chris@0 73 if (isset($this->proc)) {
Chris@0 74 $exit = proc_close($this->proc);
Chris@0 75 if ($exit !== 0) {
Chris@0 76 throw new \RuntimeException('Error closing output stream');
Chris@0 77 }
Chris@0 78 }
Chris@0 79
Chris@0 80 unset($this->pipe, $this->proc);
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Get a pipe for paging output.
Chris@0 85 *
Chris@0 86 * If no active pager process exists, fork one and return its input pipe.
Chris@0 87 */
Chris@0 88 private function getPipe()
Chris@0 89 {
Chris@0 90 if (!isset($this->pipe) || !isset($this->proc)) {
Chris@0 91 $desc = array(array('pipe', 'r'), $this->stream, fopen('php://stderr', 'w'));
Chris@0 92 $this->proc = proc_open($this->cmd, $desc, $pipes);
Chris@0 93
Chris@0 94 if (!is_resource($this->proc)) {
Chris@0 95 throw new \RuntimeException('Error opening output stream');
Chris@0 96 }
Chris@0 97
Chris@0 98 $this->pipe = $pipes[0];
Chris@0 99 }
Chris@0 100
Chris@0 101 return $this->pipe;
Chris@0 102 }
Chris@0 103 }