code everywhere

PHPRunOnChange - Live Reload for your PHP terminal scripts

posted on May 10, 2020, 11:59 pm in

Working on a PHP script, now you can live reload your changes in terminal with ease.

bash -- 70x32
[Desktop]$ php PHPRunOnChange.php tmx.php

Listening on tmx.php for changes ... \



Code:

<?php
    
/*
        Live Reload for your PHP terminal script (vApr 26 2020 - MIT) ...
        Usage: php PHPRunOnChange.php filename.php
    */
    
    
$filename $argv[1];
    
    if(!
file_exists($filename))
        exit(
"File '$filename' Not Found!");
    
    
$time filemtime($filename);
    
$pos=0;
    
    echo 
PHP_EOL."Listening on $filename for changes ... ";
    
    while(
True) {        
        if(
$time != filemtime($filename)) {
            echo 
PHP_EOL."Changes in $filename detected, running script ...".PHP_EOL;
            
sleep(1);
            
$time filemtime($filename);
            echo 
shell_exec("php $filename").PHP_EOL;
        }
        
clearstatcache();        
        
$pos = ($pos+1)%4;
        echo [
'|''/''-''\\'][$pos].chr(8);
        
sleep(1);
    }
?>

Run:

php PHPRunOnChange.php filename.php

Check out the code on GitLab github.com/codeeverywhereca/PHPRunOnChange

recent posts

< back
find something helpful? send us some coin BTC 1A6KbS5AmYQVAh5Zcy9rVSnMAoCEunLUqT

(ad) Need Hosting? Try Linode, VPS Starting from $5