/RuaApp/libraries/Doctrine/Symfony/Component/Console/Formatter/OutputFormatter.php

https://github.com/webvpro/RUA-CORE · PHP · 243 lines · 109 code · 29 blank · 105 comment · 16 complexity · fbfe3f9083974cceda783d1d70818dac 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. /**
  23. * Initializes console output formatter.
  24. *
  25. * @param Boolean $decorated Whether this formatter should actually decorate strings
  26. * @param array $styles Array of "name => FormatterStyle" instance
  27. *
  28. * @api
  29. */
  30. public function __construct($decorated = null, array $styles = array())
  31. {
  32. $this->decorated = (Boolean) $decorated;
  33. $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  34. $this->setStyle('info', new OutputFormatterStyle('green'));
  35. $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  36. $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  37. foreach ($styles as $name => $style) {
  38. $this->setStyle($name, $style);
  39. }
  40. }
  41. /**
  42. * Sets the decorated flag.
  43. *
  44. * @param Boolean $decorated Whether to decorated the messages or not
  45. *
  46. * @api
  47. */
  48. public function setDecorated($decorated)
  49. {
  50. $this->decorated = (Boolean) $decorated;
  51. }
  52. /**
  53. * Gets the decorated flag.
  54. *
  55. * @return Boolean true if the output will decorate messages, false otherwise
  56. *
  57. * @api
  58. */
  59. public function isDecorated()
  60. {
  61. return $this->decorated;
  62. }
  63. /**
  64. * Sets a new style.
  65. *
  66. * @param string $name The style name
  67. * @param OutputFormatterStyleInterface $style The style instance
  68. *
  69. * @api
  70. */
  71. public function setStyle($name, OutputFormatterStyleInterface $style)
  72. {
  73. $this->styles[strtolower($name)] = $style;
  74. }
  75. /**
  76. * Checks if output formatter has style with specified name.
  77. *
  78. * @param string $name
  79. *
  80. * @return Boolean
  81. *
  82. * @api
  83. */
  84. public function hasStyle($name)
  85. {
  86. return isset($this->styles[strtolower($name)]);
  87. }
  88. /**
  89. * Gets style options from style with specified name.
  90. *
  91. * @param string $name
  92. *
  93. * @return OutputFormatterStyleInterface
  94. *
  95. * @api
  96. */
  97. public function getStyle($name)
  98. {
  99. if (!$this->hasStyle($name)) {
  100. throw new \InvalidArgumentException('Undefined style: ' . $name);
  101. }
  102. return $this->styles[strtolower($name)];
  103. }
  104. /**
  105. * Formats a message according to the given styles.
  106. *
  107. * @param string $message The message to style
  108. *
  109. * @return string The styled message
  110. *
  111. * @api
  112. */
  113. public function format($message)
  114. {
  115. $message = preg_replace_callback(
  116. $this->getBeginStyleRegex(), array($this, 'replaceBeginStyle'), $message
  117. );
  118. return preg_replace_callback(
  119. $this->getEndStyleRegex(), array($this, 'replaceEndStyle'), $message
  120. );
  121. }
  122. /**
  123. * Gets regex for a style start.
  124. *
  125. * @return string
  126. */
  127. protected function getBeginStyleRegex()
  128. {
  129. return '#<([a-z][a-z0-9\-_=;]+)>#i';
  130. }
  131. /**
  132. * Gets regex for a style end.
  133. *
  134. * @return string
  135. */
  136. protected function getEndStyleRegex()
  137. {
  138. return '#</([a-z][a-z0-9\-_]*)?>#i';
  139. }
  140. /**
  141. * Replaces the starting style of the output.
  142. *
  143. * @param array $match
  144. *
  145. * @return string The replaced style
  146. *
  147. * @throws \InvalidArgumentException When style is unknown
  148. */
  149. private function replaceBeginStyle($match)
  150. {
  151. if (!$this->isDecorated()) {
  152. return '';
  153. }
  154. if (isset($this->styles[strtolower($match[1])])) {
  155. $style = $this->styles[strtolower($match[1])];
  156. } else {
  157. $style = $this->createStyleFromString($match[1]);
  158. if (false === $style) {
  159. return $match[0];
  160. }
  161. }
  162. return $style->getBeginStyle();
  163. }
  164. /**
  165. * Replaces the end style.
  166. *
  167. * @param string $match The text to match
  168. *
  169. * @return string The end style
  170. */
  171. private function replaceEndStyle($match)
  172. {
  173. if (!$this->isDecorated()) {
  174. return '';
  175. }
  176. if (!isset($match[1])) {
  177. $match[1] = '';
  178. }
  179. if ('' == $match[1]) {
  180. $style = new OutputFormatterStyle();
  181. } else {
  182. if (!isset($this->styles[strtolower($match[1])])) {
  183. return $match[0];
  184. }
  185. $style = $this->styles[strtolower($match[1])];
  186. }
  187. return $style->getEndStyle();
  188. }
  189. /**
  190. * Tryes to create new style instance from string.
  191. *
  192. * @param string $string
  193. *
  194. * @return Symfony\Component\Console\Format\FormatterStyle|Boolean false if string is not format string
  195. */
  196. private function createStyleFromString($string)
  197. {
  198. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
  199. return false;
  200. }
  201. $style = new OutputFormatterStyle();
  202. foreach ($matches as $match) {
  203. array_shift($match);
  204. if ('fg' == $match[0]) {
  205. $style->setForeground($match[1]);
  206. } elseif ('bg' == $match[0]) {
  207. $style->setBackground($match[1]);
  208. } else {
  209. $style->setOption($match[1]);
  210. }
  211. }
  212. return $style;
  213. }
  214. }