OutputFormatter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_=;-]+)>(.*?)</\\1?>#is';
  24. private $decorated;
  25. private $styles = array();
  26. /**
  27. * Initializes console output formatter.
  28. *
  29. * @param Boolean $decorated Whether this formatter should actually decorate strings
  30. * @param array $styles Array of "name => FormatterStyle" instances
  31. *
  32. * @api
  33. */
  34. public function __construct($decorated = null, array $styles = array())
  35. {
  36. $this->decorated = (Boolean) $decorated;
  37. $this->setStyle('error', new OutputFormatterStyle('white', 'red'));
  38. $this->setStyle('info', new OutputFormatterStyle('green'));
  39. $this->setStyle('comment', new OutputFormatterStyle('yellow'));
  40. $this->setStyle('question', new OutputFormatterStyle('black', 'cyan'));
  41. foreach ($styles as $name => $style) {
  42. $this->setStyle($name, $style);
  43. }
  44. }
  45. /**
  46. * Sets the decorated flag.
  47. *
  48. * @param Boolean $decorated Whether to decorate the messages or not
  49. *
  50. * @api
  51. */
  52. public function setDecorated($decorated)
  53. {
  54. $this->decorated = (Boolean) $decorated;
  55. }
  56. /**
  57. * Gets the decorated flag.
  58. *
  59. * @return Boolean true if the output will decorate messages, false otherwise
  60. *
  61. * @api
  62. */
  63. public function isDecorated()
  64. {
  65. return $this->decorated;
  66. }
  67. /**
  68. * Sets a new style.
  69. *
  70. * @param string $name The style name
  71. * @param OutputFormatterStyleInterface $style The style instance
  72. *
  73. * @api
  74. */
  75. public function setStyle($name, OutputFormatterStyleInterface $style)
  76. {
  77. $this->styles[strtolower($name)] = $style;
  78. }
  79. /**
  80. * Checks if output formatter has style with specified name.
  81. *
  82. * @param string $name
  83. *
  84. * @return Boolean
  85. *
  86. * @api
  87. */
  88. public function hasStyle($name)
  89. {
  90. return isset($this->styles[strtolower($name)]);
  91. }
  92. /**
  93. * Gets style options from style with specified name.
  94. *
  95. * @param string $name
  96. *
  97. * @return OutputFormatterStyleInterface
  98. *
  99. * @throws \InvalidArgumentException When style isn't defined
  100. *
  101. * @api
  102. */
  103. public function getStyle($name)
  104. {
  105. if (!$this->hasStyle($name)) {
  106. throw new \InvalidArgumentException('Undefined style: '.$name);
  107. }
  108. return $this->styles[strtolower($name)];
  109. }
  110. /**
  111. * Formats a message according to the given styles.
  112. *
  113. * @param string $message The message to style
  114. *
  115. * @return string The styled message
  116. *
  117. * @api
  118. */
  119. public function format($message)
  120. {
  121. return preg_replace_callback(self::FORMAT_PATTERN, array($this, 'replaceStyle'), $message);
  122. }
  123. /**
  124. * Replaces style of the output.
  125. *
  126. * @param array $match
  127. *
  128. * @return string The replaced style
  129. */
  130. private function replaceStyle($match)
  131. {
  132. if (!$this->isDecorated()) {
  133. return $match[2];
  134. }
  135. if (isset($this->styles[strtolower($match[1])])) {
  136. $style = $this->styles[strtolower($match[1])];
  137. } else {
  138. $style = $this->createStyleFromString($match[1]);
  139. if (false === $style) {
  140. return $match[0];
  141. }
  142. }
  143. return $style->apply($this->format($match[2]));
  144. }
  145. /**
  146. * Tries to create new style instance from string.
  147. *
  148. * @param string $string
  149. *
  150. * @return Symfony\Component\Console\Format\FormatterStyle|Boolean false if string is not format string
  151. */
  152. private function createStyleFromString($string)
  153. {
  154. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
  155. return false;
  156. }
  157. $style = new OutputFormatterStyle();
  158. foreach ($matches as $match) {
  159. array_shift($match);
  160. if ('fg' == $match[0]) {
  161. $style->setForeground($match[1]);
  162. } elseif ('bg' == $match[0]) {
  163. $style->setBackground($match[1]);
  164. } else {
  165. $style->setOption($match[1]);
  166. }
  167. }
  168. return $style;
  169. }
  170. }