[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/Formatter/ -> OutputFormatter.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   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  use Symfony\Component\Console\Exception\InvalidArgumentException;
  15  
  16  /**
  17   * Formatter class for console output.
  18   *
  19   * @author Konstantin Kudryashov <[email protected]>
  20   * @author Roland Franssen <[email protected]>
  21   */
  22  class OutputFormatter implements WrappableOutputFormatterInterface
  23  {
  24      private $decorated;
  25      private $styles = [];
  26      private $styleStack;
  27  
  28      public function __clone()
  29      {
  30          $this->styleStack = clone $this->styleStack;
  31          foreach ($this->styles as $key => $value) {
  32              $this->styles[$key] = clone $value;
  33          }
  34      }
  35  
  36      /**
  37       * Escapes "<" and ">" special chars in given text.
  38       *
  39       * @return string
  40       */
  41      public static function escape(string $text)
  42      {
  43          $text = preg_replace('/([^\\\\]|^)([<>])/', '$1\\\\$2', $text);
  44  
  45          return self::escapeTrailingBackslash($text);
  46      }
  47  
  48      /**
  49       * Escapes trailing "\" in given text.
  50       *
  51       * @internal
  52       */
  53      public static function escapeTrailingBackslash(string $text): string
  54      {
  55          if (str_ends_with($text, '\\')) {
  56              $len = \strlen($text);
  57              $text = rtrim($text, '\\');
  58              $text = str_replace("\0", '', $text);
  59              $text .= str_repeat("\0", $len - \strlen($text));
  60          }
  61  
  62          return $text;
  63      }
  64  
  65      /**
  66       * Initializes console output formatter.
  67       *
  68       * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
  69       */
  70      public function __construct(bool $decorated = false, array $styles = [])
  71      {
  72          $this->decorated = $decorated;
  73  
  74          $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  75          $this->setStyle('info', new OutputFormatterStyle('green'));
  76          $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  77          $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  78  
  79          foreach ($styles as $name => $style) {
  80              $this->setStyle($name, $style);
  81          }
  82  
  83          $this->styleStack = new OutputFormatterStyleStack();
  84      }
  85  
  86      /**
  87       * {@inheritdoc}
  88       */
  89      public function setDecorated(bool $decorated)
  90      {
  91          $this->decorated = $decorated;
  92      }
  93  
  94      /**
  95       * {@inheritdoc}
  96       */
  97      public function isDecorated()
  98      {
  99          return $this->decorated;
 100      }
 101  
 102      /**
 103       * {@inheritdoc}
 104       */
 105      public function setStyle(string $name, OutputFormatterStyleInterface $style)
 106      {
 107          $this->styles[strtolower($name)] = $style;
 108      }
 109  
 110      /**
 111       * {@inheritdoc}
 112       */
 113      public function hasStyle(string $name)
 114      {
 115          return isset($this->styles[strtolower($name)]);
 116      }
 117  
 118      /**
 119       * {@inheritdoc}
 120       */
 121      public function getStyle(string $name)
 122      {
 123          if (!$this->hasStyle($name)) {
 124              throw new InvalidArgumentException(sprintf('Undefined style: "%s".', $name));
 125          }
 126  
 127          return $this->styles[strtolower($name)];
 128      }
 129  
 130      /**
 131       * {@inheritdoc}
 132       */
 133      public function format(?string $message)
 134      {
 135          return $this->formatAndWrap($message, 0);
 136      }
 137  
 138      /**
 139       * {@inheritdoc}
 140       */
 141      public function formatAndWrap(?string $message, int $width)
 142      {
 143          if (null === $message) {
 144              return '';
 145          }
 146  
 147          $offset = 0;
 148          $output = '';
 149          $openTagRegex = '[a-z](?:[^\\\\<>]*+ | \\\\.)*';
 150          $closeTagRegex = '[a-z][^<>]*+';
 151          $currentLineLength = 0;
 152          preg_match_all("#<(($openTagRegex) | /($closeTagRegex)?)>#ix", $message, $matches, \PREG_OFFSET_CAPTURE);
 153          foreach ($matches[0] as $i => $match) {
 154              $pos = $match[1];
 155              $text = $match[0];
 156  
 157              if (0 != $pos && '\\' == $message[$pos - 1]) {
 158                  continue;
 159              }
 160  
 161              // add the text up to the next tag
 162              $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset), $output, $width, $currentLineLength);
 163              $offset = $pos + \strlen($text);
 164  
 165              // opening tag?
 166              if ($open = '/' != $text[1]) {
 167                  $tag = $matches[1][$i][0];
 168              } else {
 169                  $tag = $matches[3][$i][0] ?? '';
 170              }
 171  
 172              if (!$open && !$tag) {
 173                  // </>
 174                  $this->styleStack->pop();
 175              } elseif (null === $style = $this->createStyleFromString($tag)) {
 176                  $output .= $this->applyCurrentStyle($text, $output, $width, $currentLineLength);
 177              } elseif ($open) {
 178                  $this->styleStack->push($style);
 179              } else {
 180                  $this->styleStack->pop($style);
 181              }
 182          }
 183  
 184          $output .= $this->applyCurrentStyle(substr($message, $offset), $output, $width, $currentLineLength);
 185  
 186          return strtr($output, ["\0" => '\\', '\\<' => '<', '\\>' => '>']);
 187      }
 188  
 189      /**
 190       * @return OutputFormatterStyleStack
 191       */
 192      public function getStyleStack()
 193      {
 194          return $this->styleStack;
 195      }
 196  
 197      /**
 198       * Tries to create new style instance from string.
 199       */
 200      private function createStyleFromString(string $string): ?OutputFormatterStyleInterface
 201      {
 202          if (isset($this->styles[$string])) {
 203              return $this->styles[$string];
 204          }
 205  
 206          if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, \PREG_SET_ORDER)) {
 207              return null;
 208          }
 209  
 210          $style = new OutputFormatterStyle();
 211          foreach ($matches as $match) {
 212              array_shift($match);
 213              $match[0] = strtolower($match[0]);
 214  
 215              if ('fg' == $match[0]) {
 216                  $style->setForeground(strtolower($match[1]));
 217              } elseif ('bg' == $match[0]) {
 218                  $style->setBackground(strtolower($match[1]));
 219              } elseif ('href' === $match[0]) {
 220                  $url = preg_replace('{\\\\([<>])}', '$1', $match[1]);
 221                  $style->setHref($url);
 222              } elseif ('options' === $match[0]) {
 223                  preg_match_all('([^,;]+)', strtolower($match[1]), $options);
 224                  $options = array_shift($options);
 225                  foreach ($options as $option) {
 226                      $style->setOption($option);
 227                  }
 228              } else {
 229                  return null;
 230              }
 231          }
 232  
 233          return $style;
 234      }
 235  
 236      /**
 237       * Applies current style from stack to text, if must be applied.
 238       */
 239      private function applyCurrentStyle(string $text, string $current, int $width, int &$currentLineLength): string
 240      {
 241          if ('' === $text) {
 242              return '';
 243          }
 244  
 245          if (!$width) {
 246              return $this->isDecorated() ? $this->styleStack->getCurrent()->apply($text) : $text;
 247          }
 248  
 249          if (!$currentLineLength && '' !== $current) {
 250              $text = ltrim($text);
 251          }
 252  
 253          if ($currentLineLength) {
 254              $prefix = substr($text, 0, $i = $width - $currentLineLength)."\n";
 255              $text = substr($text, $i);
 256          } else {
 257              $prefix = '';
 258          }
 259  
 260          preg_match('~(\\n)$~', $text, $matches);
 261          $text = $prefix.preg_replace('~([^\\n]{'.$width.'})\\ *~', "\$1\n", $text);
 262          $text = rtrim($text, "\n").($matches[1] ?? '');
 263  
 264          if (!$currentLineLength && '' !== $current && "\n" !== substr($current, -1)) {
 265              $text = "\n".$text;
 266          }
 267  
 268          $lines = explode("\n", $text);
 269  
 270          foreach ($lines as $line) {
 271              $currentLineLength += \strlen($line);
 272              if ($width <= $currentLineLength) {
 273                  $currentLineLength = 0;
 274              }
 275          }
 276  
 277          if ($this->isDecorated()) {
 278              foreach ($lines as $i => $line) {
 279                  $lines[$i] = $this->styleStack->getCurrent()->apply($line);
 280              }
 281          }
 282  
 283          return implode("\n", $lines);
 284      }
 285  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer