No Description

ConsoleOutput.php 2.9KB

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