refactor RouteManager static file routing (#105)

This commit is contained in:
sunxyw
2022-04-29 12:23:06 +08:00
committed by GitHub
parent e7a8a32b73
commit 0a7c5f06c6

View File

@@ -10,7 +10,6 @@ use ZM\Annotation\Http\Controller;
use ZM\Annotation\Http\RequestMapping; use ZM\Annotation\Http\RequestMapping;
use ZM\Console\Console; use ZM\Console\Console;
use ZM\Http\StaticFileHandler; use ZM\Http\StaticFileHandler;
use ZM\Store\LightCacheInside;
/** /**
* 路由管理器2.5版本更改了命名空间 * 路由管理器2.5版本更改了命名空间
@@ -50,17 +49,14 @@ class RouteManager
$tail = trim($route, '/'); $tail = trim($route, '/');
$route_name = ($tail === '' ? '' : '/') . $tail . '/{filename}'; $route_name = ($tail === '' ? '' : '/') . $tail . '/{filename}';
Console::debug('添加静态文件路由:' . $route_name); Console::debug('添加静态文件路由:' . $route_name);
$route = new Route($route_name, ['_class' => RouteManager::class, '_method' => 'onStaticRoute']); $route = new Route($route_name, ['_class' => __CLASS__, '_method' => 'onStaticRoute'], [], compact('path'));
// echo $path.PHP_EOL;
LightCacheInside::set('static_route', $route->getPath(), $path);
self::$routes->add(md5($route_name), $route); self::$routes->add(md5($route_name), $route);
} }
public function onStaticRoute($params) public function onStaticRoute(array $params)
{ {
$route_path = self::$routes->get($params['_route'])->getPath(); if (($path = self::$routes->get($params['_route'])->getOption('path')) === null) {
if (($path = LightCacheInside::get('static_route', $route_path)) === null) {
ctx()->getResponse()->endWithStatus(404); ctx()->getResponse()->endWithStatus(404);
return false; return false;
} }