PHP code example of easyswoole / hot-reload
1. Go to this page and download the library: Download easyswoole/hot-reload 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 / hot-reload example snippets
// 创建一个服务
('0.0.0.0', '9801');
$server->on('receive', function () {});
// 设置监视器的选项
$hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
// 虚拟机中可以关闭Inotify检测
$hotReloadOptions->disableInotify(true);
// 可以设置多个监控目录的绝对路径
$hotReloadOptions->setMonitorFolder([dirname(__FILE__)]);
// 忽略某些后缀名不去检测
$hotReloadOptions->setIgnoreSuffix(['php', 'txt']);
// 自定义检测到变更后的事件
$hotReloadOptions->setReloadCallback(function (\Swoole\Server $server) {
echo "File change event triggered"; // 可以执行如清理临时目录等逻辑
$server->reload(); // 接管变更事件 需要自己执行重启
});
$hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
$hotReload->attachToServer($server);
$server->start();
class EasySwooleEvent {
// 省略部分代码 ...
/**
* 服务启动时
* @param EventRegister $register
* @throws Exception
*/
public static function mainServerCreate(EventRegister $register)
{
// 配置同上别忘了添加要检视的目录
$hotReloadOptions = new \EasySwoole\HotReload\HotReloadOptions;
$hotReload = new \EasySwoole\HotReload\HotReload($hotReloadOptions);
$hotReloadOptions->setMonitorFolder([EASYSWOOLE_ROOT . '/App']);
$server = ServerManager::getInstance()->getSwooleServer();
$hotReload->attachToServer($server);
}
// 省略部分代码 ...
}