Chris@0: stream = $output->getStream(); Chris@0: $this->cmd = $cmd; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a message to the output. Chris@0: * Chris@0: * @param string $message A message to write to the output Chris@0: * @param bool $newline Whether to add a newline or not Chris@0: * Chris@0: * @throws \RuntimeException When unable to write output (should never happen) Chris@0: */ Chris@0: public function doWrite($message, $newline) Chris@0: { Chris@0: $pipe = $this->getPipe(); Chris@0: if (false === @fwrite($pipe, $message . ($newline ? PHP_EOL : ''))) { Chris@0: // @codeCoverageIgnoreStart Chris@0: // should never happen Chris@0: throw new \RuntimeException('Unable to write output.'); Chris@0: // @codeCoverageIgnoreEnd Chris@0: } Chris@0: Chris@0: fflush($pipe); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Close the current pager process. Chris@0: */ Chris@0: public function close() Chris@0: { Chris@0: if (isset($this->pipe)) { Chris@0: fclose($this->pipe); Chris@0: } Chris@0: Chris@0: if (isset($this->proc)) { Chris@0: $exit = proc_close($this->proc); Chris@0: if ($exit !== 0) { Chris@0: throw new \RuntimeException('Error closing output stream'); Chris@0: } Chris@0: } Chris@0: Chris@0: unset($this->pipe, $this->proc); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Get a pipe for paging output. Chris@0: * Chris@0: * If no active pager process exists, fork one and return its input pipe. Chris@0: */ Chris@0: private function getPipe() Chris@0: { Chris@0: if (!isset($this->pipe) || !isset($this->proc)) { Chris@0: $desc = array(array('pipe', 'r'), $this->stream, fopen('php://stderr', 'w')); Chris@0: $this->proc = proc_open($this->cmd, $desc, $pipes); Chris@0: Chris@0: if (!is_resource($this->proc)) { Chris@0: throw new \RuntimeException('Error opening output stream'); Chris@0: } Chris@0: Chris@0: $this->pipe = $pipes[0]; Chris@0: } Chris@0: Chris@0: return $this->pipe; Chris@0: } Chris@0: }