This commit is contained in:
2024-12-16 16:36:35 +08:00
parent dc561cec9b
commit f38942f4f3
2 changed files with 197 additions and 184 deletions
+190 -178
View File
@@ -12,6 +12,7 @@ use Kiri\Router\Validator\ValidatorMiddleware;
use Kiri\Router\Base\Middleware as MiddlewareManager; use Kiri\Router\Base\Middleware as MiddlewareManager;
use Kiri\Router\Constrict\RequestMethod; use Kiri\Router\Constrict\RequestMethod;
use Psr\Container\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
/** /**
* *
@@ -26,219 +27,230 @@ class Router
{ {
const array METHODS = [RequestMethod::REQUEST_POST, RequestMethod::REQUEST_GET, RequestMethod::REQUEST_OPTIONS, RequestMethod::REQUEST_DELETE, RequestMethod::REQUEST_PUT, RequestMethod::REQUEST_HEAD]; const array METHODS = [RequestMethod::REQUEST_POST, RequestMethod::REQUEST_GET, RequestMethod::REQUEST_OPTIONS, RequestMethod::REQUEST_DELETE, RequestMethod::REQUEST_PUT, RequestMethod::REQUEST_HEAD];
/** /**
* @var string * @var string
*/ */
private static string $type = ROUTER_TYPE_HTTP; private static string $type = ROUTER_TYPE_HTTP;
/** /**
* @param string $name * @param string $name
* @param Closure $closure * @param Closure $closure
*/ */
public static function addServer(string $name, Closure $closure): void public static function addServer(string $name, Closure $closure): void
{ {
static::$type = $name; static::$type = $name;
$closure(); $closure();
static::$type = ROUTER_TYPE_HTTP; static::$type = ROUTER_TYPE_HTTP;
} }
/** /**
* @param Closure $handler * @param Closure $handler
*/ */
public static function jsonp(Closure $handler): void public static function jsonp(Closure $handler): void
{ {
static::$type = 'json-rpc'; static::$type = 'json-rpc';
$handler(); $handler();
static::$type = ROUTER_TYPE_HTTP; static::$type = ROUTER_TYPE_HTTP;
} }
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function post(string $route, string $handler): void */
{ 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); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function get(string $route, string $handler): void */
{ 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); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_GET], $route, $handler);
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function options(string $route, string $handler): void */
{ 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); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $handler);
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function any(string $route, string $handler): void */
{ public static function any(string $route, string $handler): void
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); {
$router->addRoute(self::METHODS, $route, $handler); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute(self::METHODS, $route, $handler);
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function delete(string $route, string $handler): void */
{ 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); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function head(string $route, string $handler): void */
{ 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); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_HEAD], $route, $handler);
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws *
*/ * @throws
public static function put(string $route, string $handler): void */
{ 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); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
}
/** /**
* @param array|RequestMethod $methods * @param array|RequestMethod $methods
* @param string $route * @param string $route
* @param string|array $handler * @param string|array $handler
*/ */
public static function addRoute(array|RequestMethod $methods, string $route, string|array $handler): void public static function addRoute(array|RequestMethod $methods, string $route, string|array $handler): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
if ($methods instanceof RequestMethod) { if ($methods instanceof RequestMethod) {
$methods = [$methods]; $methods = [$methods];
} }
$router->addRoute($methods, $route, $handler); $router->addRoute($methods, $route, $handler);
} }
/** /**
* @param array $config * @param array $config
* @param Closure $closure * @param Closure $closure
* @throws *
*/ * @throws
public static function group(array $config, Closure $closure): void */
{ public static function group(array $config, Closure $closure): void
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->groupTack[] = $config; $router->groupTack[] = $config;
call_user_func($closure); call_user_func($closure);
array_pop($router->groupTack); array_pop($router->groupTack);
} }
/** /**
* @throws * @throws
*/ */
public function scan_build_route(): void public function scan_build_route(): void
{ {
$coordinator = CoordinatorManager::utility(Coordinator::WORKER_START); $coordinator = CoordinatorManager::utility(Coordinator::WORKER_START);
$this->read_dir_file(APP_PATH . 'routes'); $this->read_dir_file(APP_PATH . 'routes');
$container = Kiri::getDi(); $container = Kiri::getDi();
$scanner = $container->get(Kiri\Di\Scanner::class); $scanner = $container->get(Kiri\Di\Scanner::class);
$scanner->load_directory(APP_PATH . 'app/Controller'); $scanner->load_directory(APP_PATH . 'app/Controller');
$this->reset($container); $this->reset($container);
$coordinator->done(); $coordinator->done();
} }
/** /**
* @param ContainerInterface $container * @param ContainerInterface $container
* @return void *
* @throws * @return void
*/ * @throws
public function reset(ContainerInterface $container): void */
{ public function reset(ContainerInterface $container): void
$router = $container->get(DataGrip::class)->get(static::$type); {
foreach ($router->getMethods() as $name => $method) { $router = $container->get(DataGrip::class)->get(static::$type);
$middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod()); foreach ($router->getMethods() as $name => $method) {
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod()); $middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod());
if (!is_null($validator)) { $validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
array_unshift($middlewares, new ValidatorMiddleware($method->getClass(), $method->getMethod())); if (!is_null($validator)) {
} array_unshift($middlewares, new ValidatorMiddleware(di(ResponseInterface::class), $method->getClass(), $method->getMethod()));
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method)); }
} $router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
} }
}
/** /**
* @param $path * @param $path
* @return void *
* @throws * @return void
*/ * @throws
private function read_dir_file($path): void */
{ private function read_dir_file($path): void
$files = glob($path . '/*'); {
for ($i = 0; $i < count($files); $i++) { $files = glob($path . '/*');
$file = $files[$i]; for ($i = 0; $i < count($files); $i++) {
if (is_dir($file)) { $file = $files[$i];
$this->read_dir_file($file); if (is_dir($file)) {
} else { $this->read_dir_file($file);
$this->resolve_file($file); } else {
} $this->resolve_file($file);
} }
} }
}
/** /**
* @param $files * @param $files
* @throws *
*/ * @throws
private function resolve_file($files): void */
{ private function resolve_file($files): void
try { {
include "$files"; try {
} catch (\Throwable $throwable) { include "$files";
error($throwable); } catch (\Throwable $throwable) {
} error($throwable);
} }
}
} }
+7 -6
View File
@@ -17,11 +17,12 @@ class ValidatorMiddleware implements MiddlewareInterface
{ {
/** /**
* @param string $class * @param ResponseInterface $response
* @param string $method * @param string $class
*/ * @param string $method
public function __construct(public string $class, public string $method) */
public function __construct(public ResponseInterface $response ,public string $class, public string $method)
{ {
} }
@@ -38,7 +39,7 @@ class ValidatorMiddleware implements MiddlewareInterface
if (!$validator->run($request)) { if (!$validator->run($request)) {
Kiri::getLogger()->println($request->getUri()->getPath() . ' `' . $validator->error() . '`'); Kiri::getLogger()->println($request->getUri()->getPath() . ' `' . $validator->error() . '`');
return di(ResponseInterface::class)->html($validator->error(), 415); return $this->response->html($validator->error(), 415);
} else { } else {
return $handler->handle($request); return $handler->handle($request);
} }