stop(); } /** * @param ?Process $process */ public function process(Process|null $process): void { $this->pipe = inotify_init(); $this->addListen(); Event::add($this->pipe, function () use ($process) { $read = inotify_read($this->pipe); var_dump($read); $this->reload(); }); Event::cycle(function (): void { if ($this->isStop()) { Event::exit(); } }); Event::wait(); } /** * @return void */ public function reload(): void { if ($this->reloading) { return; } $this->reloading = true; var_dump('reloading server, please waite.'); $this->clear(); var_dump('reloading server, please waite. 1'); di(ServerInterface::class)->reload(); var_dump('reloading server, please waite. 2'); $this->addListen(); var_dump('reloading server, please waite. 3'); $this->reloading = false; var_dump('reloading server, please waite. 4'); } /** * @return void */ protected function addListen(): void { foreach (config('reload.listen') as $value) { $this->readDirectory($value); } } /** * @return void */ protected function clear(): void { $this->watches = []; } /** * @param string $directory * @return void */ public function readFile(string $directory): void { if (str_ends_with($directory, '.php') === true) { $wd = inotify_add_watch($this->pipe, $directory, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE); $this->watches[] = $wd; } } /** * @param int $directory * @return void */ public function reWatch(int $directory): void { if (isset($this->watches[$directory])) { inotify_rm_watch($this->pipe, $directory); $path = $this->watches[$directory]; unset($this->watches[$directory]); $data = inotify_add_watch($this->pipe, $path, IN_MODIFY | IN_MOVE | IN_CREATE | IN_DELETE); $this->watches[$data] = $path; } } /** * @param string $directory * @return void */ public function readDirectory(string $directory): void { foreach (glob($directory . '/*') as $data) { if (is_dir($data)) { $this->readDirectory($data); } else { $this->readFile($data); } } } }