Do not use override.

This commit is contained in:
李铭昕 2019-09-02 16:05:54 +08:00
parent 9398486157
commit 94917fa1ff
3 changed files with 41 additions and 19 deletions

View File

@ -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));
}
/**

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\HttpServer;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
class ResponseInstance extends Response
{
protected $response;
public function __construct(PsrResponseInterface $response)
{
$this->response = $response;
}
public function getResponse()
{
return $this->response;
}
}

View File

@ -217,5 +217,7 @@ class ResponseTest extends TestCase
$this->assertInstanceOf(PsrResponseInterface::class, $response);
$response->send();
$this->assertSame($psrResponse, Context::get(PsrResponseInterface::class));
}
}