Happy Laracon Week! All accounts are 60% off this week.

kocoten1992's avatar

Laravel console Auto confirm after sometimes

Hi,

I'm running onto a problem, I have the code:

// https://laravel.com/docs/5.4/artisan
if ($this->confirm('Do you wish to continue?')) {
    // I might not at the computer at that time, after 10s of no interaction, please just continue ( yes or y )
}

I wrote a wrapper console app and want to have feature like above, how can I achieve that?

Thank for reading this guys :).

0 likes
5 replies
bobbybouwmann's avatar

I have bad news for you, that is not possible. The confirm method will wait for a confirmation from the prompt and not from the Laravel application. So it will wait on use interaction, but you can't fake that from a PHP application as far as I know.

kocoten1992's avatar
kocoten1992
OP
Best Answer
Level 10

Thank god, there is workaround, I've invest too much to write from scratch

https://stackoverflow.com/questions/16466200/php-cli-ask-for-user-input-or-perform-action-after-a-period-of-time

echo "input something ... (5 sec)\n";

// get file descriptor for stdin 
$fd = fopen('php://stdin', 'r');

// prepare arguments for stream_select()
$read = array($fd);
$write = $except = array(); // we don't care about this
$timeout = 5;

// wait for maximal 5 seconds for input
if(stream_select($read, $write, $except, $timeout)) {
    echo "you typed: " . fgets($fd) . PHP_EOL;
} else {
    echo "you typed nothing\n";
}
joshmanders's avatar

That's kind of an anti-pattern isn't it? if I am being asked if I want to continue on deleting the whole drive by accident, I'd want it to wait until I actually say yes, not just assume because I never answered, I meant yes.

Please or to participate in this conversation.