Skip to content

[Console][QuestionHelper] add optional timeout for human interaction #61092

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

Open
wants to merge 10 commits into
base: 7.4
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add `BackedEnum` support with `#[Argument]` and `#[Option]` inputs in invokable commands
* Allow Usages to be specified via `#[AsCommand]` attribute.
* Allow passing invokable commands to `Symfony\Component\Console\Tester\CommandTester`
* Add optional timeout for interaction in `QuestionHelper`

7.3
---
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,19 @@ private function isInteractiveInput($inputStream): bool
*/
private function readInput($inputStream, Question $question): string|false
{
if (null !== $question->getTimeoutSeconds() && $this->isInteractiveInput($inputStream)) {
$read = [$inputStream];
$write = null;
$except = null;
$timeoutSeconds = $question->getTimeoutSeconds();
$changedStreams = stream_select($read, $write, $except, $timeoutSeconds);

if (0 === $changedStreams) {
$plural = 1 === $timeoutSeconds ? '' : 's';
throw new MissingInputException("Timed out after waiting for input for $timeoutSeconds second$plural.");
Comment on lines +513 to +514
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$plural = 1 === $timeoutSeconds ? '' : 's';
throw new MissingInputException("Timed out after waiting for input for $timeoutSeconds second$plural.");
throw new MissingInputException(\sprintf('Timed out after waiting for input for %d %s.', $timeoutSeconds, $timeoutSeconds === 1 ? 'second' : 'seconds'));

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks equal to me, is there any Symfony cs rule that states sprintf is prefered? If so, I can change it.

}
}

if (!$question->isMultiline()) {
$cp = $this->setIOCodepage();
$ret = fgets($inputStream, 4096);
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/Console/Question/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Question
private ?\Closure $normalizer = null;
private bool $trimmable = true;
private bool $multiline = false;
private ?int $timeoutSeconds = null;

/**
* @param string $question The question to ask to the user
Expand Down Expand Up @@ -85,6 +86,27 @@ public function setMultiline(bool $multiline): static
return $this;
}

/**
* Returns the timeout in seconds.
*/
public function getTimeoutSeconds(): ?int
{
return $this->timeoutSeconds;
}

/**
* The timeout is the maximum time the user has to answer the question.
* If the user does not answer within this time, an exception will be thrown.
*
* @return $this
*/
public function setTimeout(?int $seconds): static
{
$this->timeoutSeconds = $seconds;

return $this;
}

/**
* Returns whether the user response must be hidden.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,45 @@ public function testAskNonTrimmed()
$this->assertEquals('What time is it?', stream_get_contents($output->getStream()));
}

public function testAskTimeout()
{
$dialog = new QuestionHelper();

$question = new Question('What is your name?');
$question->setTimeout(1);

$this->expectException(MissingInputException::class);
$this->expectExceptionMessage('Timed out after waiting for input for 1 second.');

try {
$startTime = microtime(true);
$dialog->ask($this->createStreamableInputInterfaceMock(\STDIN), $this->createOutputInterface(), $question);
} finally {
$elapsedTime = microtime(true) - $startTime;
self::assertGreaterThanOrEqual(1, $elapsedTime, 'The question should timeout after 1 second');
}
}

public function testAskTimeoutWithIncompatibleStream()
{
$dialog = new QuestionHelper();
$inputStream = $this->getInputStream('');

$question = new Question('What is your name?');
$question->setTimeout(1);

$this->expectException(MissingInputException::class);
$this->expectExceptionMessage('Aborted.');

try {
$startTime = microtime(true);
$dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question);
} finally {
$elapsedTime = microtime(true) - $startTime;
self::assertLessThan(1, $elapsedTime, 'Question should not wait for input on a non-interactive stream');
}
}

public function testAskWithAutocomplete()
{
if (!Terminal::hasSttyAvailable()) {
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy