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

Breadcrumb

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

class ConsoleIO

The Input/Output helper.

@author François Pluchino <francois.pluchino@opendisplay.com> @author Jordi Boggiano <j.boggiano@seld.be>

Hierarchy

  • class \Composer\IO\BaseIO implements \Composer\IO\IOInterface
    • class \Composer\IO\ConsoleIO extends \Composer\IO\BaseIO

Expanded class hierarchy of ConsoleIO

4 files declare their use of ConsoleIO
Application.php in vendor/composer/composer/src/Composer/Console/Application.php
Auditor.php in vendor/composer/composer/src/Composer/Advisory/Auditor.php
EventDispatcher.php in vendor/composer/composer/src/Composer/EventDispatcher/EventDispatcher.php
InstallationManager.php in vendor/composer/composer/src/Composer/Installer/InstallationManager.php

File

vendor/composer/composer/src/Composer/IO/ConsoleIO.php, line 31

Namespace

Composer\IO
View source
class ConsoleIO extends BaseIO {
    
    /** @var InputInterface */
    protected $input;
    
    /** @var OutputInterface */
    protected $output;
    
    /** @var HelperSet */
    protected $helperSet;
    
    /** @var string */
    protected $lastMessage = '';
    
    /** @var string */
    protected $lastMessageErr = '';
    
    /** @var float */
    private $startTime;
    
    /** @var array<IOInterface::*, OutputInterface::VERBOSITY_*> */
    private $verbosityMap;
    
    /**
     * Constructor.
     *
     * @param InputInterface  $input     The input instance
     * @param OutputInterface $output    The output instance
     * @param HelperSet       $helperSet The helperSet instance
     */
    public function __construct(InputInterface $input, OutputInterface $output, HelperSet $helperSet) {
        $this->input = $input;
        $this->output = $output;
        $this->helperSet = $helperSet;
        $this->verbosityMap = [
            self::QUIET => OutputInterface::VERBOSITY_QUIET,
            self::NORMAL => OutputInterface::VERBOSITY_NORMAL,
            self::VERBOSE => OutputInterface::VERBOSITY_VERBOSE,
            self::VERY_VERBOSE => OutputInterface::VERBOSITY_VERY_VERBOSE,
            self::DEBUG => OutputInterface::VERBOSITY_DEBUG,
        ];
    }
    
    /**
     * @return void
     */
    public function enableDebugging(float $startTime) {
        $this->startTime = $startTime;
    }
    
    /**
     * @inheritDoc
     */
    public function isInteractive() {
        return $this->input
            ->isInteractive();
    }
    
    /**
     * @inheritDoc
     */
    public function isDecorated() {
        return $this->output
            ->isDecorated();
    }
    
    /**
     * @inheritDoc
     */
    public function isVerbose() {
        return $this->output
            ->isVerbose();
    }
    
    /**
     * @inheritDoc
     */
    public function isVeryVerbose() {
        return $this->output
            ->isVeryVerbose();
    }
    
    /**
     * @inheritDoc
     */
    public function isDebug() {
        return $this->output
            ->isDebug();
    }
    
    /**
     * @inheritDoc
     */
    public function write($messages, bool $newline = true, int $verbosity = self::NORMAL) {
        $this->doWrite($messages, $newline, false, $verbosity);
    }
    
    /**
     * @inheritDoc
     */
    public function writeError($messages, bool $newline = true, int $verbosity = self::NORMAL) {
        $this->doWrite($messages, $newline, true, $verbosity);
    }
    
    /**
     * @inheritDoc
     */
    public function writeRaw($messages, bool $newline = true, int $verbosity = self::NORMAL) {
        $this->doWrite($messages, $newline, false, $verbosity, true);
    }
    
    /**
     * @inheritDoc
     */
    public function writeErrorRaw($messages, bool $newline = true, int $verbosity = self::NORMAL) {
        $this->doWrite($messages, $newline, true, $verbosity, true);
    }
    
