Files
kiri-router/src/Router.php
T
2026-04-17 16:30:52 +08:00

305 lines
6.9 KiB
PHP

<?php
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;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
/**
*
*
* $component->set([
* 'request' => [
* 'middlewares' => []
* ]
* ])
*/
class Router
{
const array METHODS = [RequestMethod::REQUEST_POST, RequestMethod::REQUEST_GET, RequestMethod::REQUEST_OPTIONS, RequestMethod::REQUEST_DELETE, RequestMethod::REQUEST_PUT, RequestMethod::REQUEST_HEAD];
/**
* @var string
*/
private static string $type = ROUTER_TYPE_HTTP;
private static ?string $currentSourceFile = null;
/**
* @param string $name
* @param Closure $closure
*/
public static function addServer(string $name, Closure $closure): void
{
static::$type = $name;
$closure();
static::$type = ROUTER_TYPE_HTTP;
}
/**
* @param Closure $handler
*/
public static function jsonp(Closure $handler): void
{
static::$type = 'json-rpc';
$handler();
static::$type = ROUTER_TYPE_HTTP;
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function post(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function get(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_GET], $route, $handler);
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function options(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $handler);
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function any(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute(self::METHODS, $route, $handler);
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function delete(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function head(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_HEAD], $route, $handler);
}
/**
* @param string $route
* @param string $handler
*
* @throws
*/
public static function put(string $route, string $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
}
/**
* @param array|RequestMethod $methods
* @param string $route
* @param string|array $handler
*/
public static function addRoute(array|RequestMethod $methods, string $route, string|array $handler): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
if ($methods instanceof RequestMethod) {
$methods = [$methods];
}
$router->addRoute($methods, $route, $handler);
}
/**
* @param array $config
* @param Closure $closure
*
* @throws
*/
public static function group(array $config, Closure $closure): void
{
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->groupTack[] = $config;
call_user_func($closure);
array_pop($router->groupTack);
}
/**
* @throws
*/
public function scan_build_route(): void
{
$coordinator = CoordinatorManager::utility(Coordinator::WORKER_START);
$container = Kiri::getDi();
$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);
$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();
}
/**
* @param ContainerInterface $container
*
* @return void
* @throws
*/
public function reset(ContainerInterface $container): void
{
$router = $container->get(DataGrip::class)->get(static::$type);
if ((bool)config('servers.reload.scan.prebuild_http_handlers', false)) {
$router->warmHttpHandlers();
}
}
/**
* @param $path
*
* @return void
* @throws
*/
private function read_dir_file($path): void
{
$files = glob($path . '/*');
for ($i = 0; $i < count($files); $i++) {
$file = $files[$i];
if (is_dir($file)) {
$this->read_dir_file($file);
} else {
$this->resolve_file($file);
}
}
}
/**
* @param $files
*
* @throws
*/
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;
}
}