PHP code example of easyswoole / file-watcher

1. Go to this page and download the library: Download easyswoole/file-watcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

easyswoole / file-watcher example snippets


$watchRule = new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT."/App");

/**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
$watchRule->setIgnorePaths([EASYSWOOLE_ROOT."/App/Api", EASYSWOOLE_ROOT."/App/Admin"]);

/**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
$watchRule->setIgnoreFiles([EASYSWOOLE_ROOT."/App/Api/Teacher.php", EASYSWOOLE_ROOT."/App/Admin/Teacher.php"]);

/**@var \EasySwoole\FileWatcher\WatchRule $watchRule **/
$watchRule->setType($watchRule::SCAN_TYPE_SUFFIX_MATCH);
//$watchRule->setType($watchRule::SCAN_TYPE_IGNORE_SUFFIX);
$watchRule->setSuffix(['php']);

$fileWatcher = new \EasySwoole\FileWatcher\FileWatcher();
$fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\Inotify::class);
$fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\FileScanner::class);

/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(__DIR__));
$fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT. '/App'));

/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->setOnException(function (\Throwable $throwable){

});

/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->setCheckInterval(1000);

/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
$fileWatcher->setOnChange(function (array $list, \EasySwoole\FileWatcher\WatchRule $rule){
    // list为变化的文件列表
});

/**@var \EasySwoole\FileWatcher\FileWatcher $fileWatcher **/
/**@var \Swoole\Server $server **/
$fileWatcher->attachServer($server);

namespace EasySwoole\EasySwoole;


use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\FileWatcher\FileWatcher;
use EasySwoole\FileWatcher\WatchRule;

class EasySwooleEvent implements Event
{
    public static function initialize()
    {
        date_default_timezone_set('Asia/Shanghai');
    }

    public static function mainServerCreate(EventRegister $register)
    {
        $watcher = new FileWatcher();
        $rule = new WatchRule(EASYSWOOLE_ROOT."/App");
        $watcher->addRule($rule);
        $watcher->setOnChange(function (){
            Logger::getInstance()->info('file change ,reload!!!');
            ServerManager::getInstance()->getSwooleServer()->reload();
        });
        $watcher->attachServer(ServerManager::getInstance()->getSwooleServer());
    }
}