/vendor/symfony/console/Symfony/Component/Console/Formatter/OutputFormatter.php

https://bitbucket.org/iiic/iszp · PHP · 248 lines · 106 code · 31 blank · 111 comment · 15 complexity · a3fa3dcddb67a8d2f69a1cf23742ccd3 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. /**
  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. /**
  21. * The pattern to phrase the format.
  22. */
  23. const FORMAT_PATTERN = '#(\\\\?)<(/?)([a-z][a-z0-9_=;-]+)?>((?: [^<\\\\]+ | (?!<(?:/?[a-z]|/>)). | .(?<=\\\\<) )*)#isx';
  24. private $decorated;
  25. private $styles = array();
  26. private $styleStack;
  27. /**
  28. * Escapes "<" special char in given text.
  29. *
  30. * @param string $text Text to escape
  31. *
  32. * @return string Escaped text
  33. */
  34. public static function escape($text)
  35. {
  36. return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
  37. }
  38. /**
  39. * Initializes console output formatter.
  40. *
  41. * @param Boolean $decorated Whether this formatter should actually decorate strings
  42. * @param array $styles Array of "name => FormatterStyle" instances
  43. *
  44. * @api
  45. */
  46. public function __construct($decorated = null, array $styles = array())
  47. {
  48. $this->decorated = (Boolean) $decorated;
  49. $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  50. $this->setStyle('info', new OutputFormatterStyle('green'));
  51. $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  52. $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  53. foreach ($styles as $name => $style) {
  54. $this->setStyle($name, $style);
  55. }
  56. $this->styleStack = new OutputFormatterStyleStack();
  57. }
  58. /**
  59. * Sets the decorated flag.
  60. *
  61. * @param Boolean $decorated Whether to decorate the messages or not
  62. *
  63. * @api
  64. */
  65. public function setDecorated($decorated)
  66. {
  67. $this->decorated = (Boolean) $decorated;
  68. }
  69. /**
  70. * Gets the decorated flag.
  71. *
  72. * @return Boolean true if the output will decorate messages, false otherwise
  73. *
  74. * @api
  75. */
  76. public function isDecorated()
  77. {
  78. return $this->decorated;
  79. }
  80. /**
  81. * Sets a new style.
  82. *
  83. * @param string $name The style name
  84. * @param OutputFormatterStyleInterface $style The style instance
  85. *
  86. * @api
  87. */
  88. public function setStyle($name, OutputFormatterStyleInterface $style)
  89. {
  90. $this->styles[strtolower($name)] = $style;
  91. }
  92. /**
  93. * Checks if output formatter has style with specified name.
  94. *
  95. * @param string $name
  96. *
  97. * @return Boolean
  98. *
  99. * @api
  100. */
  101. public function hasStyle($name)
  102. {
  103. return isset($this->styles[strtolower($name)]);
  104. }
  105. /**
  106. * Gets style options from style with specified name.
  107. *
  108. * @param string $name
  109. *
  110. * @return OutputFormatterStyleInterface
  111. *
  112. * @throws \InvalidArgumentException When style isn't defined
  113. *
  114. * @api
  115. */
  116. public function getStyle($name)
  117. {
  118. if (!$this->hasStyle($name)) {
  119. throw new \InvalidArgumentException('Undefined style: '.$name);
  120. }
  121. return $this->styles[strtolower($name)];
  122. }
  123. /**
  124. * Formats a message according to the given styles.
  125. *
  126. * @param string $message The message to style
  127. *
  128. * @return string The styled message
  129. *
  130. * @api
  131. */
  132. public function format($message)
  133. {
  134. $message = preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
  135. return str_replace('\\<', '<', $message);
  136. }
  137. /**
  138. * @return OutputFormatterStyleStack
  139. */
  140. public function getStyleStack()
  141. {
  142. return $this->styleStack;
  143. }
  144. /**
  145. * Replaces style of the output.
  146. *
  147. * @param array $match
  148. *
  149. * @return string The replaced style
  150. */
  151. private function replaceStyle($match)
  152. {
  153. // we got "\<" escaped char
  154. if ('\\' === $match[1]) {
  155. return $this->applyCurrentStyle($match[0]);
  156. }
  157. if ('' === $match[3]) {
  158. if ('/' === $match[2]) {
  159. // we got "</>" tag
  160. $this->styleStack->pop();
  161. return $this->applyCurrentStyle($match[4]);
  162. }
  163. // we got "<>" tag
  164. return '<>'.$this->applyCurrentStyle($match[4]);
  165. }
  166. if (isset($this->styles[strtolower($match[3])])) {
  167. $style = $this->styles[strtolower($match[3])];
  168. } else {
  169. $style = $this->createStyleFromString($match[3]);
  170. if (false === $style) {
  171. return $this->applyCurrentStyle($match[0]);
  172. }
  173. }
  174. if ('/' === $match[2]) {
  175. $this->styleStack->pop($style);
  176. } else {
  177. $this->styleStack->push($style);
  178. }
  179. return $this->applyCurrentStyle($match[4]);
  180. }
  181. /**
  182. * Tries to create new style instance from string.
  183. *
  184. * @param string $string
  185. *
  186. * @return OutputFormatterStyle|Boolean false if string is not format string
  187. */
  188. private function createStyleFromString($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. $style->setOption($match[1]);
  202. }
  203. }
  204. return $style;
  205. }
  206. /**
  207. * Applies current style from stack to text, if must be applied.
  208. *
  209. * @param string $text Input text
  210. *
  211. * @return string string Styled text
  212. */
  213. private function applyCurrentStyle($text)
  214. {
  215. return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
  216. }
  217. }