This commit is contained in:
2021-07-27 18:34:57 +08:00
parent 2beaa726cd
commit 0510c1af41
+15 -12
View File
@@ -165,9 +165,9 @@ class Response extends HttpService
public function send(mixed $context = '', int $statusCode = 200): mixed public function send(mixed $context = '', int $statusCode = 200): mixed
{ {
$sendData = $this->parseData($context); $sendData = $this->parseData($context);
$response = Context::getContext('response'); $this->statusCode = $statusCode;
if ($response instanceof SResponse) { if ($this->response instanceof SResponse) {
$this->sendData($response, $sendData, $statusCode); $this->sendData($sendData);
} else { } else {
$this->printResult($sendData); $this->printResult($sendData);
} }
@@ -214,19 +214,21 @@ class Response extends HttpService
} }
/** /**
* @param SResponse $response
* @param $sendData * @param $sendData
* @param $status
* @throws Exception
*/ */
private function sendData(SResponse $response, $sendData, $status): void private function sendData($sendData): void
{ {
if (!$response->isWritable()) { if (!$this->response->isWritable()) {
return; return;
} }
$this->setHeaders($response); defer(fn() => $this->headers = []);
$response->status($status); $this->response->header('Content-Type', $this->getContentType());
$response->end($sendData); $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);
} }
@@ -294,8 +296,9 @@ class Response extends HttpService
*/ */
public static function create($response = null): static public static function create($response = null): static
{ {
Context::setContext('response', $response);
$ciResponse = Snowflake::getDi()->get(Response::class); $ciResponse = Snowflake::getDi()->get(Response::class);
Context::setContext('response', $ciResponse);
$ciResponse->response = $response;
$ciResponse->startTime = microtime(true); $ciResponse->startTime = microtime(true);
$ciResponse->format = self::JSON; $ciResponse->format = self::JSON;
return $ciResponse; return $ciResponse;