Drupal investigation

OutputFormatter.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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('/([^=]+)=([^;]+)(;|$)/', strtolower($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. } else {
  201. try {
  202. $style->setOption($match[1]);
  203. } catch (\InvalidArgumentException $e) {
  204. return false;
  205. }
  206. }
  207. }
  208. return $style;
  209. }
  210. /**
  211. * Applies current style from stack to text, if must be applied.
  212. *
  213. * @param string $text Input text
  214. *
  215. * @return string Styled text
  216. */
  217. private function applyCurrentStyle($text)
  218. {
  219. return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
  220. }
  221. }