This commit is contained in:
2026-04-17 16:30:52 +08:00
parent 16b8df159a
commit 4cfd04c988
7 changed files with 346 additions and 22 deletions
+59 -17
View File
@@ -4,10 +4,12 @@ declare(strict_types=1);
namespace Kiri\Router;
use Closure;
use Kiri\Di\HotReloadState;
use Kiri\Server\Events\OnWorkerStart;
use Kiri;
use Kiri\Abstracts\CoordinatorManager;
use Kiri\Coordinator;
use Kiri\Router\RouteArtifactState;
use Kiri\Router\Validator\ValidatorMiddleware;
use Kiri\Router\Base\Middleware as MiddlewareManager;
use Kiri\Router\Constrict\RequestMethod;
@@ -35,6 +37,8 @@ class Router
*/
private static string $type = ROUTER_TYPE_HTTP;
private static ?string $currentSourceFile = null;
/**
* @param string $name
@@ -187,13 +191,53 @@ class Router
public function scan_build_route(): void
{
$coordinator = CoordinatorManager::utility(Coordinator::WORKER_START);
$this->read_dir_file(APP_PATH . 'routes');
$container = Kiri::getDi();
$container->get(DataGrip::class)->reset(static::$type);
$scanner = $container->get(Kiri\Di\Scanner::class);
$scanner->scan(APP_PATH . 'app/');
$artifactState = $container->get(RouteArtifactState::class);
$scanConfig = array_merge(
config('servers.reload.scan', []),
config('site.scanner', [])
);
$scanner->setConfig($scanConfig);
$changedFiles = $container->get(HotReloadState::class)->consume();
$normalizedAppPath = str_replace('\\', '/', APP_PATH . 'app');
$normalizedRoutePath = str_replace('\\', '/', APP_PATH . 'routes');
$routeChanged = false;
$appChangedFiles = [];
foreach ($changedFiles as $changedFile) {
if (str_starts_with($changedFile, $normalizedRoutePath . '/')) {
$routeChanged = true;
continue;
}
if (str_starts_with($changedFile, $normalizedAppPath . '/')) {
$appChangedFiles[] = $changedFile;
}
}
$usedArtifact = false;
if (($scanConfig['cache_enabled'] ?? false) && !$routeChanged && $artifactState->has(static::$type)) {
$artifact = $artifactState->load(static::$type);
$router = $container->get(DataGrip::class)->get(static::$type);
$usedArtifact = $router->importArtifact($artifact, $appChangedFiles);
}
if (!$usedArtifact) {
$this->read_dir_file(APP_PATH . 'routes');
}
if (!$routeChanged && !empty($appChangedFiles) && ($scanConfig['cache_enabled'] ?? false)) {
$scanner->scanFiles($appChangedFiles, APP_PATH . 'app/', null, !$usedArtifact);
} elseif (!$usedArtifact) {
$scanner->scan(APP_PATH . 'app/');
} else {
$scanner->scanFiles([], APP_PATH . 'app/', null, false);
}
$this->reset($container);
$artifactState->store(static::$type, $container->get(DataGrip::class)->get(static::$type)->exportArtifact());
$coordinator->done();
}
@@ -208,19 +252,8 @@ class Router
public function reset(ContainerInterface $container): void
{
$router = $container->get(DataGrip::class)->get(static::$type);
foreach ($router->getMethods() as $name => $method) {
$middlewares = $method->getMiddlewares();
foreach ($middlewares as $key => $middleware) {
$middlewares[$key] = di($middleware);
}
$requestHandler = new HttpRequestHandler($middlewares, $method);
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
if (!is_null($validator)) {
$requestHandler->withValidatorMiddleware(new ValidatorMiddleware(di(ResponseInterface::class), $method->getClass(), $method->getMethod()));
}
$router->setHttpHandler($name, $requestHandler);
if ((bool)config('servers.reload.scan.prebuild_http_handlers', false)) {
$router->warmHttpHandlers();
}
}
@@ -253,10 +286,19 @@ class Router
private function resolve_file($files): void
{
try {
static::$currentSourceFile = str_replace('\\', '/', realpath($files) ?: $files);
include "$files";
} catch (\Throwable $throwable) {
\Kiri::getLogger()->json_log($throwable);
} finally {
static::$currentSourceFile = null;
}
}
public static function getCurrentSourceFile(): ?string
{
return static::$currentSourceFile;
}
}