/vendor/symfony/console/Formatter/OutputFormatter.php

https://gitlab.com/ealexis.t/trends · PHP · 240 lines · 123 code · 32 blank · 85 comment · 20 complexity · 19fde6c12cff0529dff27b2c56496bd9 MD5 · raw file

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