ConsoleOutput.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  13. /**
  14. * ConsoleOutput is the default class for all CLI output. It uses STDOUT.
  15. *
  16. * This class is a convenient wrapper around `StreamOutput`.
  17. *
  18. * $output = new ConsoleOutput();
  19. *
  20. * This is equivalent to:
  21. *
  22. * $output = new StreamOutput(fopen('php://stdout', 'w'));
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. *
  26. * @api
  27. */
  28. class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
  29. {
  30. private $stderr;
  31. /**
  32. * Constructor.
  33. *
  34. * @param integer $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
  35. * @param Boolean|null $decorated Whether to decorate messages (null for auto-guessing)
  36. * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
  37. *
  38. * @api
  39. */
  40. public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
  41. {
  42. $outputStream = 'php://stdout';
  43. if (!$this->hasStdoutSupport()) {
  44. $outputStream = 'php://output';
  45. }
  46. parent::__construct(fopen($outputStream, 'w'), $verbosity, $decorated, $formatter);
  47. $this->stderr = new StreamOutput(fopen('php://stderr', 'w'), $verbosity, $decorated, $formatter);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function setDecorated($decorated)
  53. {
  54. parent::setDecorated($decorated);
  55. $this->stderr->setDecorated($decorated);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function setFormatter(OutputFormatterInterface $formatter)
  61. {
  62. parent::setFormatter($formatter);
  63. $this->stderr->setFormatter($formatter);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setVerbosity($level)
  69. {
  70. parent::setVerbosity($level);
  71. $this->stderr->setVerbosity($level);
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getErrorOutput()
  77. {
  78. return $this->stderr;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function setErrorOutput(OutputInterface $error)
  84. {
  85. $this->stderr = $error;
  86. }
  87. /**
  88. * Returns true if current environment supports writing console output to
  89. * STDOUT.
  90. *
  91. * IBM iSeries (OS400) exhibits character-encoding issues when writing to
  92. * STDOUT and doesn't properly convert ASCII to EBCDIC, resulting in garbage
  93. * output.
  94. *
  95. * @return boolean
  96. */
  97. protected function hasStdoutSupport()
  98. {
  99. return ('OS400' != php_uname('s'));
  100. }
  101. }