PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/aleksbenmaza/PPE_NEW
PHP | 262 lines | 142 code | 29 blank | 91 comment | 12 complexity | 8a000286d017d238a4ebc5cee6269c8d 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. return self::escapeTrailingBackslash($text);
  33. }
  34. /**
  35. * Escapes trailing "\" in given text.
  36. *
  37. * @param string $text Text to escape
  38. *
  39. * @return string Escaped text
  40. *
  41. * @internal
  42. */
  43. public static function escapeTrailingBackslash($text)
  44. {
  45. if ('\\' === substr($text, -1)) {
  46. $len = strlen($text);
  47. $text = rtrim($text, '\\');
  48. $text .= str_repeat('<<', $len - strlen($text));
  49. }
  50. return $text;
  51. }
  52. /**
  53. * Initializes console output formatter.
  54. *
  55. * @param bool $decorated Whether this formatter should actually decorate strings
  56. * @param OutputFormatterStyleInterface[] $styles Array of "name => FormatterStyle" instances
  57. */
  58. public function __construct($decorated = false, array $styles = array())
  59. {
  60. $this->decorated = (bool) $decorated;
  61. $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  62. $this->setStyle('info', new OutputFormatterStyle('green'));
  63. $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  64. $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  65. foreach ($styles as $name => $style) {
  66. $this->setStyle($name, $style);
  67. }
  68. $this->styleStack = new OutputFormatterStyleStack();
  69. }
  70. /**
  71. * Sets the decorated flag.
  72. *
  73. * @param bool $decorated Whether to decorate the messages or not
  74. */
  75. public function setDecorated($decorated)
  76. {
  77. $this->decorated = (bool) $decorated;
  78. }
  79. /**
  80. * Gets the decorated flag.
  81. *
  82. * @return bool true if the output will decorate messages, false otherwise
  83. */
  84. public function isDecorated()
  85. {
  86. return $this->decorated;
  87. }
  88. /**
  89. * Sets a new style.
  90. *
  91. * @param string $name The style name
  92. * @param OutputFormatterStyleInterface $style The style instance
  93. */
  94. public function setStyle($name, OutputFormatterStyleInterface $style)
  95. {
  96. $this->styles[strtolower($name)] = $style;
  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. * Gets style options from style with specified name.
  111. *
  112. * @param string $name
  113. *
  114. * @return OutputFormatterStyleInterface
  115. *
  116. * @throws InvalidArgumentException When style isn't defined
  117. */
  118. public function getStyle($name)
  119. {
  120. if (!$this->hasStyle($name)) {
  121. throw new InvalidArgumentException(sprintf('Undefined style: %s', $name));
  122. }
  123. return $this->styles[strtolower($name)];
  124. }
  125. /**
  126. * Formats a message according to the given styles.
  127. *
  128. * @param string $message The message to style
  129. *
  130. * @return string The styled message
  131. */
  132. public function format($message)
  133. {
  134. $message = (string) $message;
  135. $offset = 0;
  136. $output = '';
  137. $tagRegex = '[a-z][a-z0-9,_=;-]*+';
  138. preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
  139. foreach ($matches[0] as $i => $match) {
  140. $pos = $match[1];
  141. $text = $match[0];
  142. if (0 != $pos && '\\' == $message[$pos - 1]) {
  143. continue;
  144. }
  145. // add the text up to the next tag
  146. $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
  147. $offset = $pos + strlen($text);
  148. // opening tag?
  149. if ($open = '/' != $text[1]) {
  150. $tag = $matches[1][$i][0];
  151. } else {
  152. $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
  153. }
  154. if (!$open && !$tag) {
  155. // </>
  156. $this->styleStack->pop();
  157. } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
  158. $output .= $this->applyCurrentStyle($text);
  159. } elseif ($open) {
  160. $this->styleStack->push($style);
  161. } else {
  162. $this->styleStack->pop($style);
  163. }
  164. }
  165. $output .= $this->applyCurrentStyle(substr($message, $offset));
  166. if (false !== strpos($output, '<<')) {
  167. return strtr($output, array('\\<' => '<', '<<' => '\\'));
  168. }
  169. return str_replace('\\<', '<', $output);
  170. }
  171. /**
  172. * @return OutputFormatterStyleStack
  173. */
  174. public function getStyleStack()
  175. {
  176. return $this->styleStack;
  177. }
  178. /**
  179. * Tries to create new style instance from string.
  180. *
  181. * @param string $string
  182. *
  183. * @return OutputFormatterStyle|false false if string is not format string
  184. */
  185. private function createStyleFromString($string)
  186. {
  187. if (isset($this->styles[$string])) {
  188. return $this->styles[$string];
  189. }
  190. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, PREG_SET_ORDER)) {
  191. return false;
  192. }
  193. $style = new OutputFormatterStyle();
  194. foreach ($matches as $match) {
  195. array_shift($match);
  196. if ('fg' == $match[0]) {
  197. $style->setForeground($match[1]);
  198. } elseif ('bg' == $match[0]) {
  199. $style->setBackground($match[1]);
  200. } elseif ('options' === $match[0]) {
  201. preg_match_all('([^,;]+)', $match[1], $options);
  202. $options = array_shift($options);
  203. foreach ($options as $option) {
  204. try {
  205. $style->setOption($option);
  206. } catch (\InvalidArgumentException $e) {
  207. @trigger_error(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED);
  208. return false;
  209. }
  210. }
  211. } else {
  212. return false;
  213. }
  214. }
  215. return $style;
  216. }
  217. /**
  218. * Applies current style from stack to text, if must be applied.
  219. *
  220. * @param string $text Input text
  221. *
  222. * @return string Styled text
  223. */
  224. private function applyCurrentStyle($text)
  225. {
  226. return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
  227. }
  228. }