Output.php 4.2 KB

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