OutputFormatterStyle.php 5.7 KB

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