Output.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\Output;
  11. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  13. /**
  14. * Base class for output classes.
  15. *
  16. * There is three level of verbosity:
  17. *
  18. * * normal: no option passed (normal output - information)
  19. * * verbose: -v (more output - debug)
  20. * * quiet: -q (no output)
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @api
  25. */
  26. abstract class Output implements OutputInterface
  27. {
  28. private $verbosity;
  29. private $formatter;
  30. /**
  31. * Constructor.
  32. *
  33. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  34. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  35. * @param OutputFormatterInterface $formatter Output formatter instance
  36. *
  37. * @api
  38. */
  39. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
  40. {
  41. if (null === $formatter) {
  42. $formatter = new OutputFormatter();
  43. }
  44. $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
  45. $this->formatter = $formatter;
  46. $this->formatter->setDecorated((Boolean) $decorated);
  47. }
  48. /**
  49. * Sets output formatter.
  50. *
  51. * @param OutputFormatterInterface $formatter
  52. *
  53. * @api
  54. */
  55. public function setFormatter(OutputFormatterInterface $formatter)
  56. {
  57. $this->formatter = $formatter;
  58. }
  59. /**
  60. * Returns current output formatter instance.
  61. *
  62. * @return OutputFormatterInterface
  63. *
  64. * @api
  65. */
  66. public function getFormatter()
  67. {
  68. return $this->formatter;
  69. }
  70. /**
  71. * Sets the decorated flag.
  72. *
  73. * @param Boolean $decorated Whether to decorated the messages or not
  74. *
  75. * @api
  76. */
  77. public function setDecorated($decorated)
  78. {
  79. $this->formatter->setDecorated((Boolean) $decorated);
  80. }
  81. /**
  82. * Gets the decorated flag.
  83. *
  84. * @return Boolean true if the output will decorate messages, false otherwise
  85. *
  86. * @api
  87. */
  88. public function isDecorated()
  89. {
  90. return $this->formatter->isDecorated();
  91. }
  92. /**
  93. * Sets the verbosity of the output.
  94. *
  95. * @param integer $level The level of verbosity
  96. *
  97. * @api
  98. */
  99. public function setVerbosity($level)
  100. {
  101. $this->verbosity = (int) $level;
  102. }
  103. /**
  104. * Gets the current verbosity of the output.
  105. *
  106. * @return integer The current level of verbosity
  107. *
  108. * @api
  109. */
  110. public function getVerbosity()
  111. {
  112. return $this->verbosity;
  113. }
  114. /**
  115. * Writes a message to the output and adds a newline at the end.
  116. *
  117. * @param string|array $messages The message as an array of lines of a single string
  118. * @param integer $type The type of output
  119. *
  120. * @api
  121. */
  122. public function writeln($messages, $type = 0)
  123. {
  124. $this->write($messages, true, $type);
  125. }
  126. /**
  127. * Writes a message to the output.
  128. *
  129. * @param string|array $messages The message as an array of lines of a single string
  130. * @param Boolean $newline Whether to add a newline or not
  131. * @param integer $type The type of output
  132. *
  133. * @throws \InvalidArgumentException When unknown output type is given
  134. *
  135. * @api
  136. */
  137. public function write($messages, $newline = false, $type = 0)
  138. {
  139. if (self::VERBOSITY_QUIET === $this->verbosity) {
  140. return;
  141. }
  142. if (!is_array($messages)) {
  143. $messages = array($messages);
  144. }
  145. foreach ($messages as $message) {
  146. switch ($type) {
  147. case OutputInterface::OUTPUT_NORMAL:
  148. $message = $this->formatter->format($message);
  149. break;
  150. case OutputInterface::OUTPUT_RAW:
  151. break;
  152. case OutputInterface::OUTPUT_PLAIN:
  153. $message = strip_tags($this->formatter->format($message));
  154. break;
  155. default:
  156. throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
  157. }
  158. $this->doWrite($message, $newline);
  159. }
  160. }
  161. /**
  162. * Writes a message to the output.
  163. *
  164. * @param string $message A message to write to the output
  165. * @param Boolean $newline Whether to add a newline or not
  166. */
  167. abstract public function doWrite($message, $newline);
  168. }