time(), 'changed_files' => $normalizedFiles, ]; $directory = dirname($this->getFilePath()); if (!is_dir($directory)) { mkdir($directory, 0755, true); } file_put_contents($this->getFilePath(), json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); } public function consume(): array { return $this->peek(); } public function peek(): array { $file = $this->getFilePath(); if (!file_exists($file)) { return []; } $data = json_decode((string)file_get_contents($file), true); if (!is_array($data)) { return []; } $timestamp = (int)($data['timestamp'] ?? 0); if ($timestamp < 1 || (time() - $timestamp) > self::MAX_AGE_SECONDS) { return []; } $files = is_array($data['changed_files'] ?? null) ? $data['changed_files'] : []; $files = array_map([$this, 'normalizePath'], $files); $files = array_values(array_unique($files)); return $files; } private function getFilePath(): string { $basePath = $this->normalizePath($_SERVER['PWD'] ?? APP_PATH ?? getcwd()); $runtimePath = defined('APP_PATH') ? rtrim(str_replace('\\', '/', APP_PATH), '/') . '/storage/.kiri-hot-reload/' : sys_get_temp_dir() . '/kiri-hot-reload/'; return $runtimePath . md5($basePath) . '.json'; } private function normalizePath(string $path): string { $resolved = realpath($path) ?: $path; return str_replace('\\', '/', $resolved); } }