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`
* Allow setting human interaction timeout for `Questions`

7.3
---
Expand Down
17 changes: 17 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,23 @@ private function isInteractiveInput($inputStream): bool
*/
private function readInput($inputStream, Question $question): string|false
{
if (null !== $question->getTimeoutSeconds()) {
$streamType = stream_get_meta_data($inputStream)['stream_type'] ?? null;

if ('MEMORY' === $streamType) {
throw new RuntimeException('Using question timeout is incompatible with memory streams.');
}

$read = [$inputStream];
$write = null;
$except = null;
$changedStreams = stream_select($read, $write, $except, $question->getTimeoutSeconds());

if (0 === $changedStreams) {
throw new MissingInputException('Timed out.');
}
}

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 $timoutSeconds = 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->timoutSeconds;
}

/**
* 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->timoutSeconds = $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 @@ -14,6 +14,7 @@
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\MissingInputException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet;
Expand Down Expand Up @@ -186,6 +187,39 @@ 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.');

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(RuntimeException::class);
$this->expectExceptionMessage('Using question timeout is incompatible with memory streams.');

$dialog->ask($this->createStreamableInputInterfaceMock($inputStream), $this->createOutputInterface(), $question);
}

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