diff --git a/src/Constrict/ConstrictResponse.php b/src/Constrict/ConstrictResponse.php index 7fd556b..3560474 100644 --- a/src/Constrict/ConstrictResponse.php +++ b/src/Constrict/ConstrictResponse.php @@ -14,6 +14,7 @@ class ConstrictResponse extends Message implements ResponseInterface private string $reasonPhrase; + private array $cookieParams; /** @@ -88,6 +89,28 @@ class ConstrictResponse extends Message implements ResponseInterface } + + /** + * @param array $cookies + * @return ResponseInterface + */ + public function withCookieParams(array $cookies): static + { + $this->cookieParams = $cookies; + return $this; + } + + + /** + * @return array + */ + public function getCookieParams(): array + { + return $this->cookieParams; + } + + + /** * @param object $response * @return void diff --git a/src/HttpResponseEmitter.php b/src/HttpResponseEmitter.php index 6f0cc1e..7db3665 100644 --- a/src/HttpResponseEmitter.php +++ b/src/HttpResponseEmitter.php @@ -41,10 +41,7 @@ class HttpResponseEmitter implements ResponseEmitter foreach ($resp->getHeaders() as $name => $header) { $response->header($name, implode(', ', $header)); } - - /** @var Request $request */ - $request = \Kiri::service()->get('request'); - foreach ($request->getCookieParams() as $cookie) { + foreach ($resp->getCookieParams() as $cookie) { $response->setCookie(...$cookie); } $response->header('Server', 'swoole'); diff --git a/src/Response.php b/src/Response.php index 51251d0..98bcd9a 100644 --- a/src/Response.php +++ b/src/Response.php @@ -45,6 +45,25 @@ class Response implements ResponseInterface } + /** + * @param array $cookies + * @return ResponseInterface + */ + public function withCookieParams(array $cookies): ResponseInterface + { + return $this->__call__(__FUNCTION__, $cookies); + } + + + /** + * @return array + */ + public function getCookieParams(): array + { + return $this->__call__(__FUNCTION__); + } + + /** * Retrieves the HTTP protocol version as a string. * diff --git a/src/Server.php b/src/Server.php index 0cdc56d..b4bb074 100644 --- a/src/Server.php +++ b/src/Server.php @@ -109,14 +109,17 @@ class Server implements OnRequestInterface /** @var \Kiri\Router\Response $response */ $response = Kiri::service()->get('response'); + + $cookie = $request->cookie ?? []; + /** @var ConstrictResponse $PsrResponse */ $PsrResponse = Context::set(ResponseInterface::class, new ConstrictResponse()); - $PsrResponse->withContentType($response->contentType); + $PsrResponse->withContentType($response->contentType)->withCookieParams($cookie); $serverRequest = (new ConstrictRequest())->withDataHeaders($request->getData()) ->withUri(Uri::parse($request)) ->withProtocolVersion($request->server['server_protocol']) - ->withCookieParams($request->cookie ?? []) + ->withCookieParams($cookie) ->withQueryParams($request->get ?? []) ->withUploadedFiles($request->files ?? []) ->withMethod($request->getMethod())