From 2d00a571d5bf327187aeec6ec7352607eec369d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Fri, 15 Sep 2023 09:13:59 +0800 Subject: [PATCH] eee --- src/Base/NotFoundController.php | 17 ++++++++++------- src/RouterCollector.php | 13 +++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Base/NotFoundController.php b/src/Base/NotFoundController.php index 4ca7ca0..e3c2a17 100644 --- a/src/Base/NotFoundController.php +++ b/src/Base/NotFoundController.php @@ -10,12 +10,15 @@ class NotFoundController extends Controller { - /** - * @return ResponseInterface - */ - public function fail(): ResponseInterface - { - return $this->response->withStatus(404, "not found page."); - } + /** + * @return ResponseInterface + */ + public function fail(): ResponseInterface + { + if ($this->request->getMethod() == 'OPTIONS') { + return $this->response->withStatus(200, ""); + } + return $this->response->withStatus(404, "not found page."); + } } diff --git a/src/RouterCollector.php b/src/RouterCollector.php index d9bebaa..f7a7e1c 100644 --- a/src/RouterCollector.php +++ b/src/RouterCollector.php @@ -49,7 +49,11 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate */ public function __construct() { - $this->found = new Handler([di(NotFoundController::class), 'fail'], []); + $found = di(NotFoundController::class); + + $reflection = new \ReflectionMethod($found, 'fail'); + + $this->found = new Handler([$found, 'fail'], [], $reflection->getReturnType()); } @@ -79,7 +83,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate public function addRoute(array $method, string $route, string|array|Closure $closure): void { try { - $route = $this->_splicing_routing($route); + $route = $this->_splicing_routing($route); $interpreter = Kiri::getDi()->get(ControllerInterpreter::class); if ($closure instanceof Closure) { $handler = $interpreter->addRouteByClosure($closure); @@ -204,9 +208,6 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate */ public function query(string $path, string $method): Handler { - if ($method === 'OPTIONS') { - $path = '/*'; - } return $this->methods[$method . '_' . $path] ?? $this->found; } @@ -217,7 +218,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate */ protected function _splicing_routing(string $route): string { - $route = ltrim($route, '/'); + $route = ltrim($route, '/'); $prefix = array_column($this->groupTack, 'prefix'); if (empty($prefix = array_filter($prefix))) { return '/' . $route;