/libraries/Symfony/Component/Console/Formatter/OutputFormatterStyle.php

https://github.com/kiranatama/sagalaya · PHP · 217 lines · 129 code · 23 blank · 65 comment · 14 complexity · 2d8780ba5045b3a0035f294ca61f2cbe 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 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. * @api
  77. */
  78. public function setForeground($color = null)
  79. {
  80. if (null === $color) {
  81. $this->foreground = null;
  82. return;
  83. }
  84. if (!isset(static::$availableForegroundColors[$color])) {
  85. throw new \InvalidArgumentException(sprintf(
  86. 'Invalid foreground color specified: "%s". Expected one of (%s)',
  87. $color,
  88. implode(', ', array_keys(static::$availableForegroundColors))
  89. ));
  90. }
  91. $this->foreground = static::$availableForegroundColors[$color];
  92. }
  93. /**
  94. * Sets style background color.
  95. *
  96. * @param string $color color name
  97. *
  98. * @api
  99. */
  100. public function setBackground($color = null)
  101. {
  102. if (null === $color) {
  103. $this->background = null;
  104. return;
  105. }
  106. if (!isset(static::$availableBackgroundColors[$color])) {
  107. throw new \InvalidArgumentException(sprintf(
  108. 'Invalid background color specified: "%s". Expected one of (%s)',
  109. $color,
  110. implode(', ', array_keys(static::$availableBackgroundColors))
  111. ));
  112. }
  113. $this->background = static::$availableBackgroundColors[$color];
  114. }
  115. /**
  116. * Sets some specific style option.
  117. *
  118. * @param string $option option name
  119. *
  120. * @api
  121. */
  122. public function setOption($option)
  123. {
  124. if (!isset(static::$availableOptions[$option])) {
  125. throw new \InvalidArgumentException(sprintf(
  126. 'Invalid option specified: "%s". Expected one of (%s)',
  127. $option,
  128. implode(', ', array_keys(static::$availableOptions))
  129. ));
  130. }
  131. if (false === array_search(static::$availableOptions[$option], $this->options)) {
  132. $this->options[] = static::$availableOptions[$option];
  133. }
  134. }
  135. /**
  136. * Unsets some specific style option.
  137. *
  138. * @param string $option option name
  139. */
  140. public function unsetOption($option)
  141. {
  142. if (!isset(static::$availableOptions[$option])) {
  143. throw new \InvalidArgumentException(sprintf(
  144. 'Invalid option specified: "%s". Expected one of (%s)',
  145. $option,
  146. implode(', ', array_keys(static::$availableOptions))
  147. ));
  148. }
  149. $pos = array_search(static::$availableOptions[$option], $this->options);
  150. if (false !== $pos) {
  151. unset($this->options[$pos]);
  152. }
  153. }
  154. /**
  155. * Set multiple style options at once.
  156. *
  157. * @param array $options
  158. */
  159. public function setOptions(array $options)
  160. {
  161. $this->options = array();
  162. foreach ($options as $option) {
  163. $this->setOption($option);
  164. }
  165. }
  166. /**
  167. * Returns begin style code.
  168. *
  169. * @return string
  170. */
  171. public function getBeginStyle()
  172. {
  173. $codes = array();
  174. if (null !== $this->foreground) {
  175. $codes[] = $this->foreground;
  176. }
  177. if (null !== $this->background) {
  178. $codes[] = $this->background;
  179. }
  180. if (count($this->options)) {
  181. $codes = array_merge($codes, $this->options);
  182. }
  183. return "\033[" . implode(';', $codes) . 'm';
  184. }
  185. /**
  186. * Returns end style code.
  187. *
  188. * @return string
  189. */
  190. public function getEndStyle()
  191. {
  192. return "\033[0m";
  193. }
  194. }