Formatter.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\output;
  12. use think\console\output\formatter\Stack as StyleStack;
  13. use think\console\output\formatter\Style;
  14. class Formatter
  15. {
  16. private $decorated = false;
  17. private $styles = [];
  18. private $styleStack;
  19. /**
  20. * 转义
  21. * @param string $text
  22. * @return string
  23. */
  24. public static function escape($text)
  25. {
  26. return preg_replace('/([^\\\\]?)</is', '$1\\<', $text);
  27. }
  28. /**
  29. * 初始化命令行输出格式
  30. */
  31. public function __construct()
  32. {
  33. $this->setStyle('error', new Style('white', 'red'));
  34. $this->setStyle('info', new Style('green'));
  35. $this->setStyle('comment', new Style('yellow'));
  36. $this->setStyle('question', new Style('black', 'cyan'));
  37. $this->setStyle('highlight', new Style('red'));
  38. $this->setStyle('warning', new Style('black', 'yellow'));
  39. $this->styleStack = new StyleStack();
  40. }
  41. /**
  42. * 设置外观标识
  43. * @param bool $decorated 是否美化文字
  44. */
  45. public function setDecorated($decorated)
  46. {
  47. $this->decorated = (bool) $decorated;
  48. }
  49. /**
  50. * 获取外观标识
  51. * @return bool
  52. */
  53. public function isDecorated()
  54. {
  55. return $this->decorated;
  56. }
  57. /**
  58. * 添加一个新样式
  59. * @param string $name 样式名
  60. * @param Style $style 样式实例
  61. */
  62. public function setStyle($name, Style $style)
  63. {
  64. $this->styles[strtolower($name)] = $style;
  65. }
  66. /**
  67. * 是否有这个样式
  68. * @param string $name
  69. * @return bool
  70. */
  71. public function hasStyle($name)
  72. {
  73. return isset($this->styles[strtolower($name)]);
  74. }
  75. /**
  76. * 获取样式
  77. * @param string $name
  78. * @return Style
  79. * @throws \InvalidArgumentException
  80. */
  81. public function getStyle($name)
  82. {
  83. if (!$this->hasStyle($name)) {
  84. throw new \InvalidArgumentException(sprintf('Undefined style: %s', $name));
  85. }
  86. return $this->styles[strtolower($name)];
  87. }
  88. /**
  89. * 使用所给的样式格式化文字
  90. * @param string $message 文字
  91. * @return string
  92. */
  93. public function format($message)
  94. {
  95. $offset = 0;
  96. $output = '';
  97. $tagRegex = '[a-z][a-z0-9_=;-]*';
  98. preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE);
  99. foreach ($matches[0] as $i => $match) {
  100. $pos = $match[1];
  101. $text = $match[0];
  102. if (0 != $pos && '\\' == $message[$pos - 1]) {
  103. continue;
  104. }
  105. $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
  106. $offset = $pos + strlen($text);
  107. if ($open = '/' != $text[1]) {
  108. $tag = $matches[1][$i][0];
  109. } else {
  110. $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
  111. }
  112. if (!$open && !$tag) {
  113. // </>
  114. $this->styleStack->pop();
  115. } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
  116. $output .= $this->applyCurrentStyle($text);
  117. } elseif ($open) {
  118. $this->styleStack->push($style);
  119. } else {
  120. $this->styleStack->pop($style);
  121. }
  122. }
  123. $output .= $this->applyCurrentStyle(substr($message, $offset));
  124. return str_replace('\\<', '<', $output);
  125. }
  126. /**
  127. * @return StyleStack
  128. */
  129. public function getStyleStack()
  130. {
  131. return $this->styleStack;
  132. }
  133. /**
  134. * 根据字符串创建新的样式实例
  135. * @param string $string
  136. * @return Style|bool
  137. */
  138. private function createStyleFromString($string)
  139. {
  140. if (isset($this->styles[$string])) {
  141. return $this->styles[$string];
  142. }
  143. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
  144. return false;
  145. }
  146. $style = new Style();
  147. foreach ($matches as $match) {
  148. array_shift($match);
  149. if ('fg' == $match[0]) {
  150. $style->setForeground($match[1]);
  151. } elseif ('bg' == $match[0]) {
  152. $style->setBackground($match[1]);
  153. } else {
  154. try {
  155. $style->setOption($match[1]);
  156. } catch (\InvalidArgumentException $e) {
  157. return false;
  158. }
  159. }
  160. }
  161. return $style;
  162. }
  163. /**
  164. * 从堆栈应用样式到文字
  165. * @param string $text 文字
  166. * @return string
  167. */
  168. private function applyCurrentStyle($text)
  169. {
  170. return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
  171. }
  172. }