Output.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\OutputFormatter;
  12. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  13. /**
  14. * Base class for output classes.
  15. *
  16. * There are five levels of verbosity:
  17. *
  18. * * normal: no option passed (normal output)
  19. * * verbose: -v (more output)
  20. * * very verbose: -vv (highly extended output)
  21. * * debug: -vvv (all debug output)
  22. * * quiet: -q (no output)
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. abstract class Output implements OutputInterface
  27. {
  28. private $verbosity;
  29. private $formatter;
  30. /**
  31. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  32. * @param bool $decorated Whether to decorate messages
  33. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  34. */
  35. public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, OutputFormatterInterface $formatter = null)
  36. {
  37. $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
  38. $this->formatter = $formatter ?: new OutputFormatter();
  39. $this->formatter->setDecorated($decorated);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function setFormatter(OutputFormatterInterface $formatter)
  45. {
  46. $this->formatter = $formatter;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getFormatter()
  52. {
  53. return $this->formatter;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function setDecorated($decorated)
  59. {
  60. $this->formatter->setDecorated($decorated);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function isDecorated()
  66. {
  67. return $this->formatter->isDecorated();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function setVerbosity($level)
  73. {
  74. $this->verbosity = (int) $level;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getVerbosity()
  80. {
  81. return $this->verbosity;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function isQuiet()
  87. {
  88. return self::VERBOSITY_QUIET === $this->verbosity;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function isVerbose()
  94. {
  95. return self::VERBOSITY_VERBOSE <= $this->verbosity;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function isVeryVerbose()
  101. {
  102. return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function isDebug()
  108. {
  109. return self::VERBOSITY_DEBUG <= $this->verbosity;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function writeln($messages, $options = self::OUTPUT_NORMAL)
  115. {
  116. $this->write($messages, true, $options);
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
  122. {
  123. if (!is_iterable($messages)) {
  124. $messages = [$messages];
  125. }
  126. $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
  127. $type = $types & $options ?: self::OUTPUT_NORMAL;
  128. $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
  129. $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
  130. if ($verbosity > $this->getVerbosity()) {
  131. return;
  132. }
  133. foreach ($messages as $message) {
  134. switch ($type) {
  135. case OutputInterface::OUTPUT_NORMAL:
  136. $message = $this->formatter->format($message);
  137. break;
  138. case OutputInterface::OUTPUT_RAW:
  139. break;
  140. case OutputInterface::OUTPUT_PLAIN:
  141. $message = strip_tags($this->formatter->format($message));
  142. break;
  143. }
  144. $this->doWrite($message, $newline);
  145. }
  146. }
  147. /**
  148. * Writes a message to the output.
  149. *
  150. * @param string $message A message to write to the output
  151. * @param bool $newline Whether to add a newline or not
  152. */
  153. abstract protected function doWrite($message, $newline);
  154. }