    /**
     * @param string[]|string $messages
     */
    private function doWrite($messages, bool $newline, bool $stderr, int $verbosity, bool $raw = false) : void {
        $sfVerbosity = $this->verbosityMap[$verbosity];
        if ($sfVerbosity > $this->output
            ->getVerbosity()) {
            return;
        }
        if ($raw) {
            $sfVerbosity |= OutputInterface::OUTPUT_RAW;
        }
        if (null !== $this->startTime) {
            $memoryUsage = memory_get_usage() / 1024 / 1024;
            $timeSpent = microtime(true) - $this->startTime;
            $messages = array_map(static function ($message) use ($memoryUsage, $timeSpent) : string {
                return sprintf('[%.1fMiB/%.2fs] %s', $memoryUsage, $timeSpent, $message);
            }, (array) $messages);
        }
        if (true === $stderr && $this->output instanceof ConsoleOutputInterface) {
            $this->output
                ->getErrorOutput()
                ->write($messages, $newline, $sfVerbosity);
            $this->lastMessageErr = implode($newline ? "\n" : '', (array) $messages);
            return;
        }
        $this->output
            ->write($messages, $newline, $sfVerbosity);
        $this->lastMessage = implode($newline ? "\n" : '', (array) $messages);
    }
    
    /**
     * @inheritDoc
     */
    public function overwrite($messages, bool $newline = true, ?int $size = null, int $verbosity = self::NORMAL) {
        $this->doOverwrite($messages, $newline, $size, false, $verbosity);
    }
    
    /**
     * @inheritDoc
     */
    public function overwriteError($messages, bool $newline = true, ?int $size = null, int $verbosity = self::NORMAL) {
        $this->doOverwrite($messages, $newline, $size, true, $verbosity);
    }
    
    /**
     * @param string[]|string $messages
     */
    private function doOverwrite($messages, bool $newline, ?int $size, bool $stderr, int $verbosity) : void {
        // messages can be an array, let's convert it to string anyway
        $messages = implode($newline ? "\n" : '', (array) $messages);
        // since overwrite is supposed to overwrite last message...
        if (!isset($size)) {
            // removing possible formatting of lastMessage with strip_tags
            $size = strlen(strip_tags($stderr ? $this->lastMessageErr : $this->lastMessage));
        }
        // ...let's fill its length with backspaces
        $this->doWrite(str_repeat("\x08", $size), false, $stderr, $verbosity);
        // write the new message
        $this->doWrite($messages, false, $stderr, $verbosity);
        // In cmd.exe on Win8.1 (possibly 10?), the line can not be cleared, so we need to
        // track the length of previous output and fill it with spaces to make sure the line is cleared.
        // See https://github.com/composer/composer/pull/5836 for more details
        $fill = $size - strlen(strip_tags($messages));
        if ($fill > 0) {
            // whitespace whatever has left
            $this->doWrite(str_repeat(' ', $fill), false, $stderr, $verbosity);
            // move the cursor back
            $this->doWrite(str_repeat("\x08", $fill), false, $stderr, $verbosity);
        }
        if ($newline) {
            $this->doWrite('', true, $stderr, $verbosity);
        }
        if ($stderr) {
            $this->lastMessageErr = $messages;
        }
        else {
            $this->lastMessage = $messages;
        }
    }
    
    /**
     * @return ProgressBar
     */
    public function getProgressBar(int $max = 0) {
        return new ProgressBar($this->getErrorOutput(), $max);
    }
    
    /**
     * @inheritDoc
     */
    public function ask($question, $default = null) {
        
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
        $helper = $this->helperSet
            ->get('question');
        $question = new Question($question, $default);
        return $helper->ask($this->input, $this->getErrorOutput(), $question);
    }
    
    /**
     * @inheritDoc
     */
    public function askConfirmation($question, $default = true) {
        
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
        $helper = $this->helperSet
            ->get('question');
        $question = new StrictConfirmationQuestion($question, $default);
        return $helper->ask($this->input, $this->getErrorOutput(), $question);
    }
    
    /**
     * @inheritDoc
     */
    public function askAndValidate($question, $validator, $attempts = null, $default = null) {
        
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
        $helper = $this->helperSet
            ->get('question');
        $question = new Question($question, $default);
        $question->setValidator($validator);
        $question->setMaxAttempts($attempts);
        return $helper->ask($this->input, $this->getErrorOutput(), $question);
    }
    
    /**
     * @inheritDoc
     */
    public function askAndHideAnswer($question) {
        
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
        $helper = $this->helperSet
            ->get('question');
        $question = new Question($question);
        $question->setHidden(true);
        return $helper->ask($this->input, $this->getErrorOutput(), $question);
    }
    
