OutputStyle.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\Style;
  11. use Symfony\Component\Console\Formatter\OutputFormatterInterface;
  12. use Symfony\Component\Console\Helper\ProgressBar;
  13. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * Decorates output to add console style guide helpers.
  17. *
  18. * @author Kevin Bond <kevinbond@gmail.com>
  19. */
  20. abstract class OutputStyle implements OutputInterface, StyleInterface
  21. {
  22. private $output;
  23. public function __construct(OutputInterface $output)
  24. {
  25. $this->output = $output;
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function newLine(int $count = 1)
  31. {
  32. $this->output->write(str_repeat(\PHP_EOL, $count));
  33. }
  34. public function createProgressBar(int $max = 0): ProgressBar
  35. {
  36. return new ProgressBar($this->output, $max);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function write(string|iterable $messages, bool $newline = false, int $type = self::OUTPUT_NORMAL)
  42. {
  43. $this->output->write($messages, $newline, $type);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL)
  49. {
  50. $this->output->writeln($messages, $type);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function setVerbosity(int $level)
  56. {
  57. $this->output->setVerbosity($level);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getVerbosity(): int
  63. {
  64. return $this->output->getVerbosity();
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function setDecorated(bool $decorated)
  70. {
  71. $this->output->setDecorated($decorated);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function isDecorated(): bool
  77. {
  78. return $this->output->isDecorated();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function setFormatter(OutputFormatterInterface $formatter)
  84. {
  85. $this->output->setFormatter($formatter);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function getFormatter(): OutputFormatterInterface
  91. {
  92. return $this->output->getFormatter();
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function isQuiet(): bool
  98. {
  99. return $this->output->isQuiet();
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function isVerbose(): bool
  105. {
  106. return $this->output->isVerbose();
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function isVeryVerbose(): bool
  112. {
  113. return $this->output->isVeryVerbose();
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function isDebug(): bool
  119. {
  120. return $this->output->isDebug();
  121. }
  122. protected function getErrorOutput()
  123. {
  124. if (!$this->output instanceof ConsoleOutputInterface) {
  125. return $this->output;
  126. }
  127. return $this->output->getErrorOutput();
  128. }
  129. }