ConsoleOutput.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /**
  29. * @var \Symfony\Component\Console\Output\OutputInterface
  30. */
  31. private $stderr;
  32. /**
  33. * @var mixed[]
  34. */
  35. private $consoleSectionOutputs = [];
  36. /**
  37. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  38. * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
  39. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  40. */
  41. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, $formatter = null)
  42. {
  43. parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
  44. if (null === $formatter) {
  45. // for BC reasons, stdErr has it own Formatter only when user don't inject a specific formatter.
  46. $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated);
  47. return;
  48. }
  49. $actualDecorated = $this->isDecorated();
  50. $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
  51. if (null === $decorated) {
  52. $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
  53. }
  54. }
  55. /**
  56. * Creates a new output section.
  57. */
  58. public function section()
  59. {
  60. return new ConsoleSectionOutput($this->getStream(), $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
  61. }
  62. /**
  63. * @return void
  64. * @param bool $decorated
  65. */
  66. public function setDecorated($decorated)
  67. {
  68. parent::setDecorated($decorated);
  69. $this->stderr->setDecorated($decorated);
  70. }
  71. /**
  72. * @return void
  73. * @param \Symfony\Component\Console\Formatter\OutputFormatterInterface $formatter
  74. */
  75. public function setFormatter($formatter)
  76. {
  77. parent::setFormatter($formatter);
  78. $this->stderr->setFormatter($formatter);
  79. }
  80. /**
  81. * @return void
  82. * @param int $level
  83. */
  84. public function setVerbosity($level)
  85. {
  86. parent::setVerbosity($level);
  87. $this->stderr->setVerbosity($level);
  88. }
  89. public function getErrorOutput()
  90. {
  91. return $this->stderr;
  92. }
  93. /**
  94. * @return void
  95. * @param \Symfony\Component\Console\Output\OutputInterface $error
  96. */
  97. public function setErrorOutput($error)
  98. {
  99. $this->stderr = $error;
  100. }
  101. /**
  102. * Returns true if current environment supports writing console output to
  103. * STDOUT.
  104. */
  105. protected function hasStdoutSupport()
  106. {
  107. return false === $this->isRunningOS400();
  108. }
  109. /**
  110. * Returns true if current environment supports writing console output to
  111. * STDERR.
  112. */
  113. protected function hasStderrSupport()
  114. {
  115. return false === $this->isRunningOS400();
  116. }
  117. /**
  118. * Checks if current executing environment is IBM iSeries (OS400), which
  119. * doesn't properly convert character-encodings between ASCII to EBCDIC.
  120. */
  121. private function isRunningOS400()
  122. {
  123. $checks = [
  124. \function_exists('php_uname') ? php_uname('s') : '',
  125. getenv('OSTYPE'),
  126. \PHP_OS,
  127. ];
  128. return false !== stripos(implode(';', $checks), 'OS400');
  129. }
  130. /**
  131. * @return resource
  132. */
  133. private function openOutputStream()
  134. {
  135. if (!$this->hasStdoutSupport()) {
  136. return fopen('php://output', 'w');
  137. }
  138. // Use STDOUT when possible to prevent from opening too many file descriptors
  139. return \defined('STDOUT') ? \STDOUT : (@fopen('php://stdout', 'w') ?: fopen('php://output', 'w'));
  140. }
  141. /**
  142. * @return resource
  143. */
  144. private function openErrorStream()
  145. {
  146. if (!$this->hasStderrSupport()) {
  147. return fopen('php://output', 'w');
  148. }
  149. // Use STDERR when possible to prevent from opening too many file descriptors
  150. return \defined('STDERR') ? \STDERR : (@fopen('php://stderr', 'w') ?: fopen('php://output', 'w'));
  151. }
  152. }