-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[Console] Document timeout functionality for QuestionHelper
#21369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -324,6 +324,63 @@ the response to a question should allow multiline answers by passing ``true`` to | |||||
Multiline questions stop reading user input after receiving an end-of-transmission | ||||||
control character (``Ctrl-D`` on Unix systems or ``Ctrl-Z`` on Windows). | ||||||
|
||||||
Setting a Timeout for User Input | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
.. versionadded:: 7.4 | ||||||
|
||||||
The timeout functionality for questions was introduced in Symfony 7.4. | ||||||
|
||||||
You can set a maximum time limit for user input by using the | ||||||
:method:`Symfony\\Component\\Console\\Question\\Question::setTimeout` method. | ||||||
If the user doesn't respond within the specified timeout, a | ||||||
:class:`Symfony\\Component\\Console\\Exception\\MissingInputException` will be thrown:: | ||||||
|
||||||
use Symfony\Component\Console\Exception\MissingInputException; | ||||||
use Symfony\Component\Console\Question\Question; | ||||||
|
||||||
// ... | ||||||
public function __invoke(InputInterface $input, OutputInterface $output): int | ||||||
{ | ||||||
// ... | ||||||
$helper = new QuestionHelper(); | ||||||
|
||||||
$question = new Question('Please enter your answer'); | ||||||
$question->setTimeout(30); // 30 seconds timeout | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
would be much better, but lets wait for @xabbuh and @javiereguiluz, because IIRC we don't use named arguments in the docs, as they are not covered by BC |
||||||
|
||||||
try { | ||||||
$answer = $helper->ask($input, $output, $question); | ||||||
// ... do something with the answer | ||||||
} catch (MissingInputException $exception) { | ||||||
$output->writeln('No input received within timeout period.'); | ||||||
// ... handle timeout | ||||||
} | ||||||
|
||||||
return Command::SUCCESS; | ||||||
} | ||||||
|
||||||
This is particularly useful when you have interactive questions inside database | ||||||
transactions or other time-sensitive operations where hanging indefinitely | ||||||
could cause problems. | ||||||
|
||||||
.. note:: | ||||||
|
||||||
The timeout only applies to interactive input streams. For non-interactive | ||||||
streams (like pipes or files), the timeout setting is ignored and the | ||||||
question behaves normally. | ||||||
|
||||||
You can also use timeouts with other question types such as | ||||||
:class:`Symfony\\Component\\Console\\Question\\ConfirmationQuestion` and | ||||||
:class:`Symfony\\Component\\Console\\Question\\ChoiceQuestion`:: | ||||||
|
||||||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||||||
|
||||||
// ... | ||||||
$question = new ConfirmationQuestion('Do you want to continue?', false); | ||||||
$question->setTimeout(10); // 10 seconds timeout | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
See comment above |
||||||
|
||||||
$continue = $helper->ask($input, $output, $question); | ||||||
|
||||||
Hiding the User's Response | ||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let move this at the end of this section please