Output.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\OutputFormatterInterface;
  12. use Symfony\Component\Console\Formatter\OutputFormatter;
  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. * Constructor.
  32. *
  33. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  34. * @param bool $decorated Whether to decorate messages
  35. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  36. */
  37. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null)
  38. {
  39. $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
  40. $this->formatter = $formatter ?: new OutputFormatter();
  41. $this->formatter->setDecorated($decorated);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function setFormatter(OutputFormatterInterface $formatter)
  47. {
  48. $this->formatter = $formatter;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getFormatter()
  54. {
  55. return $this->formatter;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function setDecorated($decorated)
  61. {
  62. $this->formatter->setDecorated($decorated);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function isDecorated()
  68. {
  69. return $this->formatter->isDecorated();
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function setVerbosity($level)
  75. {
  76. $this->verbosity = (int) $level;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getVerbosity()
  82. {
  83. return $this->verbosity;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function isQuiet()
  89. {
  90. return self::VERBOSITY_QUIET === $this->verbosity;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function isVerbose()
  96. {
  97. return self::VERBOSITY_VERBOSE <= $this->verbosity;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function isVeryVerbose()
  103. {
  104. return self::VERBOSITY_VERY_VERBOSE <= $this->verbosity;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function isDebug()
  110. {
  111. return self::VERBOSITY_DEBUG <= $this->verbosity;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function writeln($messages, $options = self::OUTPUT_NORMAL)
  117. {
  118. $this->write($messages, true, $options);
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function write($messages, $newline = false, $options = self::OUTPUT_NORMAL)
  124. {
  125. $messages = (array) $messages;
  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. }