mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-30 10:47:44 +08:00
Do not use override.
This commit is contained in:
parent
9398486157
commit
94917fa1ff
@ -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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
30
src/http-server/src/ResponseInstance.php
Normal file
30
src/http-server/src/ResponseInstance.php
Normal 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;
|
||||
}
|
||||
}
|
@ -217,5 +217,7 @@ class ResponseTest extends TestCase
|
||||
$this->assertInstanceOf(PsrResponseInterface::class, $response);
|
||||
|
||||
$response->send();
|
||||
|
||||
$this->assertSame($psrResponse, Context::get(PsrResponseInterface::class));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user