You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.5 KiB

3 years ago
  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 $stderr;
  29. private $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. if (null === $formatter) {
  39. // for BC reasons, stdErr has it own Formatter only when user don't inject a specific formatter.
  40. $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated);
  41. return;
  42. }
  43. $actualDecorated = $this->isDecorated();
  44. $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
  45. if (null === $decorated) {
  46. $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
  47. }
  48. }
  49. /**
  50. * Creates a new output section.
  51. */
  52. public function section(): ConsoleSectionOutput
  53. {
  54. return new ConsoleSectionOutput($this->getStream(), $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function setDecorated(bool $decorated)
  60. {
  61. parent::setDecorated($decorated);
  62. $this->stderr->setDecorated($decorated);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setFormatter(OutputFormatterInterface $formatter)
  68. {
  69. parent::setFormatter($formatter);
  70. $this->stderr->setFormatter($formatter);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function setVerbosity(int $level)
  76. {
  77. parent::setVerbosity($level);
  78. $this->stderr->setVerbosity($level);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function getErrorOutput()
  84. {
  85. return $this->stderr;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function setErrorOutput(OutputInterface $error)
  91. {
  92. $this->stderr = $error;
  93. }
  94. /**
  95. * Returns true if current environment supports writing console output to
  96. * STDOUT.
  97. *
  98. * @return bool
  99. */
  100. protected function hasStdoutSupport()
  101. {
  102. return false === $this->isRunningOS400();
  103. }
  104. /**
  105. * Returns true if current environment supports writing console output to
  106. * STDERR.
  107. *
  108. * @return bool
  109. */
  110. protected function hasStderrSupport()
  111. {
  112. return false === $this->isRunningOS400();
  113. }
  114. /**
  115. * Checks if current executing environment is IBM iSeries (OS400), which
  116. * doesn't properly convert character-encodings between ASCII to EBCDIC.
  117. */
  118. private function isRunningOS400(): bool
  119. {
  120. $checks = [
  121. \function_exists('php_uname') ? php_uname('s') : '',
  122. getenv('OSTYPE'),
  123. \PHP_OS,
  124. ];
  125. return false !== stripos(implode(';', $checks), 'OS400');
  126. }
  127. /**
  128. * @return resource
  129. */
  130. private function openOutputStream()
  131. {
  132. if (!$this->hasStdoutSupport()) {
  133. return fopen('php://output', 'w');
  134. }
  135. return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
  136. }
  137. /**
  138. * @return resource
  139. */
  140. private function openErrorStream()
  141. {
  142. return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w');
  143. }
  144. }