diff --git a/http-helper/Route/Node.php b/http-helper/Route/Node.php index 51c410a0..b95f16ff 100644 --- a/http-helper/Route/Node.php +++ b/http-helper/Route/Node.php @@ -27,7 +27,9 @@ class Node public string $path = ''; public int $index = 0; - public string $method = ''; + + /** @var string[] */ + public array $method = []; /** @var Node[] $childes */ public array $childes = []; @@ -38,7 +40,8 @@ class Node private string $_dataType = ''; - private array|Closure|null $_handler = null; + /** @var array */ + private array $_handler = []; public string $htmlSuffix = '.html'; public bool $enableHtmlSuffix = false; @@ -90,12 +93,13 @@ class Node /** - * @param $handler - * @param $path + * @param string|array|Closure $handler + * @param string $method + * @param string $path * @return Node * @throws ReflectionException */ - public function setHandler($handler, $path): static + public function setHandler(string|array|Closure $handler, string $method, string $path): static { $this->sourcePath = '/' . ltrim($path, '/'); if (is_string($handler) && str_contains($handler, '@')) { @@ -103,7 +107,7 @@ class Node } else if ($handler != null && !is_callable($handler, true)) { $this->_error = 'Controller is con\'t exec.'; } - $this->_handler = $handler; + $this->_handler[$method] = $handler; return $this; } @@ -124,23 +128,23 @@ class Node /** + * @param string $method + * @param $handler * @throws NotFindClassException * @throws ReflectionException */ - private function injectMiddleware($handler): static + private function injectMiddleware(string $method, $handler): void { if (!($handler instanceof Closure)) { $callback = $this->injectControllerMiddleware($handler); } else { $callback = $this->injectClosureMiddleware($handler); } - HandlerProviders::add($this->method, $this->sourcePath, $callback); - return $this; + HandlerProviders::add($method, $this->sourcePath, $callback); } /** - * @param $manager * @param $handler * @return mixed * @throws NotFindClassException @@ -156,11 +160,10 @@ class Node /** - * @param $manager * @param $handler - * @return mixed + * @return Closure */ - private function injectClosureMiddleware($handler): mixed + private function injectClosureMiddleware($handler): Closure { if (!empty($this->middleware)) { return MiddlewareManager::closureMiddlewares($this->middleware, $this->normalHandler($handler)); @@ -183,17 +186,20 @@ class Node if (empty($this->_handler)) { return $this; } - $dispatcher = $this->_handler; - if ($dispatcher instanceof Closure) { - $this->_injectParameters = $container->resolveFunctionParameters($dispatcher); - } else { - [$controller, $action] = $dispatcher; - if (is_object($controller)) { - $controller = get_class($controller); + foreach ($this->_handler as $method => $dispatcher) { + if ($dispatcher instanceof Closure) { + $this->_injectParameters = $container->resolveFunctionParameters($dispatcher); + } else { + [$controller, $action] = $dispatcher; + if (is_object($controller)) { + $controller = get_class($controller); + } + $this->_injectParameters = $container->getMethodParameters($controller, $action); } - $this->_injectParameters = $container->getMethodParameters($controller, $action); + $this->injectMiddleware($method, $dispatcher); } - return $this->injectMiddleware($dispatcher); + $this->_handler = []; + return $this; } @@ -261,9 +267,9 @@ class Node * @param RequestInterface $request * @return bool */ - public function methodAllow(RequestInterface $request): bool + #[Pure] public function methodAllow(RequestInterface $request): bool { - if ($this->method == $request->getMethod()) { + if (!in_array($request->getMethod(), $this->method)) { return true; } return $this->method == 'any'; @@ -378,7 +384,10 @@ class Node */ public function dispatch(): mixed { - $handlerProviders = HandlerProviders::get($this->sourcePath, $this->method); + if (!in_array(request()->getMethod(), $this->method)) { + throw new RequestException('

HTTP 405 Method allow


Powered by Swoole', 405); + } + $handlerProviders = HandlerProviders::get($this->sourcePath, request()->getMethod()); if (empty($handlerProviders)) { throw new RequestException('

HTTP 404 Not Found


Powered by Swoole', 404); } diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 21448d67..d10d0da3 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -102,22 +102,6 @@ class Router extends HttpService implements RouterInterface return $this->tree($path, $handler, $method); } - - /** - * @param $path - * @param $handler - * @param string $method - * @return ?Node - */ - private function hash($path, $handler, string $method = 'any'): ?Node - { - $path = $this->resolve($path); - $this->nodes[$method][$path] = $this->NodeInstance($path, 0, $method); - - return $this->nodes[$method][$path]->setHandler($handler); - } - - /** * @param $path * @return string @@ -146,14 +130,14 @@ class Router extends HttpService implements RouterInterface private function tree($path, $handler, string $method = 'any'): Node { $explode = $this->split($path); - if (!isset($this->nodes[$method]['/'])) { - $this->nodes[$method]['/'] = $this->NodeInstance('/', 0, $method); + if (!isset($this->nodes['/'])) { + $this->nodes['/'] = $this->NodeInstance('/', 0, $method); } - $parent = $this->nodes[$method]['/']; + $parent = $this->nodes['/']; if (!empty($explode)) { $parent = $this->bindNode($parent, $explode, $method); } - return $parent->setHandler($handler, $path); + return $parent->setHandler($handler, $method, $path); } @@ -187,7 +171,7 @@ class Router extends HttpService implements RouterInterface */ public function socket($route, $handler): ?Node { - return $this->addRoute($route, $handler, 'socket'); + return $this->addRoute($route, $handler, 'SOCKET'); } @@ -301,7 +285,7 @@ class Router extends HttpService implements RouterInterface $node->childes = []; $node->path = $value; $node->index = $index; - $node->method = $method; + $node->method[] = $method; $node->namespace = $this->loadNamespace($method); $name = array_column($this->groupTacks, 'middleware'); diff --git a/http-server/Message/StatusCode.php b/http-server/Message/StatusCode.php new file mode 100644 index 00000000..878151de --- /dev/null +++ b/http-server/Message/StatusCode.php @@ -0,0 +1,90 @@ +