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

https://gitlab.com/pthapa81/MeroSaaman-1.0 · PHP · 241 lines · 112 code · 28 blank · 101 comment · 18 complexity · f885303f93c5515fa98510c7c00ec5d4 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. 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('/([^\\\\]?)</is', '$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. $offset = 0;
  131. $output = '';
  132. $tagRegex = '[a-z][a-z0-9_=;-]*';
  133. preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE);
  134. foreach ($matches[0] as $i => $match) {
  135. $pos = $match[1];
  136. $text = $match[0];
  137. if (0 != $pos && '\\' == $message[$pos - 1]) {
  138. continue;
  139. }
  140. // add the text up to the next tag
  141. $output .= $this->applyCurrentStyle(substr($message, $offset, $pos - $offset));
  142. $offset = $pos + strlen($text);
  143. // opening tag?
  144. if ($open = '/' != $text[1]) {
  145. $tag = $matches[1][$i][0];
  146. } else {
  147. $tag = isset($matches[3][$i][0]) ? $matches[3][$i][0] : '';
  148. }
  149. if (!$open && !$tag) {
  150. // </>
  151. $this->styleStack->pop();
  152. } elseif (false === $style = $this->createStyleFromString(strtolower($tag))) {
  153. $output .= $this->applyCurrentStyle($text);
  154. } elseif ($open) {
  155. $this->styleStack->push($style);
  156. } else {
  157. $this->styleStack->pop($style);
  158. }
  159. }
  160. $output .= $this->applyCurrentStyle(substr($message, $offset));
  161. return str_replace('\\<', '<', $output);
  162. }
  163. /**
  164. * @return OutputFormatterStyleStack
  165. */
  166. public function getStyleStack()
  167. {
  168. return $this->styleStack;
  169. }
  170. /**
  171. * Tries to create new style instance from string.
  172. *
  173. * @param string $string
  174. *
  175. * @return OutputFormatterStyle|bool false if string is not format string
  176. */
  177. private function createStyleFromString($string)
  178. {
  179. if (isset($this->styles[$string])) {
  180. return $this->styles[$string];
  181. }
  182. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
  183. return false;
  184. }
  185. $style = new OutputFormatterStyle();
  186. foreach ($matches as $match) {
  187. array_shift($match);
  188. if ('fg' == $match[0]) {
  189. $style->setForeground($match[1]);
  190. } elseif ('bg' == $match[0]) {
  191. $style->setBackground($match[1]);
  192. } else {
  193. try {
  194. $style->setOption($match[1]);
  195. } catch (\InvalidArgumentException $e) {
  196. return false;
  197. }
  198. }
  199. }
  200. return $style;
  201. }
  202. /**
  203. * Applies current style from stack to text, if must be applied.
  204. *
  205. * @param string $text Input text
  206. *
  207. * @return string Styled text
  208. */
  209. private function applyCurrentStyle($text)
  210. {
  211. return $this->isDecorated() && strlen($text) > 0 ? $this->styleStack->getCurrent()->apply($text) : $text;
  212. }
  213. }