Chris@0: initFormatters(); Chris@0: Chris@0: if ($pager === null) { Chris@0: $this->pager = new PassthruPager($this); Chris@0: } elseif (is_string($pager)) { Chris@0: $this->pager = new ProcOutputPager($this, $pager); Chris@0: } elseif ($pager instanceof OutputPager) { Chris@0: $this->pager = $pager; Chris@0: } else { Chris@0: throw new \InvalidArgumentException('Unexpected pager parameter: ' . $pager); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Page multiple lines of output. Chris@0: * Chris@0: * The output pager is started Chris@0: * Chris@0: * If $messages is callable, it will be called, passing this output instance Chris@0: * for rendering. Otherwise, all passed $messages are paged to output. Chris@0: * Chris@0: * Upon completion, the output pager is flushed. Chris@0: * Chris@0: * @param string|array|\Closure $messages A string, array of strings or a callback Chris@0: * @param int $type (default: 0) Chris@0: */ Chris@0: public function page($messages, $type = 0) Chris@0: { Chris@0: if (is_string($messages)) { Chris@0: $messages = (array) $messages; Chris@0: } Chris@0: Chris@0: if (!is_array($messages) && !is_callable($messages)) { Chris@0: throw new \InvalidArgumentException('Paged output requires a string, array or callback.'); Chris@0: } Chris@0: Chris@0: $this->startPaging(); Chris@0: Chris@0: if (is_callable($messages)) { Chris@0: $messages($this); Chris@0: } else { Chris@0: $this->write($messages, true, $type); Chris@0: } Chris@0: Chris@0: $this->stopPaging(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Start sending output to the output pager. Chris@0: */ Chris@0: public function startPaging() Chris@0: { Chris@0: $this->paging++; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Stop paging output and flush the output pager. Chris@0: */ Chris@0: public function stopPaging() Chris@0: { Chris@0: $this->paging--; Chris@0: $this->closePager(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a message to the output. Chris@0: * Chris@0: * Optionally, pass `$type | self::NUMBER_LINES` as the $type parameter to Chris@0: * number the lines of output. Chris@0: * Chris@0: * @throws \InvalidArgumentException When unknown output type is given Chris@0: * Chris@0: * @param string|array $messages The message as an array of lines or a single string Chris@0: * @param bool $newline Whether to add a newline or not Chris@0: * @param int $type The type of output Chris@0: */ Chris@0: public function write($messages, $newline = false, $type = 0) Chris@0: { Chris@0: if ($this->getVerbosity() === self::VERBOSITY_QUIET) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $messages = (array) $messages; Chris@0: Chris@0: if ($type & self::NUMBER_LINES) { Chris@0: $pad = strlen((string) count($messages)); Chris@0: $template = $this->isDecorated() ? ": %s" : "%{$pad}s: %s"; Chris@0: Chris@0: if ($type & self::OUTPUT_RAW) { Chris@0: $messages = array_map(array('Symfony\Component\Console\Formatter\OutputFormatter', 'escape'), $messages); Chris@0: } Chris@0: Chris@0: foreach ($messages as $i => $line) { Chris@0: $messages[$i] = sprintf($template, $i, $line); Chris@0: } Chris@0: Chris@0: // clean this up for super. Chris@0: $type = $type & ~self::NUMBER_LINES & ~self::OUTPUT_RAW; Chris@0: } Chris@0: Chris@0: parent::write($messages, $newline, $type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a message to the output. Chris@0: * Chris@0: * Handles paged output, or writes directly to the output stream. 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: public function doWrite($message, $newline) Chris@0: { Chris@0: if ($this->paging > 0) { Chris@0: $this->pager->doWrite($message, $newline); Chris@0: } else { Chris@0: parent::doWrite($message, $newline); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Flush and close the output pager. Chris@0: */ Chris@0: private function closePager() Chris@0: { Chris@0: if ($this->paging <= 0) { Chris@0: $this->pager->close(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initialize output formatter styles. Chris@0: */ Chris@0: private function initFormatters() Chris@0: { Chris@0: $formatter = $this->getFormatter(); Chris@0: Chris@0: $formatter->setStyle('warning', new OutputFormatterStyle('black', 'yellow')); Chris@0: $formatter->setStyle('error', new OutputFormatterStyle('black', 'red', array('bold'))); Chris@0: $formatter->setStyle('aside', new OutputFormatterStyle('blue')); Chris@0: $formatter->setStyle('strong', new OutputFormatterStyle(null, null, array('bold'))); Chris@0: $formatter->setStyle('return', new OutputFormatterStyle('cyan')); Chris@0: $formatter->setStyle('urgent', new OutputFormatterStyle('red')); Chris@0: $formatter->setStyle('hidden', new OutputFormatterStyle('black')); Chris@0: Chris@0: // Visibility Chris@0: $formatter->setStyle('public', new OutputFormatterStyle(null, null, array('bold'))); Chris@0: $formatter->setStyle('protected', new OutputFormatterStyle('yellow')); Chris@0: $formatter->setStyle('private', new OutputFormatterStyle('red')); Chris@0: $formatter->setStyle('global', new OutputFormatterStyle('cyan', null, array('bold'))); Chris@0: $formatter->setStyle('const', new OutputFormatterStyle('cyan')); Chris@0: $formatter->setStyle('class', new OutputFormatterStyle('blue', null, array('underscore'))); Chris@0: $formatter->setStyle('function', new OutputFormatterStyle(null)); Chris@0: $formatter->setStyle('default', new OutputFormatterStyle(null)); Chris@0: Chris@0: // Types Chris@0: $formatter->setStyle('number', new OutputFormatterStyle('magenta')); Chris@0: $formatter->setStyle('string', new OutputFormatterStyle('green')); Chris@0: $formatter->setStyle('bool', new OutputFormatterStyle('cyan')); Chris@0: $formatter->setStyle('keyword', new OutputFormatterStyle('yellow')); Chris@0: $formatter->setStyle('comment', new OutputFormatterStyle('blue')); Chris@0: $formatter->setStyle('object', new OutputFormatterStyle('blue')); Chris@0: $formatter->setStyle('resource', new OutputFormatterStyle('yellow')); Chris@0: } Chris@0: }