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

Breadcrumb

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

class SymfonyQuestionHelper

Symfony Style Guide compliant question helper.

@author Kevin Bond <kevinbond@gmail.com>

Hierarchy

  • class \Symfony\Component\Console\Helper\Helper implements \Symfony\Component\Console\Helper\HelperInterface
    • class \Symfony\Component\Console\Helper\QuestionHelper extends \Symfony\Component\Console\Helper\Helper
      • class \Symfony\Component\Console\Helper\SymfonyQuestionHelper extends \Symfony\Component\Console\Helper\QuestionHelper

Expanded class hierarchy of SymfonyQuestionHelper

1 file declares its use of SymfonyQuestionHelper
SymfonyStyle.php in vendor/symfony/console/Style/SymfonyStyle.php

File

vendor/symfony/console/Helper/SymfonyQuestionHelper.php, line 26

Namespace

Symfony\Component\Console\Helper
View source
class SymfonyQuestionHelper extends QuestionHelper {
    protected function writePrompt(OutputInterface $output, Question $question) : void {
        $text = OutputFormatter::escapeTrailingBackslash($question->getQuestion());
        $default = $question->getDefault();
        if ($question->isMultiline()) {
            $text .= \sprintf(' (press %s to continue)', $this->getEofShortcut());
        }
        switch (true) {
            case null === $default:
                $text = \sprintf(' <info>%s</info>:', $text);
                break;
            case $question instanceof ConfirmationQuestion:
                $text = \sprintf(' <info>%s (yes/no)</info> [<comment>%s</comment>]:', $text, $default ? 'yes' : 'no');
                break;
            case $question instanceof ChoiceQuestion && $question->isMultiselect():
                $choices = $question->getChoices();
                $default = explode(',', $default);
                foreach ($default as $key => $value) {
                    $default[$key] = $choices[trim($value)];
                }
                $text = \sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape(implode(', ', $default)));
                break;
            case $question instanceof ChoiceQuestion:
                $choices = $question->getChoices();
                $text = \sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($choices[$default] ?? $default));
                break;
            default:
                $text = \sprintf(' <info>%s</info> [<comment>%s</comment>]:', $text, OutputFormatter::escape($default));
        }
        $output->writeln($text);
        $prompt = ' > ';
        if ($question instanceof ChoiceQuestion) {
            $output->writeln($this->formatChoiceQuestionChoices($question, 'comment'));
            $prompt = $question->getPrompt();
        }
        $output->write($prompt);
    }
    protected function writeError(OutputInterface $output, \Exception $error) : void {
        if ($output instanceof SymfonyStyle) {
            $output->newLine();
            $output->error($error->getMessage());
            return;
        }
        parent::writeError($output, $error);
    }
    private function getEofShortcut() : string {
        if ('Windows' === \PHP_OS_FAMILY) {
            return '<comment>Ctrl+Z</comment> then <comment>Enter</comment>';
        }
        return '<comment>Ctrl+D</comment>';
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
Helper::$helperSet protected property
Helper::formatMemory public static function
Helper::formatTime public static function
Helper::getHelperSet public function Gets the helper set associated with this helper. Overrides HelperInterface::getHelperSet
Helper::length public static function Returns the length of a string, using mb_strlen if it is available.
The length is related to how many bytes the string will use.
Helper::removeDecoration public static function
Helper::setHelperSet public function Sets the helper set associated with this helper. Overrides HelperInterface::setHelperSet
Helper::substr public static function Returns the subset of a string, using mb_substr if it is available.
Helper::width public static function Returns the width of a string, using mb_strwidth if it is available.
The width is how many characters positions the string will use.
QuestionHelper::$stdinIsInteractive private static property
QuestionHelper::$stty private static property
QuestionHelper::ask public function Asks a question to the user.
QuestionHelper::autocomplete private function Autocompletes a question.
QuestionHelper::cloneInputStream private function Clones an input stream in order to act on one instance of the same
stream without affecting the other instance.
QuestionHelper::disableStty public static function Prevents usage of stty.
QuestionHelper::doAsk private function Asks the question to the user.
QuestionHelper::formatChoiceQuestionChoices protected function
QuestionHelper::getDefaultAnswer private function
QuestionHelper::getHiddenResponse private function Gets a hidden response from user.
QuestionHelper::getName public function Returns the canonical name of this helper. Overrides HelperInterface::getName
QuestionHelper::isInteractiveInput private function
QuestionHelper::mostRecentlyEnteredValue private function
QuestionHelper::readInput private function Reads one or more lines of input and returns what is read.
QuestionHelper::resetIOCodepage private function Sets console I/O to the specified code page and converts the user input.
QuestionHelper::setIOCodepage private function
QuestionHelper::validateAttempts private function Validates an attempt.
SymfonyQuestionHelper::getEofShortcut private function
SymfonyQuestionHelper::writeError protected function Outputs an error message. Overrides QuestionHelper::writeError
SymfonyQuestionHelper::writePrompt protected function Outputs the question prompt. Overrides QuestionHelper::writePrompt

API Navigation

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