OutputFormatterStyle.php 6.6KB

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