Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ConsoleOutput.php

class ConsoleOutput

ConsoleOutput is the default class for all CLI output. It uses STDOUT and STDERR.

This class is a convenient wrapper around `StreamOutput` for both STDOUT and STDERR.

$output = new ConsoleOutput();

This is equivalent to:

$output = new StreamOutput(fopen('php://stdout', 'w')); $stdErr = new StreamOutput(fopen('php://stderr', 'w'));

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\Console\Output\Output implements \Symfony\Component\Console\Output\OutputInterface
    • class \Symfony\Component\Console\Output\StreamOutput extends \Symfony\Component\Console\Output\Output
      • class \Symfony\Component\Console\Output\ConsoleOutput extends \Symfony\Component\Console\Output\StreamOutput implements \Symfony\Component\Console\Output\ConsoleOutputInterface

Expanded class hierarchy of ConsoleOutput

4 files declare their use of ConsoleOutput
Application.php in vendor/symfony/console/Application.php
EventDispatcher.php in vendor/composer/composer/src/Composer/EventDispatcher/EventDispatcher.php
Factory.php in vendor/composer/composer/src/Composer/Factory.php
TesterTrait.php in vendor/symfony/console/Tester/TesterTrait.php

File

vendor/symfony/console/Output/ConsoleOutput.php, line 30

Namespace

Symfony\Component\Console\Output
View source
class ConsoleOutput extends StreamOutput implements ConsoleOutputInterface {
    private OutputInterface $stderr;
    private array $consoleSectionOutputs = [];
    
    /**
     * @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)
     */
    public function __construct(int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null) {
        parent::__construct($this->openOutputStream(), $verbosity, $decorated, $formatter);
        if (null === $formatter) {
            // for BC reasons, stdErr has it own Formatter only when user don't inject a specific formatter.
            $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated);
            return;
        }
        $actualDecorated = $this->isDecorated();
        $this->stderr = new StreamOutput($this->openErrorStream(), $verbosity, $decorated, $this->getFormatter());
        if (null === $decorated) {
            $this->setDecorated($actualDecorated && $this->stderr
                ->isDecorated());
        }
    }
    
    /**
     * Creates a new output section.
     */
    public function section() : ConsoleSectionOutput {
        return new ConsoleSectionOutput($this->getStream(), $this->consoleSectionOutputs, $this->getVerbosity(), $this->isDecorated(), $this->getFormatter());
    }
    public function setDecorated(bool $decorated) : void {
        parent::setDecorated($decorated);
        $this->stderr
            ->setDecorated($decorated);
    }
    public function setFormatter(OutputFormatterInterface $formatter) : void {
        parent::setFormatter($formatter);
        $this->stderr
            ->setFormatter($formatter);
    }
    public function setVerbosity(int $level) : void {
        parent::setVerbosity($level);
        $this->stderr
            ->setVerbosity($level);
    }
    public function getErrorOutput() : OutputInterface {
        return $this->stderr;
    }
    public function setErrorOutput(OutputInterface $error) : void {
        $this->stderr = $error;
    }
    
    /**
     * Returns true if current environment supports writing console output to
     * STDOUT.
     */
    protected function hasStdoutSupport() : bool {
        return false === $this->isRunningOS400();
    }
    
    /**
     * Returns true if current environment supports writing console output to
     * STDERR.
     */
    protected function hasStderrSupport() : bool {
        return false === $this->isRunningOS400();
    }
    
    /**
     * Checks if current executing environment is IBM iSeries (OS400), which
     * doesn't properly convert character-encodings between ASCII to EBCDIC.
     */
    private function isRunningOS400() : bool {
        $checks = [
            \function_exists('php_uname') ? php_uname('s') : '',
            getenv('OSTYPE'),
            \PHP_OS,
        ];
        return false !== stripos(implode(';', $checks), 'OS400');
    }
    
    /**
     * @return resource
     */
    private function openOutputStream() {
        if (!$this->hasStdoutSupport()) {
            return fopen('php://output', 'w');
        }
        // Use STDOUT when possible to prevent from opening too many file descriptors
        return \defined('STDOUT') ? \STDOUT : (@fopen('php://stdout', 'w') ?: fopen('php://output', 'w'));
    }
    
    /**
     * @return resource
     */
    private function openErrorStream() {
        if (!$this->hasStderrSupport()) {
            return fopen('php://output', 'w');
        }
        // Use STDERR when possible to prevent from opening too many file descriptors
        return \defined('STDERR') ? \STDERR : (@fopen('php://stderr', 'w') ?: fopen('php://output', 'w'));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ConsoleOutput::$consoleSectionOutputs private property
ConsoleOutput::$stderr private property
ConsoleOutput::getErrorOutput public function Gets the OutputInterface for errors. Overrides ConsoleOutputInterface::getErrorOutput
ConsoleOutput::hasStderrSupport protected function Returns true if current environment supports writing console output to
STDERR.
ConsoleOutput::hasStdoutSupport protected function Returns true if current environment supports writing console output to
STDOUT.
ConsoleOutput::isRunningOS400 private function Checks if current executing environment is IBM iSeries (OS400), which
doesn&#039;t properly convert character-encodings between ASCII to EBCDIC.
ConsoleOutput::openErrorStream private function
ConsoleOutput::openOutputStream private function
ConsoleOutput::section public function Creates a new output section. Overrides ConsoleOutputInterface::section
ConsoleOutput::setDecorated public function Sets the decorated flag. Overrides Output::setDecorated
ConsoleOutput::setErrorOutput public function Overrides ConsoleOutputInterface::setErrorOutput
ConsoleOutput::setFormatter public function Overrides Output::setFormatter
ConsoleOutput::setVerbosity public function Sets the verbosity of the output. Overrides Output::setVerbosity
ConsoleOutput::__construct public function Overrides StreamOutput::__construct
Output::$formatter private property
Output::$verbosity private property
Output::getFormatter public function Returns current output formatter instance. Overrides OutputInterface::getFormatter
Output::getVerbosity public function Gets the current verbosity of the output. Overrides OutputInterface::getVerbosity
Output::isDebug public function Returns whether verbosity is debug (-vvv). Overrides OutputInterface::isDebug
Output::isDecorated public function Gets the decorated flag. Overrides OutputInterface::isDecorated
Output::isQuiet public function Returns whether verbosity is quiet (-q). Overrides OutputInterface::isQuiet
Output::isSilent public function
Output::isVerbose public function Returns whether verbosity is verbose (-v). Overrides OutputInterface::isVerbose
Output::isVeryVerbose public function Returns whether verbosity is very verbose (-vv). Overrides OutputInterface::isVeryVerbose
Output::write public function Writes a message to the output. Overrides OutputInterface::write
Output::writeln public function Writes a message to the output and adds a newline at the end. Overrides OutputInterface::writeln
OutputInterface::OUTPUT_NORMAL public constant
OutputInterface::OUTPUT_PLAIN public constant
OutputInterface::OUTPUT_RAW public constant
OutputInterface::VERBOSITY_DEBUG public constant
OutputInterface::VERBOSITY_NORMAL public constant
OutputInterface::VERBOSITY_QUIET public constant
OutputInterface::VERBOSITY_SILENT public constant
OutputInterface::VERBOSITY_VERBOSE public constant
OutputInterface::VERBOSITY_VERY_VERBOSE public constant
StreamOutput::$stream private property @var resource
StreamOutput::doWrite protected function Writes a message to the output. Overrides Output::doWrite 1
StreamOutput::getStream public function Gets the stream attached to this StreamOutput instance.
StreamOutput::hasColorSupport protected function Returns true if the stream supports colorization.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal