123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Console\Output;
- use Symfony\Component\Console\Formatter\OutputFormatterInterface;
- /**
- * ConsoleOutput is the default class for all CLI output. It uses STDOUT.
- *
- * This class is a convenient wrapper around `StreamOutput`.
- *
- * $output = new ConsoleOutput();
- *
- * This is equivalent to:
- *
- * $output = new StreamOutput(fopen('php://stdout', 'w'));
- *
- * @author Fabien Potencier <fabien@symfony.com>
- *
- * @api
- */
- class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface
- {
- /**
- * @var StreamOutput
- */
- private $stderr;
- /**
- * Constructor.
- *
- * @param int $verbosity The verbosity level (one of the VERBOSITY constants in OutputInterface)
- * @param bool|null $decorated Whether to decorate messages (null for auto-guessing)
- * @param OutputFormatterInterface|null $formatter Output formatter instance (null to use default OutputFormatter)
- *
- * @api
- */
- public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = null, OutputFormatterInterface $formatter = null)
- {
- parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
- $actualDecorated = $this->isDecorated();
- $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
- if (null === $decorated) {
- $this->setDecorated($actualDecorated && $this->stderr->isDecorated());
- }
- }
- /**
- * {@inheritdoc}
- */
- public function setDecorated($decorated)
- {
- parent::setDecorated($decorated);
- $this->stderr->setDecorated($decorated);
- }
- /**
- * {@inheritdoc}
- */
- public function setFormatter(OutputFormatterInterface $formatter)
- {
- parent::setFormatter($formatter);
- $this->stderr->setFormatter($formatter);
- }
- /**
- * {@inheritdoc}
- */
- public function setVerbosity($level)
- {
- parent::setVerbosity($level);
- $this->stderr->setVerbosity($level);
- }
- /**
- * {@inheritdoc}
- */
- public function getErrorOutput()
- {
- return $this->stderr;
- }
- /**
- * {@inheritdoc}
- */
- public function setErrorOutput(OutputInterface $error)
- {
- $this->stderr = $error;
- }
- /**
- * Returns true if current environment supports writing console output to
- * STDOUT.
- *
- * @return bool
- */
- protected function hasStdoutSupport()
- {
- return false === $this->isRunningOS400();
- }
- /**
- * Returns true if current environment supports writing console output to
- * STDERR.
- *
- * @return bool
- */
- protected function hasStderrSupport()
- {
- return false === $this->isRunningOS400();
- }
- /**
- * Checks if current executing environment is IBM iSeries (OS400), which
- * doesn't properly convert character-encodings between ASCII to EBCDIC.
- *
- * @return bool
- */
- private function isRunningOS400()
- {
- return 'OS400' === php_uname('s');
- }
- /**
- * @return resource
- */
- private function openOutputStream()
- {
- $outputStream = $this->hasStdoutSupport() ? 'php://stdout' : 'php://output';
- return @fopen($outputStream, 'w') ?: fopen('php://output', 'w');
- }
- /**
- * @return resource
- */
- private function openErrorStream()
- {
- $errorStream = $this->hasStderrSupport() ? 'php://stderr' : 'php://output';
- return fopen($errorStream, 'w');
- }
- }
|