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.
19 * This class is a convenient wrapper around `StreamOutput`.
21 * $output = new ConsoleOutput();
23 * This is equivalent to:
25 * $output = new StreamOutput(fopen('php://stdout', 'w'));
27 * @author Fabien Potencier <fabien@symfony.com>
29 class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
39 * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
40 * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
41 * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
43 public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
45 parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
47 $actualDecorated = $this->isDecorated();
48 $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
50 if (null === $decorated) {
51 $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
58 public function setDecorated($decorated)
60 parent::setDecorated($decorated);
61 $this->stderr->setDecorated($decorated);
67 public function setFormatter(OutputFormatterInterface $formatter)
69 parent::setFormatter($formatter);
70 $this->stderr->setFormatter($formatter);
76 public function setVerbosity($level)
78 parent::setVerbosity($level);
79 $this->stderr->setVerbosity($level);
85 public function getErrorOutput()
93 public function setErrorOutput(OutputInterface $error)
95 $this->stderr = $error;
99 * Returns true if current environment supports writing console output to
104 protected function hasStdoutSupport()
106 return false === $this->isRunningOS400();
110 * Returns true if current environment supports writing console output to
115 protected function hasStderrSupport()
117 return false === $this->isRunningOS400();
121 * Checks if current executing environment is IBM iSeries (OS400), which
122 * doesn't properly convert character-encodings between ASCII to EBCDIC.
126 private function isRunningOS400()
129 function_exists('php_uname') ? php_uname('s') : '',
134 return false !== stripos(implode(';', $checks), 'OS400');
140 private function openOutputStream()
142 $outputStream = $this->hasStdoutSupport() ? 'php://stdout' : 'php://output';
144 return @fopen($outputStream, 'w') ?: fopen('php://output', 'w');
150 private function openErrorStream()
152 $errorStream = $this->hasStderrSupport() ? 'php://stderr' : 'php://output';
154 return fopen($errorStream, 'w');