pid); if (extension_loaded('inotify')) { $this->inotify = inotify_init(); $this->events = IN_MODIFY | IN_DELETE | IN_CREATE | IN_MOVE; $process->name('event: file change.'); $this->watch(APP_PATH); Event::add($this->inotify, [$this, 'check']); Event::wait(); } else { $this->loadByDir(APP_PATH . 'app'); $this->loadByDir(APP_PATH . 'routes'); Timer::tick(2000, [$this, 'tick']); } } private $md5Map = []; /** * @throws Exception */ public function tick() { $this->loadByDir(APP_PATH . 'app', true); $this->loadByDir(APP_PATH . 'routes', true); } /** * @param $path * @param bool $isReload * @return array|void * @throws Exception */ private function loadByDir($path, $isReload = false) { $path = rtrim($path, '/'); if ($this->isReloading) { return; } foreach (glob($path . '/*') as $value) { if (is_dir($value)) { $this->loadByDir($value, $isReload); continue; } $md5 = md5($value); $mTime = filectime($value); if (!isset($this->md5Map[$md5])) { if ($isReload) { $this->isReloading = true; return [$this, 'reload']; } $this->md5Map[$md5] = $mTime; } else { if ($this->md5Map[$md5] != $mTime) { if ($isReload) { $this->isReloading = true; return [$this, 'reload']; } $this->md5Map[$md5] = $mTime; } } } } /** * 开始监听 */ public function check() { if (!($events = inotify_read($this->inotify))) { return; } if ($this->isReloading) { if (!$this->isReloadingOut) { $this->isReloadingOut = true; } return; } $eventList = [IN_CREATE, IN_DELETE, IN_MODIFY, IN_MOVED_TO, IN_MOVED_FROM]; foreach ($events as $ev) { if (empty($ev['name'])) { continue; } if ($ev['mask'] == IN_IGNORED || !in_array($ev['mask'], $eventList)) { continue; } $fileType = strstr($ev['name'], '.'); //非重启类型 if ($fileType !== '.php') { continue; } if ($this->int !== -1) { return; } $this->int = @swoole_timer_after(2000, [$this, 'reload']); $this->isReloading = true; } } /** * @throws Exception */ public function reload() { $this->isReloading = true; //清理所有监听 $this->trigger_reload(); $this->clearWatch(); //重新监听 foreach ($this->dirs as $root) { $this->watch($root); } $this->int = -1; $this->isReloading = FALSE; $this->isReloadingOut = FALSE; $this->loadByDir(APP_PATH . 'app'); } /** * 重启 * @throws ComponentException */ public function trigger_reload() { /** @var Server $server */ $server = Snowflake::app()->get('server')->getServer(); $server->reload(); } /** * 清理所有inotify监听 */ public function clearWatch() { foreach ($this->watchFiles as $wd) { @inotify_rm_watch($this->inotify, $wd); } $this->watchFiles = []; } /** * @param $dir * @param bool $root * @return bool * @throws Exception */ public function watch($dir, $root = TRUE) { if (!is_dir($dir)) { return FALSE; } if (isset($this->watchFiles[$dir]) || in_array($dir, $this->outListener)) { return FALSE; } if ($root) { $this->dirs[] = $dir; } $wd = @inotify_add_watch($this->inotify, $dir, $this->events); $this->watchFiles[$dir] = $wd; $files = scandir($dir); foreach ($files as $f) { if ($f == '.' || $f == '..' || $f == 'runtime' || !preg_match('/\.php/', $f)) { continue; } $path = $dir . '/' . $f; //递归目录 if (is_dir($path)) { $this->watch($path, FALSE); } $wd = @inotify_add_watch($this->inotify, $path, $this->events); $this->watchFiles[$path] = $wd; } return TRUE; } }