Output.php 4.0 KB

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