This commit is contained in:
2021-07-27 18:50:24 +08:00
parent a4160b78f2
commit e1547e58f8
2 changed files with 9 additions and 34 deletions
+8 -28
View File
@@ -37,7 +37,6 @@ class Response extends HttpService
/** @var int */ /** @var int */
public int $statusCode = 200; public int $statusCode = 200;
public ?SResponse $response = null;
public array $headers = []; public array $headers = [];
public array $cookies = []; public array $cookies = [];
@@ -166,10 +165,10 @@ class Response extends HttpService
{ {
$sendData = $this->parseData($context); $sendData = $this->parseData($context);
$this->statusCode = $statusCode; $this->statusCode = $statusCode;
if ($this->response instanceof SResponse) { if (!Context::hasContext('response')) {
$this->sendData($sendData);
} else {
$this->printResult($sendData); $this->printResult($sendData);
} else {
$this->sendData($sendData);
} }
return $sendData; return $sendData;
} }
@@ -218,35 +217,19 @@ class Response extends HttpService
*/ */
private function sendData($sendData): void private function sendData($sendData): void
{ {
if (!$this->response->isWritable()) { $response = Context::getContext('response');
if (!$response || !$response->isWritable()) {
return; return;
} }
$this->setCookies($response);
defer(fn() => $this->headers = []); defer(fn() => $this->headers = []);
$this->response->header('Content-Type', $this->getContentType());
$this->response->header('Run-Time', $this->getRuntime());
foreach ($this->headers as $key => $header) {
$this->response->header($key, $header);
}
$this->response->status($this->statusCode);
$this->response->end($sendData);
}
/**
* @param SResponse $response
* @return void
*/
private function setHeaders(SResponse $response): void
{
$response->header('Content-Type', $this->getContentType()); $response->header('Content-Type', $this->getContentType());
$response->header('Run-Time', $this->getRuntime()); $response->header('Run-Time', $this->getRuntime());
if (empty($this->headers) || !is_array($this->headers)) {
return;
}
foreach ($this->headers as $key => $header) { foreach ($this->headers as $key => $header) {
$response->header($key, $header); $response->header($key, $header);
} }
$this->headers = []; $response->status($this->statusCode);
$response->end($sendData);
} }
@@ -256,9 +239,6 @@ class Response extends HttpService
*/ */
private function setCookies(SResponse $response): void private function setCookies(SResponse $response): void
{ {
if (empty($this->cookies) || !is_array($this->cookies)) {
return;
}
foreach ($this->cookies as $header) { foreach ($this->cookies as $header) {
$response->setCookie(...$header); $response->setCookie(...$header);
} }
+1 -6
View File
@@ -4,7 +4,6 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
use Annotation\Annotation; use Annotation\Annotation;
use HttpServer\Http\Context;
use HttpServer\Http\HttpParams; use HttpServer\Http\HttpParams;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Http\Response; use HttpServer\Http\Response;
@@ -614,11 +613,7 @@ if (!function_exists('response')) {
*/ */
function response(): Response|stdClass function response(): Response|stdClass
{ {
$response = Context::getContext('response'); return Snowflake::getDi()->get(Response::class);
if (empty($response)){
$response = Snowflake::getApp('response');
}
return $response;
} }
} }