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' );
easyswoole / file-watcher example snippets
$watchRule = new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT."/App" );
$watchRule->setIgnorePaths([EASYSWOOLE_ROOT."/App/Api" , EASYSWOOLE_ROOT."/App/Admin" ]);
$watchRule->setIgnoreFiles([EASYSWOOLE_ROOT."/App/Api/Teacher.php" , EASYSWOOLE_ROOT."/App/Admin/Teacher.php" ]);
$watchRule->setType($watchRule::SCAN_TYPE_SUFFIX_MATCH);
$watchRule->setSuffix(['php' ]);
$fileWatcher = new \EasySwoole\FileWatcher\FileWatcher();
$fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\Inotify::class);
$fileWatcher->setScannerDriver(\EasySwoole\FileWatcher\Scanner\FileScanner::class);
$fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(__DIR__ ));
$fileWatcher->addRule(new \EasySwoole\FileWatcher\WatchRule(EASYSWOOLE_ROOT. '/App' ));
$fileWatcher->setOnException(function (\Throwable $throwable) {
});
$fileWatcher->setCheckInterval(1000 );
$fileWatcher->setOnChange(function (array $list, \EasySwoole\FileWatcher\WatchRule $rule) {
});
$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());
}
}