diff --git a/src/Response.php b/src/Response.php index 2de7751..fdb588a 100644 --- a/src/Response.php +++ b/src/Response.php @@ -8,7 +8,12 @@ use Kiri\Di\Context; use Kiri\Router\Constrict\ConstrictResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; +use ReflectionException; + +/** + * + */ class Response implements ResponseInterface { @@ -19,6 +24,24 @@ class Response implements ResponseInterface public ContentType $contentType = ContentType::JSON; + /** + * @var string|SwooleHttpResponseEmitterInterface + */ + public string|SwooleHttpResponseEmitterInterface $emmit = SwooleHttpResponseEmitterInterface::class; + + + /** + * @return void + * @throws ReflectionException + */ + public function init(): void + { + if (is_string($this->emmit)) { + $this->emmit = di($this->emmit); + } + } + + /** * @param ContentType $contentType * @return Response diff --git a/src/Server.php b/src/Server.php deleted file mode 100644 index 84d2bf5..0000000 --- a/src/Server.php +++ /dev/null @@ -1,157 +0,0 @@ -emitter = $container->get(HttpResponseEmitter::class); - - $exception = $this->request->exception; - if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { - $exception = ExceptionHandlerDispatcher::class; - } - $this->exception = $container->get($exception); - - $this->router = $container->get(DataGrip::class)->get(ROUTER_TYPE_HTTP); - } - - - /** - * @param Request $request - * @param Response $response - * @throws Exception - */ - public function onRequest(Request $request, Response $response): void - { - try { - /** @var ConstrictRequest $PsrRequest */ - $PsrRequest = $this->initRequestAndResponse($request); - - $request_uri = $request->getMethod() == 'OPTIONS' ? '/*' : $request->server['request_uri']; - $dispatcher = $this->router->query($request_uri, $request->getMethod()); - - $middleware = []; - if (!($dispatcher instanceof Kiri\Router\Base\NotFoundController)) { - $middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class); - - $middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod()); - } - - $PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($PsrRequest); - } catch (\Throwable $throwable) { - error($throwable); - $PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class)); - } finally { - $this->emitter->sender($PsrResponse, $response); - } - } - - - /** - * @param Request $request - * @return RequestInterface - * @throws Exception - */ - private function initRequestAndResponse(Request $request): RequestInterface - { - /** @var ConstrictResponse $PsrResponse */ - $PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse()); - $PsrResponse->withContentType($this->response->contentType); - - $serverRequest = (new ConstrictRequest())->withDataHeaders($request->getData()) - ->withUri(static::parse($request)) - ->withProtocolVersion($request->server['server_protocol']) - ->withCookieParams($request->cookie ?? []) - ->withQueryParams($request->get ?? []) - ->withUploadedFiles($request->files ?? []) - ->withMethod($request->getMethod()) - ->withParsedBody($request->post ?? []); - - /** @var ConstrictRequest $PsrRequest */ - return Context::set(RequestInterface::class, $serverRequest); - } - - - /** - * @param Request $request - * @return UriInterface - */ - public static function parse(Request $request): UriInterface - { - $uri = new Uri(); - $uri->withQuery($request->server['query_string'] ?? '') - ->withPath($request->server['path_info']) - ->withPort($request->server['server_port']); - if (isset($request->server['https']) && $request->server['https'] !== 'off') { - $uri->withScheme('https'); - } else { - $uri->withScheme('http'); - } - return $uri; - } - - - -} diff --git a/src/ServerRequest.php b/src/ServerRequest.php new file mode 100644 index 0000000..49a1b82 --- /dev/null +++ b/src/ServerRequest.php @@ -0,0 +1,96 @@ +request->exception; + if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { + $exception = ExceptionHandlerDispatcher::class; + } + $this->exception = $container->get($exception); + $this->router = $container->get(DataGrip::class)->get(ROUTER_TYPE_HTTP); + + $this->emitter = Kiri::service()->get('response')->emmit; + } + + + /** + * @param ServerRequestInterface $request + * @param object $response + * @return void + * @throws + */ + public function onServerRequest(ServerRequestInterface $request, object $response): void + { + try { + $request_uri = $request->getMethod() == 'OPTIONS' ? '/*' : $request->getUri()->getPath(); + $dispatcher = $this->router->query($request_uri, $request->getMethod()); + + $middleware = []; + if (!($dispatcher instanceof Kiri\Router\Base\NotFoundController)) { + $middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class); + + $middleware = $middlewareManager->get($dispatcher->getClass(), $dispatcher->getMethod()); + } + + $PsrResponse = (new HttpRequestHandler($middleware, $dispatcher))->handle($request); + } catch (\Throwable $throwable) { + error($throwable); + $PsrResponse = $this->exception->emit($throwable, di(ConstrictResponse::class)); + } finally { + $this->emitter->sender($PsrResponse, $response); + } + } + +} diff --git a/src/StreamResponse.php b/src/StreamResponse.php index fc234ff..8700c60 100644 --- a/src/StreamResponse.php +++ b/src/StreamResponse.php @@ -3,8 +3,10 @@ declare(strict_types=1); namespace Kiri\Router; -use Psr\Http\Message\ResponseInterface; +/** + * + */ class StreamResponse extends Response { diff --git a/src/HttpResponseEmitter.php b/src/SwooleHttpResponseEmitterInterface.php similarity index 88% rename from src/HttpResponseEmitter.php rename to src/SwooleHttpResponseEmitterInterface.php index b8d4b00..398c0c6 100644 --- a/src/HttpResponseEmitter.php +++ b/src/SwooleHttpResponseEmitterInterface.php @@ -4,11 +4,11 @@ declare(strict_types=1); namespace Kiri\Router; use Exception; -use Kiri\Di\Interface\ResponseEmitter; +use Kiri\Di\Interface\ResponseEmitterInterface; use Psr\Http\Message\ResponseInterface; -class HttpResponseEmitter implements ResponseEmitter +class SwooleHttpResponseEmitterInterface implements ResponseEmitterInterface { diff --git a/src/SwowHttpResponseEmitterInterface.php b/src/SwowHttpResponseEmitterInterface.php new file mode 100644 index 0000000..27c0e48 --- /dev/null +++ b/src/SwowHttpResponseEmitterInterface.php @@ -0,0 +1,24 @@ +withHeader('Server', 'Swow'); + $response->sendHttpResponse($proxy); + } + +}