Output.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  36. * @param Boolean $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 = null === $formatter ? new OutputFormatter() : $formatter;
  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. /**
  90. * {@inheritdoc}
  91. */
  92. public function writeln($messages, $type = self::OUTPUT_NORMAL)
  93. {
  94. $this->write($messages, true, $type);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
  100. {
  101. if (self::VERBOSITY_QUIET === $this->verbosity) {
  102. return;
  103. }
  104. $messages = (array) $messages;
  105. foreach ($messages as $message) {
  106. switch ($type) {
  107. case OutputInterface::OUTPUT_NORMAL:
  108. $message = $this->formatter->format($message);
  109. break;
  110. case OutputInterface::OUTPUT_RAW:
  111. break;
  112. case OutputInterface::OUTPUT_PLAIN:
  113. $message = strip_tags($this->formatter->format($message));
  114. break;
  115. default:
  116. throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
  117. }
  118. $this->doWrite($message, $newline);
  119. }
  120. }
  121. /**
  122. * Writes a message to the output.
  123. *
  124. * @param string $message A message to write to the output
  125. * @param Boolean $newline Whether to add a newline or not
  126. */
  127. abstract protected function doWrite($message, $newline);
  128. }