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
35 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
36 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
37 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
39 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
41 parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
43 $actualDecorated = $this->isDecorated();
44 $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
46 if (null === $decorated) {
47 $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
54 public function setDecorated($decorated)
56 parent::setDecorated($decorated);
57 $this->stderr->setDecorated($decorated);
63 public function setFormatter(OutputFormatterInterface $formatter)
65 parent::setFormatter($formatter);
66 $this->stderr->setFormatter($formatter);
72 public function setVerbosity($level)
74 parent::setVerbosity($level);
75 $this->stderr->setVerbosity($level);
81 public function getErrorOutput()
89 public function setErrorOutput(OutputInterface $error)
91 $this->stderr = $error;
95 * Returns true if current environment supports writing console output to
100 protected function hasStdoutSupport()
102 return false === $this->isRunningOS400();
106 * Returns true if current environment supports writing console output to
111 protected function hasStderrSupport()
113 return false === $this->isRunningOS400();
117 * Checks if current executing environment is IBM iSeries (OS400), which
118 * doesn't properly convert character-encodings between ASCII to EBCDIC.
122 private function isRunningOS400()
125 function_exists('php_uname') ? php_uname('s') : '',
130 return false !== stripos(implode(';', $checks), 'OS400');
136 private function openOutputStream()
138 if (!$this->hasStdoutSupport()) {
139 return fopen('php://output', 'w');
142 return @fopen('php://stdout', 'w') ?: fopen('php://output', 'w');
148 private function openErrorStream()
150 return fopen($this->hasStderrSupport() ? 'php://stderr' : 'php://output', 'w');