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

Breadcrumb

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

class StreamOutput

StreamOutput writes the output to a given stream.

Usage:

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

As `StreamOutput` can use any stream, you can also use a file:

$output = new StreamOutput(fopen('/path/to/output.log', 'a', false));

@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

Expanded class hierarchy of StreamOutput

2 files declare their use of StreamOutput
BufferIO.php in vendor/composer/composer/src/Composer/IO/BufferIO.php
TesterTrait.php in vendor/symfony/console/Tester/TesterTrait.php

File

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

Namespace

Symfony\Component\Console\Output
View source
class StreamOutput extends Output {
    
    /** @var resource */
    private $stream;
    
    /**
     * @param resource                      $stream    A stream resource
     * @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)
     *
     * @throws InvalidArgumentException When first argument is not a real stream
     */
    public function __construct($stream, int $verbosity = self::VERBOSITY_NORMAL, ?bool $decorated = null, ?OutputFormatterInterface $formatter = null) {
        if (!\is_resource($stream) || 'stream' !== get_resource_type($stream)) {
            throw new InvalidArgumentException('The StreamOutput class needs a stream as its first argument.');
        }
        $this->stream = $stream;
        $decorated ??= $this->hasColorSupport();
        parent::__construct($verbosity, $decorated, $formatter);
    }
    
    /**
     * Gets the stream attached to this StreamOutput instance.
     *
     * @return resource
     */
    public function getStream() {
        return $this->stream;
    }
    protected function doWrite(string $message, bool $newline) : void {
        if ($newline) {
            $message .= \PHP_EOL;
        }
        @fwrite($this->stream, $message);
        fflush($this->stream);
    }
    
    /**
     * Returns true if the stream supports colorization.
     *
     * Colorization is disabled if not supported by the stream:
     *
     * This is tricky on Windows, because Cygwin, Msys2 etc emulate pseudo
     * terminals via named pipes, so we can only check the environment.
     *
     * Reference: Composer\XdebugHandler\Process::supportsColor
     * https://github.com/composer/xdebug-handler
     *
     * @return bool true if the stream supports colorization, false otherwise
     */
    protected function hasColorSupport() : bool {
        // Follow https://no-color.org/
        if ('' !== (($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR'))[0] ?? '')) {
            return false;
        }
        // Follow https://force-color.org/
        if ('' !== (($_SERVER['FORCE_COLOR'] ?? getenv('FORCE_COLOR'))[0] ?? '')) {
            return true;
        }
        // Detect msysgit/mingw and assume this is a tty because detection
        // does not work correctly, see https://github.com/composer/composer/issues/9690
        if (!@stream_isatty($this->stream) && !\in_array(strtoupper((string) getenv('MSYSTEM')), [
            'MINGW32',
            'MINGW64',
        ], true)) {
            return false;
        }
        if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support($this->stream)) {
            return true;
        }
        if ('Hyper' === getenv('TERM_PROGRAM') || false !== getenv('COLORTERM') || false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI')) {
            return true;
        }
        if ('dumb' === ($term = (string) getenv('TERM'))) {
            return false;
        }
        // See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
        return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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::setDecorated public function Sets the decorated flag. Overrides OutputInterface::setDecorated 1
Output::setFormatter public function Overrides OutputInterface::setFormatter 1
Output::setVerbosity public function Sets the verbosity of the output. Overrides OutputInterface::setVerbosity 1
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.
StreamOutput::__construct public function Overrides Output::__construct 2

API Navigation

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