OutputStyle.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 OutputInterface $output;
  23. public function __construct(OutputInterface $output)
  24. {
  25. $this->output = $output;
  26. }
  27. /**
  28. * @return void
  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. * @return void
  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. * @return void
  47. */
  48. public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL)
  49. {
  50. $this->output->writeln($messages, $type);
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function setVerbosity(int $level)
  56. {
  57. $this->output->setVerbosity($level);
  58. }
  59. public function getVerbosity(): int
  60. {
  61. return $this->output->getVerbosity();
  62. }
  63. /**
  64. * @return void
  65. */
  66. public function setDecorated(bool $decorated)
  67. {
  68. $this->output->setDecorated($decorated);
  69. }
  70. public function isDecorated(): bool
  71. {
  72. return $this->output->isDecorated();
  73. }
  74. /**
  75. * @return void
  76. */
  77. public function setFormatter(OutputFormatterInterface $formatter)
  78. {
  79. $this->output->setFormatter($formatter);
  80. }
  81. public function getFormatter(): OutputFormatterInterface
  82. {
  83. return $this->output->getFormatter();
  84. }
  85. public function isQuiet(): bool
  86. {
  87. return $this->output->isQuiet();
  88. }
  89. public function isVerbose(): bool
  90. {
  91. return $this->output->isVerbose();
  92. }
  93. public function isVeryVerbose(): bool
  94. {
  95. return $this->output->isVeryVerbose();
  96. }
  97. public function isDebug(): bool
  98. {
  99. return $this->output->isDebug();
  100. }
  101. /**
  102. * @return OutputInterface
  103. */
  104. protected function getErrorOutput()
  105. {
  106. if (!$this->output instanceof ConsoleOutputInterface) {
  107. return $this->output;
  108. }
  109. return $this->output->getErrorOutput();
  110. }
  111. }