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