Output.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 six 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 (only output errors)
  23. * * silent: --silent (no output)
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com>
  26. */
  27. abstract class Output implements OutputInterface
  28. {
  29. private int $verbosity;
  30. private OutputFormatterInterface $formatter;
  31. /**
  32. * @param int|null $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  33. * @param bool $decorated Whether to decorate messages
  34. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  35. */
  36. public function __construct(?int $verbosity = self::VERBOSITY_NORMAL, bool $decorated = false, ?OutputFormatterInterface $formatter = null)
  37. {
  38. $this->verbosity = $verbosity ?? self::VERBOSITY_NORMAL;
  39. $this->formatter = $formatter ?? new OutputFormatter();
  40. $this->formatter->setDecorated($decorated);
  41. }
  42. public function setFormatter(OutputFormatterInterface $formatter): void
  43. {
  44. $this->formatter = $formatter;
  45. }
  46. public function getFormatter(): OutputFormatterInterface
  47. {
  48. return $this->formatter;
  49. }
  50. public function setDecorated(bool $decorated): void
  51. {
  52. $this->formatter->setDecorated($decorated);
  53. }
  54. public function isDecorated(): bool
  55. {
  56. return $this->formatter->isDecorated();
  57. }
  58. public function setVerbosity(int $level): void
  59. {
  60. $this->verbosity = $level;
  61. }
  62. public function getVerbosity(): int
  63. {
  64. return $this->verbosity;
  65. }
  66. public function isSilent(): bool
  67. {
  68. return self::VERBOSITY_SILENT === $this->verbosity;
  69. }
  70. public function isQuiet(): bool
  71. {
  72. return self::VERBOSITY_QUIET === $this->verbosity;
  73. }
  74. public function isVerbose(): bool
  75. {
  76. return self::VERBOSITY_VERBOSE <= $this->verbosity;
  77. }
  78. public function isVeryVerbose(): bool
  79. {
  80. return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
  81. }
  82. public function isDebug(): bool
  83. {
  84. return self::VERBOSITY_DEBUG <= $this->verbosity;
  85. }
  86. public function writeln(string|iterable $messages, int $options = self::OUTPUT_NORMAL): void
  87. {
  88. $this->write($messages, true, $options);
  89. }
  90. public function write(string|iterable $messages, bool $newline = false, int $options = self::OUTPUT_NORMAL): void
  91. {
  92. if (!is_iterable($messages)) {
  93. $messages = [$messages];
  94. }
  95. $types = self::OUTPUT_NORMAL | self::OUTPUT_RAW | self::OUTPUT_PLAIN;
  96. $type = $types & $options ?: self::OUTPUT_NORMAL;
  97. $verbosities = self::VERBOSITY_QUIET | self::VERBOSITY_NORMAL | self::VERBOSITY_VERBOSE | self::VERBOSITY_VERY_VERBOSE | self::VERBOSITY_DEBUG;
  98. $verbosity = $verbosities & $options ?: self::VERBOSITY_NORMAL;
  99. if ($verbosity > $this->getVerbosity()) {
  100. return;
  101. }
  102. foreach ($messages as $message) {
  103. switch ($type) {
  104. case OutputInterface::OUTPUT_NORMAL:
  105. $message = $this->formatter->format($message);
  106. break;
  107. case OutputInterface::OUTPUT_RAW:
  108. break;
  109. case OutputInterface::OUTPUT_PLAIN:
  110. $message = strip_tags($this->formatter->format($message));
  111. break;
  112. }
  113. $this->doWrite($message ?? '', $newline);
  114. }
  115. }
  116. /**
  117. * Writes a message to the output.
  118. */
  119. abstract protected function doWrite(string $message, bool $newline): void;
  120. }