    /**
     * @inheritDoc
     */
    public function select($question, $choices, $default, $attempts = false, $errorMessage = 'Value "%s" is invalid', $multiselect = false) {
        
        /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
        $helper = $this->helperSet
            ->get('question');
        $question = new ChoiceQuestion($question, $choices, $default);
        $question->setMaxAttempts($attempts ?: null);
        // IOInterface requires false, and Question requires null or int
        $question->setErrorMessage($errorMessage);
        $question->setMultiselect($multiselect);
        $result = $helper->ask($this->input, $this->getErrorOutput(), $question);
        $isAssoc = (bool) \count(array_filter(array_keys($choices), 'is_string'));
        if ($isAssoc) {
            return $result;
        }
        if (!is_array($result)) {
            return (string) array_search($result, $choices, true);
        }
        $results = [];
        foreach ($choices as $index => $choice) {
            if (in_array($choice, $result, true)) {
                $results[] = (string) $index;
            }
        }
        return $results;
    }
    public function getTable() : Table {
        return new Table($this->output);
    }
    private function getErrorOutput() : OutputInterface {
        if ($this->output instanceof ConsoleOutputInterface) {
            return $this->output
                ->getErrorOutput();
        }
        return $this->output;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
BaseIO::$authentications protected property @var array&lt;string, array{username: string|null, password: string|null}&gt;
BaseIO::alert public function Overrides LoggerInterface::alert
BaseIO::checkAndSetAuthentication protected function Check for overwrite and set the authentication information for the repository.
BaseIO::critical public function Overrides LoggerInterface::critical
BaseIO::debug public function Overrides LoggerInterface::debug
BaseIO::emergency public function Overrides LoggerInterface::emergency
BaseIO::error public function Overrides LoggerInterface::error
BaseIO::getAuthentication public function @inheritDoc Overrides IOInterface::getAuthentication
BaseIO::getAuthentications public function @inheritDoc Overrides IOInterface::getAuthentications
BaseIO::hasAuthentication public function @inheritDoc Overrides IOInterface::hasAuthentication
BaseIO::info public function Overrides LoggerInterface::info
BaseIO::loadConfiguration public function @inheritDoc Overrides IOInterface::loadConfiguration
BaseIO::log public function Overrides LoggerInterface::log
BaseIO::notice public function Overrides LoggerInterface::notice
BaseIO::resetAuthentications public function
BaseIO::setAuthentication public function @inheritDoc Overrides IOInterface::setAuthentication
BaseIO::warning public function Overrides LoggerInterface::warning
ConsoleIO::$helperSet protected property @var HelperSet
ConsoleIO::$input protected property @var InputInterface
ConsoleIO::$lastMessage protected property @var string
ConsoleIO::$lastMessageErr protected property @var string
ConsoleIO::$output protected property @var OutputInterface
ConsoleIO::$startTime private property @var float
ConsoleIO::$verbosityMap private property @var array&lt;IOInterface::*, OutputInterface::VERBOSITY_*&gt;
ConsoleIO::ask public function @inheritDoc Overrides IOInterface::ask
ConsoleIO::askAndHideAnswer public function @inheritDoc Overrides IOInterface::askAndHideAnswer
ConsoleIO::askAndValidate public function @inheritDoc Overrides IOInterface::askAndValidate
ConsoleIO::askConfirmation public function @inheritDoc Overrides IOInterface::askConfirmation
ConsoleIO::doOverwrite private function
ConsoleIO::doWrite private function
ConsoleIO::enableDebugging public function
ConsoleIO::getErrorOutput private function
ConsoleIO::getProgressBar public function
ConsoleIO::getTable public function
ConsoleIO::isDebug public function @inheritDoc Overrides IOInterface::isDebug
ConsoleIO::isDecorated public function @inheritDoc Overrides IOInterface::isDecorated
ConsoleIO::isInteractive public function @inheritDoc Overrides IOInterface::isInteractive
ConsoleIO::isVerbose public function @inheritDoc Overrides IOInterface::isVerbose
ConsoleIO::isVeryVerbose public function @inheritDoc Overrides IOInterface::isVeryVerbose
ConsoleIO::overwrite public function @inheritDoc Overrides IOInterface::overwrite
ConsoleIO::overwriteError public function @inheritDoc Overrides IOInterface::overwriteError
ConsoleIO::select public function @inheritDoc Overrides IOInterface::select
ConsoleIO::write public function @inheritDoc Overrides IOInterface::write
ConsoleIO::writeError public function @inheritDoc Overrides IOInterface::writeError
ConsoleIO::writeErrorRaw public function @inheritDoc Overrides BaseIO::writeErrorRaw
ConsoleIO::writeRaw public function @inheritDoc Overrides BaseIO::writeRaw
ConsoleIO::__construct public function Constructor. 1
IOInterface::DEBUG public constant
IOInterface::NORMAL public constant
IOInterface::QUIET public constant
IOInterface::VERBOSE public constant
IOInterface::VERY_VERBOSE public constant

API Navigation

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