Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to update screen and wait for user input at the same time with PHP?

Tags:

php

I want to write a small management tool to oversee my server processes, my problem is now, how could i wait for user input and at same time update the screen with current stats? Is it even possible with PHP-CLI or are there any tricks for doing this I missing currently?

I have looked into newt and ncurses PECL extensions but both doesn't seem to fit my needs.

like image 907
Tobias Herkula Avatar asked May 14 '12 15:05

Tobias Herkula


1 Answers

Go for libevent http://www.php.net/manual/en/book.libevent.php

You can run your main loop while listening to console with a code roughly like this one:

<?php   
// you need libevent, installable via PEAR
$forever=true;
$base=event_base_new();
$console=event_buffer_new(STDIN,"process_console");
event_buffer_base_set($console,$base);
event_buffer_enable($console,EV_READ);
while ($forever) {
    event_base_loop($base,EVLOOP_NONBLOCK); // Non blocking poll to console listener
    //Do your video update process
}
event_base_free($base); //Cleanup
function process_console($buffer,$id) {
    global $base;
    global $forever;
    $message='';
    while ($read = event_buffer_read($buffer, 256)) {
        $message.=$read;
    }
    $message=trim($message);
    print("[$message]\n");
    if ($message=="quit") {
        event_base_loopexit($base);
        $forever=false;
    }
    else {
        //whatever.....
    }
}
like image 87
Alar Avatar answered Sep 20 '22 02:09

Alar