OutputFormatterStyle.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 style class for defining styles.
  13. *
  14. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  15. *
  16. * @api
  17. */
  18. class OutputFormatterStyle implements OutputFormatterStyleInterface
  19. {
  20. private static $availableForegroundColors = array(
  21. 'black' => array('set' => 30, 'unset' => 39),
  22. 'red' => array('set' => 31, 'unset' => 39),
  23. 'green' => array('set' => 32, 'unset' => 39),
  24. 'yellow' => array('set' => 33, 'unset' => 39),
  25. 'blue' => array('set' => 34, 'unset' => 39),
  26. 'magenta' => array('set' => 35, 'unset' => 39),
  27. 'cyan' => array('set' => 36, 'unset' => 39),
  28. 'white' => array('set' => 37, 'unset' => 39),
  29. 'default' => array('set' => 39, 'unset' => 39),
  30. );
  31. private static $availableBackgroundColors = array(
  32. 'black' => array('set' => 40, 'unset' => 49),
  33. 'red' => array('set' => 41, 'unset' => 49),
  34. 'green' => array('set' => 42, 'unset' => 49),
  35. 'yellow' => array('set' => 43, 'unset' => 49),
  36. 'blue' => array('set' => 44, 'unset' => 49),
  37. 'magenta' => array('set' => 45, 'unset' => 49),
  38. 'cyan' => array('set' => 46, 'unset' => 49),
  39. 'white' => array('set' => 47, 'unset' => 49),
  40. 'default' => array('set' => 49, 'unset' => 49),
  41. );
  42. private static $availableOptions = array(
  43. 'bold' => array('set' => 1, 'unset' => 22),
  44. 'underscore' => array('set' => 4, 'unset' => 24),
  45. 'blink' => array('set' => 5, 'unset' => 25),
  46. 'reverse' => array('set' => 7, 'unset' => 27),
  47. 'conceal' => array('set' => 8, 'unset' => 28),
  48. );
  49. private $foreground;
  50. private $background;
  51. private $options = array();
  52. /**
  53. * Initializes output formatter style.
  54. *
  55. * @param string|null $foreground The style foreground color name
  56. * @param string|null $background The style background color name
  57. * @param array $options The style options
  58. *
  59. * @api
  60. */
  61. public function __construct($foreground = null, $background = null, array $options = array())
  62. {
  63. if (null !== $foreground) {
  64. $this->setForeground($foreground);
  65. }
  66. if (null !== $background) {
  67. $this->setBackground($background);
  68. }
  69. if (count($options)) {
  70. $this->setOptions($options);
  71. }
  72. }
  73. /**
  74. * Sets style foreground color.
  75. *
  76. * @param string|null $color The color name
  77. *
  78. * @throws \InvalidArgumentException When the color name isn't defined
  79. *
  80. * @api
  81. */
  82. public function setForeground($color = null)
  83. {
  84. if (null === $color) {
  85. $this->foreground = null;
  86. return;
  87. }
  88. if (!isset(static::$availableForegroundColors[$color])) {
  89. throw new \InvalidArgumentException(sprintf(
  90. 'Invalid foreground color specified: "%s". Expected one of (%s)',
  91. $color,
  92. implode(', ', array_keys(static::$availableForegroundColors))
  93. ));
  94. }
  95. $this->foreground = static::$availableForegroundColors[$color];
  96. }
  97. /**
  98. * Sets style background color.
  99. *
  100. * @param string|null $color The color name
  101. *
  102. * @throws \InvalidArgumentException When the color name isn't defined
  103. *
  104. * @api
  105. */
  106. public function setBackground($color = null)
  107. {
  108. if (null === $color) {
  109. $this->background = null;
  110. return;
  111. }
  112. if (!isset(static::$availableBackgroundColors[$color])) {
  113. throw new \InvalidArgumentException(sprintf(
  114. 'Invalid background color specified: "%s". Expected one of (%s)',
  115. $color,
  116. implode(', ', array_keys(static::$availableBackgroundColors))
  117. ));
  118. }
  119. $this->background = static::$availableBackgroundColors[$color];
  120. }
  121. /**
  122. * Sets some specific style option.
  123. *
  124. * @param string $option The option name
  125. *
  126. * @throws \InvalidArgumentException When the option name isn't defined
  127. *
  128. * @api
  129. */
  130. public function setOption($option)
  131. {
  132. if (!isset(static::$availableOptions[$option])) {
  133. throw new \InvalidArgumentException(sprintf(
  134. 'Invalid option specified: "%s". Expected one of (%s)',
  135. $option,
  136. implode(', ', array_keys(static::$availableOptions))
  137. ));
  138. }
  139. if (!in_array(static::$availableOptions[$option], $this->options)) {
  140. $this->options[] = static::$availableOptions[$option];
  141. }
  142. }
  143. /**
  144. * Unsets some specific style option.
  145. *
  146. * @param string $option The option name
  147. *
  148. * @throws \InvalidArgumentException When the option name isn't defined
  149. */
  150. public function unsetOption($option)
  151. {
  152. if (!isset(static::$availableOptions[$option])) {
  153. throw new \InvalidArgumentException(sprintf(
  154. 'Invalid option specified: "%s". Expected one of (%s)',
  155. $option,
  156. implode(', ', array_keys(static::$availableOptions))
  157. ));
  158. }
  159. $pos = array_search(static::$availableOptions[$option], $this->options);
  160. if (false !== $pos) {
  161. unset($this->options[$pos]);
  162. }
  163. }
  164. /**
  165. * Sets multiple style options at once.
  166. *
  167. * @param array $options
  168. */
  169. public function setOptions(array $options)
  170. {
  171. $this->options = array();
  172. foreach ($options as $option) {
  173. $this->setOption($option);
  174. }
  175. }
  176. /**
  177. * Applies the style to a given text.
  178. *
  179. * @param string $text The text to style
  180. *
  181. * @return string
  182. */
  183. public function apply($text)
  184. {
  185. $setCodes = array();
  186. $unsetCodes = array();
  187. if (null !== $this->foreground) {
  188. $setCodes[] = $this->foreground['set'];
  189. $unsetCodes[] = $this->foreground['unset'];
  190. }
  191. if (null !== $this->background) {
  192. $setCodes[] = $this->background['set'];
  193. $unsetCodes[] = $this->background['unset'];
  194. }
  195. if (count($this->options)) {
  196. foreach ($this->options as $option) {
  197. $setCodes[] = $option['set'];
  198. $unsetCodes[] = $option['unset'];
  199. }
  200. }
  201. if (0 === count($setCodes)) {
  202. return $text;
  203. }
  204. return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
  205. }
  206. }