diff --git a/core/Di/Container.php b/core/Di/Container.php index d7a02e1d..9408c398 100644 --- a/core/Di/Container.php +++ b/core/Di/Container.php @@ -295,7 +295,7 @@ class Container extends BaseObject implements ContainerInterface /** - * @param ReflectionClass|string $class + * @param string $className * @param string $method * @return array|null * @throws ReflectionException diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php new file mode 100644 index 00000000..a3c60a02 --- /dev/null +++ b/http-handler/Abstracts/Handler.php @@ -0,0 +1,50 @@ +middlewares) || !isset($this->middlewares[$this->offset])) { + return call_user_func($this->handler->callback, ...$this->handler->params); + } + + $middleware = $this->middlewares[$this->offset]; + if (!($middleware instanceof MiddlewareInterface)) { + throw new \Exception('get_implements_class($middleware) not found method process.'); + } + + ++$this->offset; + + return $middleware->process($request, $this); + } + + +} diff --git a/http-handler/Abstracts/HandlerManager.php b/http-handler/Abstracts/HandlerManager.php new file mode 100644 index 00000000..e188a579 --- /dev/null +++ b/http-handler/Abstracts/HandlerManager.php @@ -0,0 +1,50 @@ +get($array[1]); + return $array; + } + +} diff --git a/http-handler/Abstracts/Middleware.php b/http-handler/Abstracts/Middleware.php new file mode 100644 index 00000000..7e091375 --- /dev/null +++ b/http-handler/Abstracts/Middleware.php @@ -0,0 +1,12 @@ +handle($request); + } + +} diff --git a/http-handler/DataGrip.php b/http-handler/DataGrip.php new file mode 100644 index 00000000..9d3aaf66 --- /dev/null +++ b/http-handler/DataGrip.php @@ -0,0 +1,10 @@ +execute($request); + } +} diff --git a/http-handler/Handler.php b/http-handler/Handler.php new file mode 100644 index 00000000..8b241192 --- /dev/null +++ b/http-handler/Handler.php @@ -0,0 +1,49 @@ +route = $route; + + $this->_injectParams($callback); + + $this->callback = $callback; + } + + + /** + * @param array|Closure $callback + * @throws \ReflectionException + */ + private function _injectParams(array|Closure $callback) + { + $container = Kiri::getDi(); + if (!($callback instanceof Closure)) { + $this->params = $container->getMethodParameters($callback[0], $callback[1]); + } else { + $this->params = $container->getFunctionParameters($callback); + } + } +} diff --git a/http-handler/Router.php b/http-handler/Router.php new file mode 100644 index 00000000..ad8c0eae --- /dev/null +++ b/http-handler/Router.php @@ -0,0 +1,129 @@ +groupTack, $options); + + $this->addRoute('GET', $route, $closure); + + array_pop($this->groupTack); + } + + + /** + * @param string $route + * @param string|Closure $closure + * @param array $options + */ + public function post(string $route, string|Closure $closure, array $options = []) + { + array_push($this->groupTack, $options); + + $this->addRoute('POST', $route, $closure); + + array_pop($this->groupTack); + } + + + /** + * @param string|array $method + * @param string $route + * @param string|Closure $closure + */ + public function addRoute(string|array $method, string $route, string|Closure $closure) + { + if (!is_array($method)) $method = [$method]; + $route = $this->getPath($route); + if (is_string($closure)) { + $closure = explode('@', $closure); + $controller = $this->addNamespace($closure[0]); + if (!class_exists($controller)) { + return; + } + $this->addMiddlewares($controller, $closure[0]); + } + foreach ($method as $value) { + HandlerManager::add($route, $value, $closure); + } + } + + + /** + * @param array $config + * @param Closure $closure + */ + public function group(array $config, Closure $closure) + { + array_push($this->groupTack, $config); + + call_user_func($closure, $this); + + array_pop($this->groupTack); + } + + + /** + * @param string $route + * @return string + */ + protected function getPath(string $route): string + { + $route = ltrim($route, '/'); + $prefix = array_column($this->groupTack, 'prefix'); + if (empty($prefix = array_filter($prefix))) { + return $route; + } + return implode('/', $prefix) . $route; + } + + + /** + * @param $controller + * @param $method + */ + protected function addMiddlewares($controller, $method) + { + $middleware = array_column($this->groupTack, 'middleware'); + if (empty($middleware = array_filter($middleware))) { + return; + } + MiddlewareManager::add($controller, $method, $middleware); + } + + + /** + * @param $class + * @return string|null + */ + protected function addNamespace($class): ?string + { + $middleware = array_column($this->groupTack, 'namespace'); + if (empty($middleware = array_filter($middleware))) { + return $class; + } + $middleware[] = $class; + return implode('\\', array_map(function ($value) { + return trim($value, '\\'); + }, $middleware)); + } + + +} diff --git a/http-handler/TestRequest.php b/http-handler/TestRequest.php new file mode 100644 index 00000000..68d944b7 --- /dev/null +++ b/http-handler/TestRequest.php @@ -0,0 +1,70 @@ +initRequestResponse($request); + + /** @var Handler $handler */ + $handler = HandlerManager::get($request->server['request_uri'], $request->getMethod()); + if (is_null($handler)) { + $PsrResponse->withStatus(404)->withBody(null); + } else if (is_integer($handler)) { + $PsrResponse->withStatus($handler)->withBody(null); + } else { + $middlewares = MiddlewareManager::get($handler->callback[0]::class, $handler->callback[1]); + + $stream = new Stream((new Dispatcher($handler, $middlewares))->handle($PsrRequest)); + + $PsrResponse->withStatus(200)->withBody($stream); + } + } catch (\Throwable $throwable) { + + } finally { + $this->response->sender($response, $PsrResponse); + } + } + + + /** + * @param Request $request + * @return array + * @throws Exception + */ + private function initRequestResponse(Request $request): array + { + $PsrResponse = Context::setContext(\Server\Constrict\ResponseInterface::class, new \Http\Message\Response()); + + $PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request)); + + return [$PsrRequest, $PsrResponse]; + } + +}