From bced72d8112fd1d2341b0afc10cfa2999e3b668e Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Mon, 3 Jul 2023 22:56:59 +0800 Subject: [PATCH 1/2] change --- src/Constrict/ConstrictResponse.php | 2 +- src/OptionsController.php | 23 ++ src/Response.php | 2 +- src/Router.php | 334 +++++++++++++++------------- 4 files changed, 202 insertions(+), 159 deletions(-) create mode 100644 src/OptionsController.php diff --git a/src/Constrict/ConstrictResponse.php b/src/Constrict/ConstrictResponse.php index a5cbe77..2bdb31f 100644 --- a/src/Constrict/ConstrictResponse.php +++ b/src/Constrict/ConstrictResponse.php @@ -76,7 +76,7 @@ class ConstrictResponse extends Message implements ResponseInterface * @param int $statusCode * @return $this */ - public function html(string $content, int $statusCode = 200): static + public function html(string $content = '', int $statusCode = 200): static { $this->getBody()->write($content); return $this->withContentType(ContentType::HTML)->withStatus($statusCode); diff --git a/src/OptionsController.php b/src/OptionsController.php new file mode 100644 index 0000000..b601f89 --- /dev/null +++ b/src/OptionsController.php @@ -0,0 +1,23 @@ +response->withHeaders(['Access-Control-Allow-Headers' => '*', + 'Access-Control-Request-Method' => '*', + 'Access-Control-Allow-Origin' => '*' + ])->html(''); + } + +} \ No newline at end of file diff --git a/src/Response.php b/src/Response.php index 417fcb5..beb10d9 100644 --- a/src/Response.php +++ b/src/Response.php @@ -89,7 +89,7 @@ class Response implements ResponseInterface * @param int $statusCode * @return ResponseInterface */ - public function html(string $content, int $statusCode = 200): ResponseInterface + public function html(string $content = '', int $statusCode = 200): ResponseInterface { return $this->__call__(__FUNCTION__, $content, $statusCode); } diff --git a/src/Router.php b/src/Router.php index adfc212..c6834fb 100644 --- a/src/Router.php +++ b/src/Router.php @@ -23,198 +23,218 @@ const ROUTER_TYPE_HTTP = 'http'; */ class Router { - const METHODS = []; + const METHODS = []; - /** - * @var string - */ - private static string $type = ROUTER_TYPE_HTTP; + /** + * @var string + */ + private static string $type = ROUTER_TYPE_HTTP; - /** - * @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 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 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 + * @param bool $enableOption + * @throws ReflectionException + */ + public static function post(string $route, string $handler, bool $enableOption = true): void + { + $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); + $router->addRoute([RequestMethod::REQUEST_POST], $route, $handler); + if ($enableOption) { + $options = [di(OptionsController::class), 'index']; + $router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options); + } + } - /** - * @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 + * @param bool $enableOption + * @throws ReflectionException + */ + public static function get(string $route, string $handler, bool $enableOption = true): void + { + $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); + $router->addRoute([RequestMethod::REQUEST_GET], $route, $handler); + if ($enableOption) { + $options = [di(OptionsController::class), 'index']; + $router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options); + } + } - /** - * @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 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 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 + * @param bool $enableOption + * @throws ReflectionException + */ + public static function delete(string $route, string $handler, bool $enableOption = true): void + { + $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); + $router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler); + if ($enableOption) { + $options = [di(OptionsController::class), 'index']; + $router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options); + } + } - /** - * @param string $route - * @param string $handler - * @throws ReflectionException - */ - 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 ReflectionException + */ + 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 string $route + * @param string $handler + * @param bool $enableOption + * @throws ReflectionException + */ + public static function put(string $route, string $handler, bool $enableOption = true): void + { + $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); + $router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler); + if ($enableOption) { + $options = [di(OptionsController::class), 'index']; + $router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options); + } + } - /** - * @param array|RequestMethod $methods - * @param string $route - * @param array|string $handler - * @throws ReflectionException - */ - public static function addRoute(array|RequestMethod $methods, string $route, array|string $handler): void - { - $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); - if ($methods instanceof RequestMethod) { - $methods = [$methods]; - } - $router->addRoute($methods, $route, $handler); - } + /** + * @param array|RequestMethod $methods + * @param string $route + * @param array|string $handler + * @throws ReflectionException + */ + public static function addRoute(array|RequestMethod $methods, string $route, array|string $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); + /** + * @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; + $router->groupTack[] = $config; - call_user_func($closure); + call_user_func($closure); - array_pop($router->groupTack); - } + array_pop($router->groupTack); + } - /** - * @throws Exception - */ - public function scan_build_route(): void - { - $scanner = Kiri::getDi()->get(Kiri\Di\Scanner::class); - $scanner->parse('App'); + /** + * @throws Exception + */ + public function scan_build_route(): void + { + $scanner = Kiri::getDi()->get(Kiri\Di\Scanner::class); + $scanner->parse('App'); - $this->read_dir_file(APP_PATH . 'routes'); + $this->read_dir_file(APP_PATH . 'routes'); $router = Kiri::getDi()->get(DataGrip::class)->get(ROUTER_TYPE_HTTP); $router->reset(); } - /** - * @param $path - * @return void - * @throws Exception - */ - 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 $path + * @return void + * @throws Exception + */ + 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 Exception - */ - private function resolve_file($files): void - { - try { - include "$files"; - } catch (\Throwable $throwable) { - error($throwable); - } - } + /** + * @param $files + * @throws Exception + */ + private function resolve_file($files): void + { + try { + include "$files"; + } catch (\Throwable $throwable) { + error($throwable); + } + } } From e3393d602c64c06fd71162ad4f3f5fb611115953 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Mon, 3 Jul 2023 22:59:11 +0800 Subject: [PATCH 2/2] change --- src/OptionsController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/OptionsController.php b/src/OptionsController.php index b601f89..6d38aae 100644 --- a/src/OptionsController.php +++ b/src/OptionsController.php @@ -2,9 +2,12 @@ namespace Kiri\Router; +use Kiri\Router\Annotate\AutoController; use Kiri\Router\Base\Controller; use Psr\Http\Message\ResponseInterface; + +#[AutoController] class OptionsController extends Controller { @@ -17,7 +20,7 @@ class OptionsController extends Controller return $this->response->withHeaders(['Access-Control-Allow-Headers' => '*', 'Access-Control-Request-Method' => '*', 'Access-Control-Allow-Origin' => '*' - ])->html(''); + ])->html(); } } \ No newline at end of file