eee
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Di;
|
||||
|
||||
class HotReloadState
|
||||
{
|
||||
private const MAX_AGE_SECONDS = 30;
|
||||
|
||||
public function store(array $changedFiles): void
|
||||
{
|
||||
$payload = [
|
||||
'timestamp' => time(),
|
||||
'changed_files' => array_values(array_unique(array_map([$this, 'normalizePath'], array_filter($changedFiles)))),
|
||||
];
|
||||
|
||||
$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'] : [];
|
||||
return array_values(array_unique(array_map([$this, 'normalizePath'], $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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user