4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Output;
14 use Symfony\Component\Console\Formatter\OutputFormatterInterface;
17 * ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.
19 * This class is a convenient wrapper around `StreamOutput` for both STDOUT and STDERR.
21 * $output = new ConsoleOutput();
23 * This is equivalent to:
25 * $output = new StreamOutput(fopen('php://stdout', 'w'));
26 * $stdErr = new StreamOutput(fopen('php://stderr', 'w'));
28 * @author Fabien Potencier <fabien@symfony.com>
30 class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
40 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
41 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
42 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
44 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
46 parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
48 $actualDecorated = $this->isDecorated();
49 $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
51 if (null === $decorated) {
52 $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
59 public function setDecorated($decorated)
61 parent::setDecorated($decorated);
62 $this->stderr->setDecorated($decorated);
68 public function setFormatter(OutputFormatterInterface $formatter)
70 parent::setFormatter($formatter);
71 $this->stderr->setFormatter($formatter);
77 public function setVerbosity($level)
79 parent::setVerbosity($level);
80 $this->stderr->setVerbosity($level);
86 public function getErrorOutput()
94 public function setErrorOutput(OutputInterface $error)
96 $this->stderr = $error;
100 * Returns true if current environment supports writing console output to
105 protected function hasStdoutSupport()
107 return false === $this->isRunningOS400();
111 * Returns true if current environment supports writing console output to
116 protected function hasStderrSupport()
118 return false === $this->isRunningOS400();
122 * Checks if current executing environment is IBM iSeries (OS400), which
123 * doesn't properly convert character-encodings between ASCII to EBCDIC.
127 private function isRunningOS400()
130 function_exists('php_uname') ? php_uname('s') : '',
135 return false !== stripos(implode(';', $checks), 'OS400');
141 private function openOutputStream()
143 if (!$this->hasStdoutSupport()) {
144 return fopen('php://output', 'w');
147 return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
153 private function openErrorStream()
155 return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w');