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

Breadcrumb

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

function QuestionHelper::ask

Asks a question to the user.

Return value

mixed The user answer

Throws

RuntimeException If there is no data to read in the input stream

File

vendor/symfony/console/Helper/QuestionHelper.php, line 47

Class

QuestionHelper
The QuestionHelper class provides helpers to interact with the user.

Namespace

Symfony\Component\Console\Helper

Code

public function ask(InputInterface $input, OutputInterface $output, Question $question) : mixed {
    if ($output instanceof ConsoleOutputInterface) {
        $output = $output->getErrorOutput();
    }
    if (!$input->isInteractive()) {
        return $this->getDefaultAnswer($question);
    }
    $inputStream = $input instanceof StreamableInputInterface ? $input->getStream() : null;
    $inputStream ??= STDIN;
    try {
        if (!$question->getValidator()) {
            return $this->doAsk($inputStream, $output, $question);
        }
        $interviewer = fn() => $this->doAsk($inputStream, $output, $question);
        return $this->validateAttempts($interviewer, $output, $question);
    } catch (MissingInputException $exception) {
        $input->setInteractive(false);
        if (null === ($fallbackOutput = $this->getDefaultAnswer($question))) {
            throw $exception;
        }
        return $fallbackOutput;
    }
}

API Navigation

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