[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/console/Symfony/Component/Console/Formatter/ -> OutputFormatter.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\Console\Formatter;
  13  
  14  /**
  15   * Formatter class for console output.
  16   *
  17   * @author Konstantin Kudryashov <ever.zet@gmail.com>
  18   */
  19  class OutputFormatter implements OutputFormatterInterface
  20  {
  21      private $decorated;
  22      private $styles = array();
  23      private $styleStack;
  24  
  25      /**
  26       * Escapes "<" special char in given text.
  27       *
  28       * @param string $text Text to escape
  29       *
  30       * @return string Escaped text
  31       */
  32      public static function escape($text)
  33      {
  34          $text = preg_replace('/([^\\\\]?)</', '$1\\<', $text);
  35  
  36          if ('\\' === substr($text, -1)) {
  37              $len = strlen($text);
  38              $text = rtrim($text, '\\');
  39              $text .= str_repeat('<<', $len - strlen($text));
  40          }
  41  
  42          return $text;
  43      }
  44  
  45      /**
  46       * Initializes console output formatter.
  47       *
  48       * @param bool                            $decorated Whether this formatter should actually decorate strings
  49       * @param OutputFormatterStyleInterface[] $styles    Array of "name => FormatterStyle" instances
  50       */
  51      public function __construct($decorated = false, array $styles = array())
  52      {
  53          $this->decorated = (bool) $decorated;
  54  
  55          $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  56          $this->setStyle('info', new OutputFormatterStyle('green'));
  57          $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  58          $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  59  
  60          foreach ($styles as $name => $style) {
  61              $this->setStyle($name, $style);
  62          }
  63  
  64          $this->styleStack = new OutputFormatterStyleStack();
  65      }
  66  
  67      /**
  68       * Sets the decorated flag.
  69       *
  70       * @param bool $decorated Whether to decorate the messages or not
  71       */
  72      public function setDecorated($decorated)
  73      {
  74          $this->decorated = (bool) $decorated;
  75      }
  76  
  77      /**
  78       * Gets the decorated flag.
  79       *
  80       * @return bool true if the output will decorate messages, false otherwise
  81       */
  82      public function isDecorated()
  83      {
  84          return $this->decorated;
  85      }
  86  
  87      /**
  88       * Sets a new style.
  89       *
  90       * @param string                        $name  The style name
  91       * @param OutputFormatterStyleInterface $style The style instance
  92       */
  93      public function setStyle($name, OutputFormatterStyleInterface $style)
  94      {
  95          $this->styles[strtolower($name)] = $style;
  96      }
  97  
  98      /**
  99       * Checks if output formatter has style with specified name.
 100       *
 101       * @param string $name
 102       *
 103       * @return bool
 104       */
 105      public function hasStyle($name)
 106      {
 107          return isset($this->styles[strtolower($name)]);
 108      }
 109  
 110      /**
 111       * Gets style options from style with specified name.
 112       *
 113       * @param string $name
 114       *
 115       * @return OutputFormatterStyleInterface
 116       *
 117       * @throws \InvalidArgumentException When style isn't defined
 118       */
 119      public function getStyle($name)
 120      {
 121          if (!$this->hasStyle($name)) {
 122              throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
 123          }
 124  
 125          return $this->styles[strtolower($name)];
 126      }
 127  
 128      /**
 129       * Formats a message according to the given styles.
 130       *
 131       * @param string $message The message to style
 132       *
 133       * @return string The styled message
 134       */
 135      public function format($message)
 136      {
 137          $message = (string) $message;
 138          $offset = 0;
 139          $output = '';
 140          $tagRegex = '[a-z][a-z0-9_=;-]*+';
 141          preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
 142          foreach ($matches[0] as $i => $match) {
 143              $pos = $match[1];
 144              $text = $match[0];
 145  
 146              if (0 != $pos && '\\' == $message[$pos - 1]) {
 147                  continue;
 148              }
 149  
 150              // add the text up to the next tag
 151              $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
 152              $offset = $pos + strlen($text);
 153  
 154              // opening tag?
 155              if ($open = '/' != $text[1]) {
 156                  $tag = $matches[1][$i][0];
 157              } else {
 158                  $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
 159              }
 160  
 161              if (!$open && !$tag) {
 162                  // </>
 163                  $this->styleStack->pop();
 164              } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
 165                  $output .= $this->applyCurrentStyle($text);
 166              } elseif ($open) {
 167                  $this->styleStack->push($style);
 168              } else {
 169                  $this->styleStack->pop($style);
 170              }
 171          }
 172  
 173          $output .= $this->applyCurrentStyle(substr($message, $offset));
 174  
 175          if (false !== strpos($output, '<<')) {
 176              return strtr($output, array('\\<' => '<', '<<' => '\\'));
 177          }
 178  
 179          return str_replace('\\<', '<', $output);
 180      }
 181  
 182      /**
 183       * @return OutputFormatterStyleStack
 184       */
 185      public function getStyleStack()
 186      {
 187          return $this->styleStack;
 188      }
 189  
 190      /**
 191       * Tries to create new style instance from string.
 192       *
 193       * @param string $string
 194       *
 195       * @return OutputFormatterStyle|bool false if string is not format string
 196       */
 197      private function createStyleFromString($string)
 198      {
 199          if (isset($this->styles[$string])) {
 200              return $this->styles[$string];
 201          }
 202  
 203          if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
 204              return false;
 205          }
 206  
 207          $style = new OutputFormatterStyle();
 208          foreach ($matches as $match) {
 209              array_shift($match);
 210  
 211              if ('fg' == $match[0]) {
 212                  $style->setForeground($match[1]);
 213              } elseif ('bg' == $match[0]) {
 214                  $style->setBackground($match[1]);
 215              } else {
 216                  try {
 217                      $style->setOption($match[1]);
 218                  } catch (\InvalidArgumentException $e) {
 219                      return false;
 220                  }
 221              }
 222          }
 223  
 224          return $style;
 225      }
 226  
 227      /**
 228       * Applies current style from stack to text, if must be applied.
 229       *
 230       * @param string $text Input text
 231       *
 232       * @return string Styled text
 233       */
 234      private function applyCurrentStyle($text)
 235      {
 236          return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
 237      }
 238  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1