diff --git a/src/http-server/src/Response.php b/src/http-server/src/Response.php index 33f686e90..fbd866557 100644 --- a/src/http-server/src/Response.php +++ b/src/http-server/src/Response.php @@ -14,7 +14,6 @@ namespace Hyperf\HttpServer; use BadMethodCallException; use Hyperf\HttpMessage\Cookie\Cookie; -use Hyperf\HttpMessage\Server\Response as ServerResponse; use Hyperf\HttpMessage\Stream\SwooleFileStream; use Hyperf\HttpMessage\Stream\SwooleStream; use Hyperf\HttpServer\Contract\ResponseInterface; @@ -166,14 +165,7 @@ class Response implements PsrResponseInterface, ResponseInterface public function withCookie(Cookie $cookie): ResponseInterface { - Context::override(PsrResponseInterface::class, function ($response) use ($cookie) { - if (! $response instanceof ServerResponse) { - throw new InvalidResponseException('The response is not instanceof ' . ServerResponse::class); - } - return $response->withCookie($cookie); - }); - - return $this; + return $this->call(__FUNCTION__, func_get_args()); } /** @@ -412,19 +404,17 @@ class Response implements PsrResponseInterface, ResponseInterface protected function call($name, $arguments) { - Context::override(PsrResponseInterface::class, function ($response) use ($name, $arguments) { - if (! $response instanceof PsrResponseInterface) { - throw new InvalidResponseException('The response is not instanceof ' . PsrResponseInterface::class); - } + $response = $this->getResponse(); - if (! method_exists($response, $name)) { - throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $name)); - } + if (! $response instanceof PsrResponseInterface) { + throw new InvalidResponseException('The response is not instanceof ' . PsrResponseInterface::class); + } - return $response->{$name}(...$arguments); - }); + if (! method_exists($response, $name)) { + throw new BadMethodCallException(sprintf('Call to undefined method %s::%s()', get_class($this), $name)); + } - return $this; + return new ResponseInstance($response->{$name}(...$arguments)); } /** diff --git a/src/http-server/src/ResponseInstance.php b/src/http-server/src/ResponseInstance.php new file mode 100644 index 000000000..f77a37826 --- /dev/null +++ b/src/http-server/src/ResponseInstance.php @@ -0,0 +1,30 @@ +response = $response; + } + + public function getResponse() + { + return $this->response; + } +} diff --git a/src/http-server/tests/ResponseTest.php b/src/http-server/tests/ResponseTest.php index 6cc75c147..4f830559b 100644 --- a/src/http-server/tests/ResponseTest.php +++ b/src/http-server/tests/ResponseTest.php @@ -217,5 +217,7 @@ class ResponseTest extends TestCase $this->assertInstanceOf(PsrResponseInterface::class, $response); $response->send(); + + $this->assertSame($psrResponse, Context::get(PsrResponseInterface::class)); } }