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\HelperCode
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;
}
}