PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/Console/Output/Output.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 231 lines | 118 code | 30 blank | 83 comment | 12 complexity | 24edb4f0f4eff8f458d12f0ab85d72a0 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Output;
  11. /**
  12. * Base class for output classes.
  13. *
  14. * There is three level of verbosity:
  15. *
  16. * * normal: no option passed (normal output - information)
  17. * * verbose: -v (more output - debug)
  18. * * quiet: -q (no output)
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. abstract class Output implements OutputInterface
  23. {
  24. const VERBOSITY_QUIET = 0;
  25. const VERBOSITY_NORMAL = 1;
  26. const VERBOSITY_VERBOSE = 2;
  27. const OUTPUT_NORMAL = 0;
  28. const OUTPUT_RAW = 1;
  29. const OUTPUT_PLAIN = 2;
  30. protected $verbosity;
  31. protected $decorated;
  32. static protected $styles = array(
  33. 'error' => array('bg' => 'red', 'fg' => 'white'),
  34. 'info' => array('fg' => 'green'),
  35. 'comment' => array('fg' => 'yellow'),
  36. 'question' => array('bg' => 'cyan', 'fg' => 'black'),
  37. );
  38. static protected $options = array('bold' => 1, 'underscore' => 4, 'blink' => 5, 'reverse' => 7, 'conceal' => 8);
  39. static protected $foreground = array('black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37);
  40. static protected $background = array('black' => 40, 'red' => 41, 'green' => 42, 'yellow' => 43, 'blue' => 44, 'magenta' => 45, 'cyan' => 46, 'white' => 47);
  41. /**
  42. * Constructor.
  43. *
  44. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  45. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  46. */
  47. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null)
  48. {
  49. $this->decorated = (Boolean) $decorated;
  50. $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
  51. }
  52. /**
  53. * Sets a new style.
  54. *
  55. * @param string $name The style name
  56. * @param array $options An array of options
  57. */
  58. static public function setStyle($name, $options = array())
  59. {
  60. static::$styles[strtolower($name)] = $options;
  61. }
  62. /**
  63. * Sets the decorated flag.
  64. *
  65. * @param Boolean $decorated Whether to decorated the messages or not
  66. */
  67. public function setDecorated($decorated)
  68. {
  69. $this->decorated = (Boolean) $decorated;
  70. }
  71. /**
  72. * Gets the decorated flag.
  73. *
  74. * @return Boolean true if the output will decorate messages, false otherwise
  75. */
  76. public function isDecorated()
  77. {
  78. return $this->decorated;
  79. }
  80. /**
  81. * Sets the verbosity of the output.
  82. *
  83. * @param integer $level The level of verbosity
  84. */
  85. public function setVerbosity($level)
  86. {
  87. $this->verbosity = (int) $level;
  88. }
  89. /**
  90. * Gets the current verbosity of the output.
  91. *
  92. * @return integer The current level of verbosity
  93. */
  94. public function getVerbosity()
  95. {
  96. return $this->verbosity;
  97. }
  98. /**
  99. * Writes a message to the output and adds a newline at the end.
  100. *
  101. * @param string|array $messages The message as an array of lines of a single string
  102. * @param integer $type The type of output
  103. */
  104. public function writeln($messages, $type = 0)
  105. {
  106. $this->write($messages, true, $type);
  107. }
  108. /**
  109. * Writes a message to the output.
  110. *
  111. * @param string|array $messages The message as an array of lines of a single string
  112. * @param Boolean $newline Whether to add a newline or not
  113. * @param integer $type The type of output
  114. *
  115. * @throws \InvalidArgumentException When unknown output type is given
  116. */
  117. public function write($messages, $newline = false, $type = 0)
  118. {
  119. if (self::VERBOSITY_QUIET === $this->verbosity) {
  120. return;
  121. }
  122. if (!is_array($messages)) {
  123. $messages = array($messages);
  124. }
  125. foreach ($messages as $message) {
  126. switch ($type) {
  127. case Output::OUTPUT_NORMAL:
  128. $message = $this->format($message);
  129. break;
  130. case Output::OUTPUT_RAW:
  131. break;
  132. case Output::OUTPUT_PLAIN:
  133. $message = strip_tags($this->format($message));
  134. break;
  135. default:
  136. throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
  137. }
  138. $this->doWrite($message, $newline);
  139. }
  140. }
  141. /**
  142. * Writes a message to the output.
  143. *
  144. * @param string $message A message to write to the output
  145. * @param Boolean $newline Whether to add a newline or not
  146. */
  147. abstract public function doWrite($message, $newline);
  148. /**
  149. * Formats a message according to the given styles.
  150. *
  151. * @param string $message The message to style
  152. *
  153. * @return string The styled message
  154. */
  155. protected function format($message)
  156. {
  157. $message = preg_replace_callback('#<([a-z][a-z0-9\-_=;]+)>#i', array($this, 'replaceStartStyle'), $message);
  158. return preg_replace_callback('#</([a-z][a-z0-9\-_]*)?>#i', array($this, 'replaceEndStyle'), $message);
  159. }
  160. /**
  161. * @throws \InvalidArgumentException When style is unknown
  162. */
  163. protected function replaceStartStyle($match)
  164. {
  165. if (!$this->decorated) {
  166. return '';
  167. }
  168. if (isset(static::$styles[strtolower($match[1])])) {
  169. $parameters = static::$styles[strtolower($match[1])];
  170. } else {
  171. // bg=blue;fg=red
  172. if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($match[1]), $matches, PREG_SET_ORDER)) {
  173. throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
  174. }
  175. $parameters = array();
  176. foreach ($matches as $match) {
  177. $parameters[$match[1]] = $match[2];
  178. }
  179. }
  180. $codes = array();
  181. if (isset($parameters['fg'])) {
  182. $codes[] = static::$foreground[$parameters['fg']];
  183. }
  184. if (isset($parameters['bg'])) {
  185. $codes[] = static::$background[$parameters['bg']];
  186. }
  187. foreach (static::$options as $option => $value) {
  188. if (isset($parameters[$option]) && $parameters[$option]) {
  189. $codes[] = $value;
  190. }
  191. }
  192. return "\033[".implode(';', $codes).'m';
  193. }
  194. protected function replaceEndStyle($match)
  195. {
  196. if (!$this->decorated) {
  197. return '';
  198. }
  199. return "\033[0m";
  200. }
  201. }