111
This commit is contained in:
@@ -5,14 +5,14 @@ namespace Server\Constrict;
|
||||
|
||||
|
||||
use Http\Context\Context;
|
||||
use Http\Context\Response as HttpResponse;
|
||||
use Server\ResponseInterface;
|
||||
use Server\Message\Response as Psr7Response;
|
||||
|
||||
|
||||
/**
|
||||
* Class Response
|
||||
* @package Server
|
||||
* @mixin HttpResponse
|
||||
* @mixin Psr7Response
|
||||
*/
|
||||
class Response implements ResponseInterface
|
||||
{
|
||||
@@ -29,10 +29,10 @@ class Response implements ResponseInterface
|
||||
*/
|
||||
public function __call($name, $args)
|
||||
{
|
||||
if (!Context::hasContext(HttpResponse::class)) {
|
||||
$context = Context::setContext(HttpResponse::class, new HttpResponse());
|
||||
if (!Context::hasContext(Psr7Response::class)) {
|
||||
$context = Context::setContext(Psr7Response::class, new Psr7Response());
|
||||
} else {
|
||||
$context = Context::getContext(HttpResponse::class);
|
||||
$context = Context::getContext(Psr7Response::class);
|
||||
}
|
||||
return $context->{$name}(...$args);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Server\Constrict;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Http\Context\Formatter\FileFormatter;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
@@ -17,22 +18,38 @@ class ResponseEmitter implements Emitter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||
{
|
||||
$content = $emitter->configure($response)->getContent();
|
||||
if ($content instanceof FileFormatter) {
|
||||
di(DownloadEmitter::class)->sender($response, $emitter);
|
||||
return;
|
||||
}
|
||||
$response->header('Content-Type', $emitter->getResponseFormat());
|
||||
$response->end($content->getData());
|
||||
}
|
||||
/**
|
||||
* @var \Server\Constrict\DownloadEmitter
|
||||
*/
|
||||
#[Inject(DownloadEmitter::class)]
|
||||
public DownloadEmitter $downloadEmitter;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Http\Response|\Swoole\Http2\Response $response
|
||||
* @param ResponseInterface $emitter
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function sender(mixed $response, ResponseInterface $emitter): void
|
||||
{
|
||||
if (!empty($this->headers) && is_array($this->headers)) {
|
||||
foreach ($this->headers as $name => $values) {
|
||||
$response->header($name, implode(';', $values));
|
||||
}
|
||||
$this->headers = [];
|
||||
}
|
||||
if (!empty($this->cookies) && is_array($this->cookies)) {
|
||||
foreach ($this->cookies as $name => $cookie) {
|
||||
$response->cookie($name, ...$cookie);
|
||||
}
|
||||
$this->cookies = [];
|
||||
}
|
||||
$response->setStatusCode($emitter->getStatusCode());
|
||||
$response->header('Run-Time', time());
|
||||
$response->end($emitter->getBody());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user