ConsoleOutput.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /**
  30. * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  31. * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
  32. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  33. */
  34. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
  35. {
  36. parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
  37. $actualDecorated = $this->isDecorated();
  38. $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
  39. if (null === $decorated) {
  40. $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
  41. }
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function setDecorated($decorated)
  47. {
  48. parent::setDecorated($decorated);
  49. $this->stderr->setDecorated($decorated);
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function setFormatter(OutputFormatterInterface $formatter)
  55. {
  56. parent::setFormatter($formatter);
  57. $this->stderr->setFormatter($formatter);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function setVerbosity($level)
  63. {
  64. parent::setVerbosity($level);
  65. $this->stderr->setVerbosity($level);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getErrorOutput()
  71. {
  72. return $this->stderr;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function setErrorOutput(OutputInterface $error)
  78. {
  79. $this->stderr = $error;
  80. }
  81. /**
  82. * Returns true if current environment supports writing console output to
  83. * STDOUT.
  84. *
  85. * @return bool
  86. */
  87. protected function hasStdoutSupport()
  88. {
  89. return false === $this->isRunningOS400();
  90. }
  91. /**
  92. * Returns true if current environment supports writing console output to
  93. * STDERR.
  94. *
  95. * @return bool
  96. */
  97. protected function hasStderrSupport()
  98. {
  99. return false === $this->isRunningOS400();
  100. }
  101. /**
  102. * Checks if current executing environment is IBM iSeries (OS400), which
  103. * doesn't properly convert character-encodings between ASCII to EBCDIC.
  104. *
  105. * @return bool
  106. */
  107. private function isRunningOS400()
  108. {
  109. $checks = array(
  110. \function_exists('php_uname') ? php_uname('s') : '',
  111. getenv('OSTYPE'),
  112. PHP_OS,
  113. );
  114. return false !== stripos(implode(';', $checks), 'OS400');
  115. }
  116. /**
  117. * @return resource
  118. */
  119. private function openOutputStream()
  120. {
  121. if (!$this->hasStdoutSupport()) {
  122. return fopen('php://output', 'w');
  123. }
  124. return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
  125. }
  126. /**
  127. * @return resource
  128. */
  129. private function openErrorStream()
  130. {
  131. return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w');
  132. }
  133. }