Output.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 three levels of verbosity:
  17. *
  18. * * normal: no option passed (normal output - information)
  19. * * verbose: -v (more output - debug)
  20. * * quiet: -q (no output)
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @api
  25. */
  26. abstract class Output implements OutputInterface
  27. {
  28. private $verbosity;
  29. private $formatter;
  30. /**
  31. * Constructor.
  32. *
  33. * @param integer $verbosity The verbosity level (self::VERBOSITY_QUIET, self::VERBOSITY_NORMAL, self::VERBOSITY_VERBOSE)
  34. * @param Boolean $decorated Whether to decorate messages or not (null for auto-guessing)
  35. * @param OutputFormatterInterface $formatter Output formatter instance
  36. *
  37. * @api
  38. */
  39. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
  40. {
  41. $this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
  42. $this->formatter = null === $formatter ? new OutputFormatter() : $formatter;
  43. $this->formatter->setDecorated((Boolean) $decorated);
  44. }
  45. /**
  46. * Sets output formatter.
  47. *
  48. * @param OutputFormatterInterface $formatter
  49. *
  50. * @api
  51. */
  52. public function setFormatter(OutputFormatterInterface $formatter)
  53. {
  54. $this->formatter = $formatter;
  55. }
  56. /**
  57. * Returns current output formatter instance.
  58. *
  59. * @return OutputFormatterInterface
  60. *
  61. * @api
  62. */
  63. public function getFormatter()
  64. {
  65. return $this->formatter;
  66. }
  67. /**
  68. * Sets the decorated flag.
  69. *
  70. * @param Boolean $decorated Whether to decorate the messages or not
  71. *
  72. * @api
  73. */
  74. public function setDecorated($decorated)
  75. {
  76. $this->formatter->setDecorated((Boolean) $decorated);
  77. }
  78. /**
  79. * Gets the decorated flag.
  80. *
  81. * @return Boolean true if the output will decorate messages, false otherwise
  82. *
  83. * @api
  84. */
  85. public function isDecorated()
  86. {
  87. return $this->formatter->isDecorated();
  88. }
  89. /**
  90. * Sets the verbosity of the output.
  91. *
  92. * @param integer $level The level of verbosity
  93. *
  94. * @api
  95. */
  96. public function setVerbosity($level)
  97. {
  98. $this->verbosity = (int) $level;
  99. }
  100. /**
  101. * Gets the current verbosity of the output.
  102. *
  103. * @return integer The current level of verbosity
  104. *
  105. * @api
  106. */
  107. public function getVerbosity()
  108. {
  109. return $this->verbosity;
  110. }
  111. /**
  112. * Writes a message to the output and adds a newline at the end.
  113. *
  114. * @param string|array $messages The message as an array of lines of a single string
  115. * @param integer $type The type of output
  116. *
  117. * @api
  118. */
  119. public function writeln($messages, $type = 0)
  120. {
  121. $this->write($messages, true, $type);
  122. }
  123. /**
  124. * Writes a message to the output.
  125. *
  126. * @param string|array $messages The message as an array of lines of a single string
  127. * @param Boolean $newline Whether to add a newline or not
  128. * @param integer $type The type of output
  129. *
  130. * @throws \InvalidArgumentException When unknown output type is given
  131. *
  132. * @api
  133. */
  134. public function write($messages, $newline = false, $type = 0)
  135. {
  136. if (self::VERBOSITY_QUIET === $this->verbosity) {
  137. return;
  138. }
  139. $messages = (array) $messages;
  140. foreach ($messages as $message) {
  141. switch ($type) {
  142. case OutputInterface::OUTPUT_NORMAL:
  143. $message = $this->formatter->format($message);
  144. break;
  145. case OutputInterface::OUTPUT_RAW:
  146. break;
  147. case OutputInterface::OUTPUT_PLAIN:
  148. $message = strip_tags($this->formatter->format($message));
  149. break;
  150. default:
  151. throw new \InvalidArgumentException(sprintf('Unknown output type given (%s)', $type));
  152. }
  153. $this->doWrite($message, $newline);
  154. }
  155. }
  156. /**
  157. * Writes a message to the output.
  158. *
  159. * @param string $message A message to write to the output
  160. * @param Boolean $newline Whether to add a newline or not
  161. */
  162. abstract public function doWrite($message, $newline);
  163. }