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