OutputStyle.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public function __construct(
  23. private OutputInterface $output,
  24. ) {
  25. }
  26. public function newLine(int $count = 1): void
  27. {
  28. $this->output->write(str_repeat(\PHP_EOL, $count));
  29. }
  30. public function createProgressBar(int $max = 0): ProgressBar
  31. {
  32. return new ProgressBar($this->output, $max);
  33. }
  34. public function write(string|iterable $messages, bool $newline = false, int $type = self::OUTPUT_NORMAL): void
  35. {
  36. $this->output->write($messages, $newline, $type);
  37. }
  38. public function writeln(string|iterable $messages, int $type = self::OUTPUT_NORMAL): void
  39. {
  40. $this->output->writeln($messages, $type);
  41. }
  42. public function setVerbosity(int $level): void
  43. {
  44. $this->output->setVerbosity($level);
  45. }
  46. public function getVerbosity(): int
  47. {
  48. return $this->output->getVerbosity();
  49. }
  50. public function setDecorated(bool $decorated): void
  51. {
  52. $this->output->setDecorated($decorated);
  53. }
  54. public function isDecorated(): bool
  55. {
  56. return $this->output->isDecorated();
  57. }
  58. public function setFormatter(OutputFormatterInterface $formatter): void
  59. {
  60. $this->output->setFormatter($formatter);
  61. }
  62. public function getFormatter(): OutputFormatterInterface
  63. {
  64. return $this->output->getFormatter();
  65. }
  66. public function isSilent(): bool
  67. {
  68. // @deprecated since Symfony 7.2, change to $this->output->isSilent() in 8.0
  69. return method_exists($this->output, 'isSilent') ? $this->output->isSilent() : self::VERBOSITY_SILENT === $this->output->getVerbosity();
  70. }
  71. public function isQuiet(): bool
  72. {
  73. return $this->output->isQuiet();
  74. }
  75. public function isVerbose(): bool
  76. {
  77. return $this->output->isVerbose();
  78. }
  79. public function isVeryVerbose(): bool
  80. {
  81. return $this->output->isVeryVerbose();
  82. }
  83. public function isDebug(): bool
  84. {
  85. return $this->output->isDebug();
  86. }
  87. protected function getErrorOutput(): OutputInterface
  88. {
  89. if (!$this->output instanceof ConsoleOutputInterface) {
  90. return $this->output;
  91. }
  92. return $this->output->getErrorOutput();
  93. }
  94. }