ConsoleOutput.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  13. * ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.
  14. *
  15. * This class is a convenient wrapper around `StreamOutput` for both STDOUT and STDERR.
  16. *
  17. * $output = new ConsoleOutput();
  18. *
  19. * This is equivalent to:
  20. *
  21. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  22. * $stdErr = new StreamOutput(fopen('php://stderr', 'w'));
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. */
  26. class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
  27. {
  28. private OutputInterface $stderr;
  29. private array $consoleSectionOutputs = [];
  30. /**
  31. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  32. * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
  33. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  34. */
  35. public function __construct(int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null)
  36. {
  37. parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
  38. // stderr gets its own formatter: decoration is held by the formatter, so a shared one
  39. // would force both streams to the same state instead of detecting each independently
  40. $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $formatter ? clone $formatter : null);
  41. }
  42. /**
  43. * Creates a new output section.
  44. */
  45. public function section(): ConsoleSectionOutput
  46. {
  47. return new ConsoleSectionOutput($this->getStream(), $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
  48. }
  49. public function setDecorated(bool $decorated): void
  50. {
  51. parent::setDecorated($decorated);
  52. $this->stderr->setDecorated($decorated);
  53. }
  54. public function setFormatter(OutputFormatterInterface $formatter): void
  55. {
  56. parent::setFormatter($formatter);
  57. $this->stderr->setFormatter($formatter);
  58. }
  59. public function setVerbosity(int $level): void
  60. {
  61. parent::setVerbosity($level);
  62. $this->stderr->setVerbosity($level);
  63. }
  64. public function getErrorOutput(): OutputInterface
  65. {
  66. return $this->stderr;
  67. }
  68. public function setErrorOutput(OutputInterface $error): void
  69. {
  70. $this->stderr = $error;
  71. }
  72. /**
  73. * Returns true if current environment supports writing console output to
  74. * STDOUT.
  75. */
  76. protected function hasStdoutSupport(): bool
  77. {
  78. return !$this->isRunningOS400();
  79. }
  80. /**
  81. * Returns true if current environment supports writing console output to
  82. * STDERR.
  83. */
  84. protected function hasStderrSupport(): bool
  85. {
  86. return !$this->isRunningOS400();
  87. }
  88. /**
  89. * Checks if current executing environment is IBM iSeries (OS400), which
  90. * doesn't properly convert character-encodings between ASCII to EBCDIC.
  91. */
  92. private function isRunningOS400(): bool
  93. {
  94. $checks = [
  95. \function_exists('php_uname') ? php_uname('s') : '',
  96. getenv('OSTYPE'),
  97. \PHP_OS,
  98. ];
  99. return false !== stripos(implode(';', $checks), 'OS400');
  100. }
  101. /**
  102. * @return resource
  103. */
  104. private function openOutputStream()
  105. {
  106. static $stdout;
  107. if ($stdout) {
  108. return $stdout;
  109. }
  110. if (!$this->hasStdoutSupport()) {
  111. return $stdout = fopen('php://output', 'w');
  112. }
  113. // Use STDOUT when possible to prevent from opening too many file descriptors
  114. if (!\defined('STDOUT')) {
  115. return $stdout = @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
  116. }
  117. // On Windows, STDOUT is opened in text mode; reopen in binary mode to prevent \n to \r\n conversion
  118. if ('\\' === \DIRECTORY_SEPARATOR) {
  119. return $stdout = @fopen('php://stdout', 'w') ?: \STDOUT;
  120. }
  121. return $stdout = \STDOUT;
  122. }
  123. /**
  124. * @return resource
  125. */
  126. private function openErrorStream()
  127. {
  128. static $stderr;
  129. if ($stderr) {
  130. return $stderr;
  131. }
  132. if (!$this->hasStderrSupport()) {
  133. return $stderr = fopen('php://output', 'w');
  134. }
  135. // Use STDERR when possible to prevent from opening too many file descriptors
  136. if (!\defined('STDERR')) {
  137. return $stderr = @fopen('php://stderr', 'w') ?: fopen('php://output', 'w');
  138. }
  139. // On Windows, STDERR is opened in text mode; reopen in binary mode to prevent \n → \r\n conversion
  140. if ('\\' === \DIRECTORY_SEPARATOR) {
  141. return $stderr = @fopen('php://stderr', 'w') ?: \STDERR;
  142. }
  143. return $stderr ??= \STDERR;
  144. }
  145. }