You are here

class OutputFormatter in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Formatter/OutputFormatter.php \Symfony\Component\Console\Formatter\OutputFormatter

Formatter class for console output.

@author Konstantin Kudryashov <ever.zet@gmail.com>

Hierarchy

Expanded class hierarchy of OutputFormatter

5 files declare their use of OutputFormatter
FormatterHelper.php in vendor/symfony/console/Helper/FormatterHelper.php
NullOutput.php in vendor/symfony/console/Output/NullOutput.php
Output.php in vendor/symfony/console/Output/Output.php
OutputFormatterTest.php in vendor/symfony/console/Tests/Formatter/OutputFormatterTest.php
SymfonyStyle.php in vendor/symfony/console/Style/SymfonyStyle.php

File

vendor/symfony/console/Formatter/OutputFormatter.php, line 19

Namespace

Symfony\Component\Console\Formatter
View source
class OutputFormatter implements OutputFormatterInterface {
  private $decorated;
  private $styles = array();
  private $styleStack;

  /**
   * Escapes "<" special char in given text.
   *
   * @param string $text Text to escape
   *
   * @return string Escaped text
   */
  public static function escape($text) {
    return preg_replace('/([^\\\\]?)</', '$1\\<', $text);
  }

  /**
   * Initializes console output formatter.
   *
   * @param bool                            $decorated Whether this formatter should actually decorate strings
   * @param OutputFormatterStyleInterface[] $styles    Array of "name => FormatterStyle" instances
   */
  public function __construct($decorated = false, array $styles = array()) {
    $this->decorated = (bool) $decorated;
    $this
      ->setStyle('error', new OutputFormatterStyle('white', 'red'));
    $this
      ->setStyle('info', new OutputFormatterStyle('green'));
    $this
      ->setStyle('comment', new OutputFormatterStyle('yellow'));
    $this
      ->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
    foreach ($styles as $name => $style) {
      $this
        ->setStyle($name, $style);
    }
    $this->styleStack = new OutputFormatterStyleStack();
  }

  /**
   * Sets the decorated flag.
   *
   * @param bool $decorated Whether to decorate the messages or not
   */
  public function setDecorated($decorated) {
    $this->decorated = (bool) $decorated;
  }

  /**
   * Gets the decorated flag.
   *
   * @return bool true if the output will decorate messages, false otherwise
   */
  public function isDecorated() {
    return $this->decorated;
  }

  /**
   * Sets a new style.
   *
   * @param string                        $name  The style name
   * @param OutputFormatterStyleInterface $style The style instance
   */
  public function setStyle($name, OutputFormatterStyleInterface $style) {
    $this->styles[strtolower($name)] = $style;
  }

  /**
   * Checks if output formatter has style with specified name.
   *
   * @param string $name
   *
   * @return bool
   */
  public function hasStyle($name) {
    return isset($this->styles[strtolower($name)]);
  }

  /**
   * Gets style options from style with specified name.
   *
   * @param string $name
   *
   * @return OutputFormatterStyleInterface
   *
   * @throws \InvalidArgumentException When style isn't defined
   */
  public function getStyle($name) {
    if (!$this
      ->hasStyle($name)) {
      throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
    }
    return $this->styles[strtolower($name)];
  }

  /**
   * Formats a message according to the given styles.
   *
   * @param string $message The message to style
   *
   * @return string The styled message
   */
  public function format($message) {
    $message = (string) $message;
    $offset = 0;
    $output = '';
    $tagRegex = '[a-z][a-z0-9_=;-]*';
    preg_match_all("#<(({$tagRegex}) | /({$tagRegex})?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
    foreach ($matches[0] as $i => $match) {
      $pos = $match[1];
      $text = $match[0];
      if (0 != $pos && '\\' == $message[$pos - 1]) {
        continue;
      }

      // add the text up to the next tag
      $output .= $this
        ->applyCurrentStyle(substr($message, $offset, $pos - $offset));
      $offset = $pos + strlen($text);

      // opening tag?
      if ($open = '/' != $text[1]) {
        $tag = $matches[1][$i][0];
      }
      else {
        $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
      }
      if (!$open && !$tag) {

        // </>
        $this->styleStack
          ->pop();
      }
      elseif (false === ($style = $this
        ->createStyleFromString(strtolower($tag)))) {
        $output .= $this
          ->applyCurrentStyle($text);
      }
      elseif ($open) {
        $this->styleStack
          ->push($style);
      }
      else {
        $this->styleStack
          ->pop($style);
      }
    }
    $output .= $this
      ->applyCurrentStyle(substr($message, $offset));
    return str_replace('\\<', '<', $output);
  }

  /**
   * @return OutputFormatterStyleStack
   */
  public function getStyleStack() {
    return $this->styleStack;
  }

  /**
   * Tries to create new style instance from string.
   *
   * @param string $string
   *
   * @return OutputFormatterStyle|bool false if string is not format string
   */
  private function createStyleFromString($string) {
    if (isset($this->styles[$string])) {
      return $this->styles[$string];
    }
    if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
      return false;
    }
    $style = new OutputFormatterStyle();
    foreach ($matches as $match) {
      array_shift($match);
      if ('fg' == $match[0]) {
        $style
          ->setForeground($match[1]);
      }
      elseif ('bg' == $match[0]) {
        $style
          ->setBackground($match[1]);
      }
      else {
        try {
          $style
            ->setOption($match[1]);
        } catch (\InvalidArgumentException $e) {
          return false;
        }
      }
    }
    return $style;
  }

  /**
   * Applies current style from stack to text, if must be applied.
   *
   * @param string $text Input text
   *
   * @return string Styled text
   */
  private function applyCurrentStyle($text) {
    return $this
      ->isDecorated() && strlen($text) > 0 ? $this->styleStack
      ->getCurrent()
      ->apply($text) : $text;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OutputFormatter::$decorated private property
OutputFormatter::$styles private property
OutputFormatter::$styleStack private property
OutputFormatter::applyCurrentStyle private function Applies current style from stack to text, if must be applied.
OutputFormatter::createStyleFromString private function Tries to create new style instance from string.
OutputFormatter::escape public static function Escapes "<" special char in given text.
OutputFormatter::format public function Formats a message according to the given styles. Overrides OutputFormatterInterface::format
OutputFormatter::getStyle public function Gets style options from style with specified name. Overrides OutputFormatterInterface::getStyle
OutputFormatter::getStyleStack public function
OutputFormatter::hasStyle public function Checks if output formatter has style with specified name. Overrides OutputFormatterInterface::hasStyle
OutputFormatter::isDecorated public function Gets the decorated flag. Overrides OutputFormatterInterface::isDecorated
OutputFormatter::setDecorated public function Sets the decorated flag. Overrides OutputFormatterInterface::setDecorated
OutputFormatter::setStyle public function Sets a new style. Overrides OutputFormatterInterface::setStyle
OutputFormatter::__construct public function Initializes console output formatter.