Output.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /**
  29. * @var int
  30. */
  31. private $verbosity;
  32. /**
  33. * @var \Symfony\Component\Console\Formatter\OutputFormatterInterface
  34. */
  35. private $formatter;
  36. /**
  37. * @param int|null $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  38. * @param bool $decorated Whether to decorate messages
  39. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  40. */
  41. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, $formatter = null)
  42. {
  43. $this->verbosity = $verbosity ?? self::VERBOSITY_NORMAL;
  44. $this->formatter = $formatter ?? new OutputFormatter();
  45. $this->formatter->setDecorated($decorated);
  46. }
  47. /**
  48. * @return void
  49. * @param \Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter
  50. */
  51. public function setFormatter($formatter)
  52. {
  53. $this->formatter = $formatter;
  54. }
  55. public function getFormatter()
  56. {
  57. return $this->formatter;
  58. }
  59. /**
  60. * @return void
  61. * @param bool $decorated
  62. */
  63. public function setDecorated($decorated)
  64. {
  65. $this->formatter->setDecorated($decorated);
  66. }
  67. public function isDecorated()
  68. {
  69. return $this->formatter->isDecorated();
  70. }
  71. /**
  72. * @return void
  73. * @param int $level
  74. */
  75. public function setVerbosity($level)
  76. {
  77. $this->verbosity = $level;
  78. }
  79. public function getVerbosity()
  80. {
  81. return $this->verbosity;
  82. }
  83. public function isQuiet()
  84. {
  85. return self::VERBOSITY_QUIET === $this->verbosity;
  86. }
  87. public function isVerbose()
  88. {
  89. return self::VERBOSITY_VERBOSE <= $this->verbosity;
  90. }
  91. public function isVeryVerbose()
  92. {
  93. return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
  94. }
  95. public function isDebug()
  96. {
  97. return self::VERBOSITY_DEBUG <= $this->verbosity;
  98. }
  99. /**
  100. * @return void
  101. * @param string|mixed[] $messages
  102. * @param int $options
  103. */
  104. public function writeln($messages, $options = self::OUTPUT_NORMAL)
  105. {
  106. $this->write($messages, true, $options);
  107. }
  108. /**
  109. * @return void
  110. * @param string|mixed[] $messages
  111. * @param bool $newline
  112. * @param int $options
  113. */
  114. public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
  115. {
  116. if (!is_iterable($messages)) {
  117. $messages = [$messages];
  118. }
  119. $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
  120. $type = $types & $options ?: self::OUTPUT_NORMAL;
  121. $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
  122. $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
  123. if ($verbosity > $this->getVerbosity()) {
  124. return;
  125. }
  126. foreach ($messages as $message) {
  127. switch ($type) {
  128. case OutputInterface::OUTPUT_NORMAL:
  129. $message = $this->formatter->format($message);
  130. break;
  131. case OutputInterface::OUTPUT_RAW:
  132. break;
  133. case OutputInterface::OUTPUT_PLAIN:
  134. $message = strip_tags($this->formatter->format($message));
  135. break;
  136. }
  137. $this->doWrite($message ?? '', $newline);
  138. }
  139. }
  140. /**
  141. * Writes a message to the output.
  142. *
  143. * @return void
  144. * @param string $message
  145. * @param bool $newline
  146. */
  147. abstract protected function doWrite($message, $newline);
  148. }