Output.php 4.1 KB

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