OutputFormatter.php 6.3 KB

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