Compare commits
5
Commits
v1.13
...
7bc8d04df3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7bc8d04df3 | ||
|
|
e7d5d1b567 | ||
|
|
614f21b240 | ||
|
|
1bca442f55 | ||
|
|
aac6d5b80a |
+39
-4
@@ -38,7 +38,7 @@ class File
|
||||
4 => 'No file was uploaded.',
|
||||
6 => 'Missing a temporary folder.',
|
||||
7 => 'Failed to write file to disk.',
|
||||
8 => 'A PHP extension stopped the file upload.'
|
||||
8 => 'A PHP extension stopped the file upload.',
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -65,14 +65,19 @@ class File
|
||||
*/
|
||||
public function rename(): string
|
||||
{
|
||||
if (!empty($this->newName)) return $this->newName;
|
||||
if (!empty($this->newName))
|
||||
return $this->newName;
|
||||
if (!file_exists($this->getTmpPath())) {
|
||||
throw new Exception('(' . $this->name . ')Failed to open stream: No such file or directory');
|
||||
}
|
||||
|
||||
$hash = md5_file($this->getTmpPath());
|
||||
|
||||
$later = '.' . exif_imagetype($this->getTmpPath());
|
||||
$later = match ($this->type) {
|
||||
'image/jpeg', 'image/jpg' => '.jpeg',
|
||||
'image/gif' => '.gif',
|
||||
'image/png' => '.png',
|
||||
default => '.' . $this->exif_imageType(),
|
||||
};
|
||||
|
||||
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
|
||||
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
|
||||
@@ -80,6 +85,36 @@ class File
|
||||
return $this->name = strtoupper($tmp) . $later;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function exif_imageType(): ?int
|
||||
{
|
||||
return match (\exif_imagetype($this->tmp_name)) {
|
||||
IMAGETYPE_GIF => '.gif',
|
||||
IMAGETYPE_JPEG => '.jpg',
|
||||
IMAGETYPE_PNG => '.png',
|
||||
IMAGETYPE_SWF => '.swf',
|
||||
IMAGETYPE_PSD => '.psd',
|
||||
IMAGETYPE_BMP => '.bmp',
|
||||
IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM => '.tiff',
|
||||
IMAGETYPE_JPC => '.jpc',
|
||||
IMAGETYPE_JP2 => '.jp2',
|
||||
IMAGETYPE_JPX => '.jpx',
|
||||
IMAGETYPE_JB2 => '.jb2',
|
||||
IMAGETYPE_SWC => '.swc',
|
||||
IMAGETYPE_IFF => '.iff',
|
||||
IMAGETYPE_WBMP => '.wbmp',
|
||||
IMAGETYPE_XBM => '.xbm',
|
||||
IMAGETYPE_ICO => '.ico',
|
||||
IMAGETYPE_WEBP => '.webp',
|
||||
IMAGETYPE_AVIF => '.avif',
|
||||
default => throw new Exception('未知的图片类型'),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
|
||||
+101
-13
@@ -8,21 +8,78 @@ class RouteArtifactState
|
||||
{
|
||||
public function store(string $type, array $artifact): void
|
||||
{
|
||||
$previous = $this->loadPayload($type);
|
||||
$payload = [
|
||||
'timestamp' => time(),
|
||||
'type' => $type,
|
||||
'artifact' => $artifact,
|
||||
'build' => is_array($previous['build'] ?? null) ? $previous['build'] : null,
|
||||
];
|
||||
|
||||
$directory = dirname($this->getFilePath($type));
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0755, true);
|
||||
}
|
||||
|
||||
file_put_contents($this->getFilePath($type), json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
||||
$this->writePayload($type, $payload);
|
||||
}
|
||||
|
||||
public function load(string $type): array
|
||||
{
|
||||
$data = $this->loadPayload($type);
|
||||
return is_array($data['artifact'] ?? null) ? $data['artifact'] : [];
|
||||
}
|
||||
|
||||
public function markBuild(string $type, array $changedFiles): void
|
||||
{
|
||||
$data = $this->loadPayload($type);
|
||||
if (!is_array($data['artifact'] ?? null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data['build'] = [
|
||||
'generation' => time(),
|
||||
'timestamp' => time(),
|
||||
'changed_files' => $this->normalizeFiles($changedFiles),
|
||||
];
|
||||
|
||||
$this->writePayload($type, $data);
|
||||
}
|
||||
|
||||
public function clearBuild(string $type): void
|
||||
{
|
||||
$data = $this->loadPayload($type);
|
||||
if (empty($data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$data['build'] = null;
|
||||
$this->writePayload($type, $data);
|
||||
}
|
||||
public function coversChangedFiles(string $type, array $changedFiles): bool
|
||||
{
|
||||
if (empty($changedFiles)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->loadPayload($type);
|
||||
$build = is_array($data['build'] ?? null) ? $data['build'] : [];
|
||||
$builtFiles = is_array($build['changed_files'] ?? null) ? $build['changed_files'] : [];
|
||||
if (empty($builtFiles)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$built = array_fill_keys($this->normalizeFiles($builtFiles), true);
|
||||
foreach ($this->normalizeFiles($changedFiles) as $file) {
|
||||
if (!isset($built[$file])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function has(string $type): bool
|
||||
{
|
||||
return file_exists($this->getFilePath($type));
|
||||
}
|
||||
|
||||
private function loadPayload(string $type): array
|
||||
{
|
||||
$file = $this->getFilePath($type);
|
||||
if (!file_exists($file)) {
|
||||
@@ -30,16 +87,47 @@ class RouteArtifactState
|
||||
}
|
||||
|
||||
$data = json_decode((string)file_get_contents($file), true);
|
||||
if (!is_array($data)) {
|
||||
return [];
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
return is_array($data['artifact'] ?? null) ? $data['artifact'] : [];
|
||||
}
|
||||
|
||||
public function has(string $type): bool
|
||||
private function writePayload(string $type, array $payload): void
|
||||
{
|
||||
return file_exists($this->getFilePath($type));
|
||||
$file = $this->getFilePath($type);
|
||||
$directory = dirname($file);
|
||||
if (!is_dir($directory)) {
|
||||
mkdir($directory, 0755, true);
|
||||
}
|
||||
|
||||
$tmpFile = $file . '.tmp.' . getmypid();
|
||||
$json = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
|
||||
if (file_put_contents($tmpFile, $json, LOCK_EX) === false) {
|
||||
throw new \RuntimeException('Unable to write route artifact: ' . $tmpFile);
|
||||
}
|
||||
|
||||
if (@rename($tmpFile, $file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Windows rename() cannot replace an existing file.
|
||||
if (is_file($file) && @unlink($file) && @rename($tmpFile, $file)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@unlink($tmpFile);
|
||||
throw new \RuntimeException('Unable to publish route artifact: ' . $file);
|
||||
}
|
||||
|
||||
private function normalizeFiles(array $files): array
|
||||
{
|
||||
$normalized = [];
|
||||
foreach ($files as $file) {
|
||||
if (!is_string($file) || $file === '') {
|
||||
continue;
|
||||
}
|
||||
$normalized[] = str_replace('\\', '/', realpath($file) ?: $file);
|
||||
}
|
||||
|
||||
return array_values(array_unique($normalized));
|
||||
}
|
||||
|
||||
private function getFilePath(string $type): string
|
||||
|
||||
+33
-13
@@ -214,6 +214,18 @@ class Router
|
||||
|
||||
$changedFiles = $container->get(HotReloadState::class)->consume();
|
||||
|
||||
$artifactState = $container->get(RouteArtifactState::class);
|
||||
if (getenv('KIRI_FILE_BUILD') !== '1' && !empty($changedFiles) && $artifactState->coversChangedFiles(static::$type, $changedFiles)) {
|
||||
$container->get(DataGrip::class)->reset(static::$type);
|
||||
$router = $container->get(DataGrip::class)->get(static::$type);
|
||||
if ($router->importArtifact($artifactState->load(static::$type))) {
|
||||
$this->read_dir_file(APP_PATH . 'routes');
|
||||
$this->reset($container);
|
||||
$coordinator->done();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Worker 首次启动(无变更文件 + Master 已完成扫描):
|
||||
// 重新 include 路由文件(Router::get/post 显式注册) + 基于 Master 扫描清单重建注解路由
|
||||
// 避免 opcache_compile_file,仅用 Reflection 重建路由,内存开销极小
|
||||
@@ -232,21 +244,20 @@ class Router
|
||||
$container->get(DataGrip::class)->reset(static::$type);
|
||||
|
||||
$scanner = $container->get(Kiri\Di\Scanner::class);
|
||||
$artifactState = $container->get(RouteArtifactState::class);
|
||||
$scanConfig = array_merge(
|
||||
config('servers.reload.scan', []),
|
||||
config('site.scanner', [])
|
||||
);
|
||||
$scanner->setConfig($scanConfig);
|
||||
|
||||
$normalizedAppPath = str_replace('\\', '/', APP_PATH . 'app');
|
||||
$normalizedRoutePath = str_replace('\\', '/', APP_PATH . 'routes');
|
||||
$routeChanged = false;
|
||||
$normalizedAppPath = str_replace('\\', '/', realpath(APP_PATH . 'app') ?: APP_PATH . 'app');
|
||||
$normalizedRoutePath = str_replace('\\', '/', realpath(APP_PATH . 'routes') ?: APP_PATH . 'routes');
|
||||
$routeChangedFiles = [];
|
||||
$appChangedFiles = [];
|
||||
|
||||
foreach ($changedFiles as $changedFile) {
|
||||
if (str_starts_with($changedFile, $normalizedRoutePath . '/')) {
|
||||
$routeChanged = true;
|
||||
$routeChangedFiles[] = $changedFile;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -255,23 +266,29 @@ class Router
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($appChangedFiles) && (bool)($scanConfig['expand_dependencies'] ?? false)) {
|
||||
$appChangedFiles = $scanner->expandDependentFiles($appChangedFiles, APP_PATH . 'app/');
|
||||
}
|
||||
|
||||
$usedArtifact = false;
|
||||
if (($scanConfig['cache_enabled'] ?? false) && !$routeChanged && $artifactState->has(static::$type)) {
|
||||
$canUseArtifact = $artifactState->has(static::$type)
|
||||
&& (!empty($changedFiles) || ($scanConfig['cache_enabled'] ?? false));
|
||||
if ($canUseArtifact) {
|
||||
$artifact = $artifactState->load(static::$type);
|
||||
$router = $container->get(DataGrip::class)->get(static::$type);
|
||||
$usedArtifact = $router->importArtifact($artifact, $appChangedFiles);
|
||||
$usedArtifact = $router->importArtifact($artifact, array_merge($appChangedFiles, $routeChangedFiles));
|
||||
}
|
||||
|
||||
// routes 目录中的显式路由文件必须每次重建路由表时重新 include。
|
||||
// route artifact 只加速注解路由,不能替代 routes/*.php 的注册副作用。
|
||||
$this->read_dir_file(APP_PATH . 'routes');
|
||||
|
||||
if (!$routeChanged && !empty($appChangedFiles) && ($scanConfig['cache_enabled'] ?? false)) {
|
||||
$scanner->scanFiles($appChangedFiles, APP_PATH . 'app/', null, !$usedArtifact);
|
||||
if (!empty($appChangedFiles) && (($scanConfig['cache_enabled'] ?? false) || $usedArtifact)) {
|
||||
$scanner->scanFiles($appChangedFiles, APP_PATH . 'app/', null, true, !$usedArtifact);
|
||||
} elseif (!$usedArtifact) {
|
||||
$scanner->scan(APP_PATH . 'app/');
|
||||
} else {
|
||||
$scanner->scanFiles([], APP_PATH . 'app/', null, false);
|
||||
$scanner->scanFiles([], APP_PATH . 'app/', null, false, false);
|
||||
}
|
||||
$this->reset($container);
|
||||
$artifactState->store(static::$type, $container->get(DataGrip::class)->get(static::$type)->exportArtifact());
|
||||
@@ -462,8 +479,12 @@ class Router
|
||||
private function resolve_file($files): void
|
||||
{
|
||||
try {
|
||||
static::$currentSourceFile = str_replace('\\', '/', realpath($files) ?: $files);
|
||||
include "$files";
|
||||
$file = realpath($files) ?: $files;
|
||||
static::$currentSourceFile = str_replace('\\\\', '/', $file);
|
||||
if (function_exists('opcache_invalidate')) {
|
||||
@opcache_invalidate($file, true);
|
||||
}
|
||||
include $file;
|
||||
} catch (\Throwable $throwable) {
|
||||
\Kiri::getLogger()->json_log($throwable);
|
||||
} finally {
|
||||
@@ -471,7 +492,6 @@ class Router
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function getCurrentSourceFile(): ?string
|
||||
{
|
||||
return static::$currentSourceFile;
|
||||
|
||||
@@ -282,10 +282,6 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
|
||||
public function importArtifact(array $artifact, array $excludeSourceFiles = []): bool
|
||||
{
|
||||
if (($artifact['has_closure_routes'] ?? false) === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entries = $artifact['entries'] ?? null;
|
||||
if (!is_array($entries)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user