This commit is contained in:
as2252258@163.com
2021-08-03 01:35:56 +08:00
parent d245ac394c
commit 6f09f68dbb
+22 -16
View File
@@ -69,21 +69,6 @@ class Response extends HttpService
$this->format = null; $this->format = null;
} }
/**
* @return string
*/
public function getContentType(): string
{
if ($this->format == null || $this->format == static::JSON) {
return 'application/json;charset=utf-8';
} else if ($this->format == static::XML) {
return 'application/xml;charset=utf-8';
} else {
return 'text/html;charset=utf-8';
}
}
/** /**
* @param $content * @param $content
* @return string * @return string
@@ -91,6 +76,11 @@ class Response extends HttpService
public function toHtml($content): string public function toHtml($content): string
{ {
$this->format = self::HTML; $this->format = self::HTML;
/** @var SResponse $response */
$response = Context::getContext(SResponse::class);
$response->header('Content-Type','text/html;charset=utf-8');
return (string)$content; return (string)$content;
} }
@@ -102,6 +92,11 @@ class Response extends HttpService
public function toJson($content): string|bool public function toJson($content): string|bool
{ {
$this->format = self::JSON; $this->format = self::JSON;
/** @var SResponse $response */
$response = Context::getContext(SResponse::class);
$response->header('Content-Type','application/json;charset=utf-8');
return json_encode($content, JSON_UNESCAPED_UNICODE); return json_encode($content, JSON_UNESCAPED_UNICODE);
} }
@@ -113,6 +108,11 @@ class Response extends HttpService
public function toXml($content): mixed public function toXml($content): mixed
{ {
$this->format = self::XML; $this->format = self::XML;
/** @var SResponse $response */
$response = Context::getContext(SResponse::class);
$response->header('Content-Type','application/xml;charset=utf-8');
return $content; return $content;
} }
@@ -217,13 +217,19 @@ class Response extends HttpService
*/ */
private function sendData($sendData): void private function sendData($sendData): void
{ {
/** @var SResponse $response */
$response = Context::getContext(SResponse::class); $response = Context::getContext(SResponse::class);
if (!$response?->isWritable()) { if (!$response?->isWritable()) {
return; return;
} }
$this->setCookies($response); $this->setCookies($response);
defer(fn() => $this->headers = []); defer(fn() => $this->headers = []);
$response->header('Content-Type', $this->getContentType());
var_dump($response);
// $response->header['']
//
// $response->header('Content-Type', $this->getContentType());
$response->header('Run-Time', $this->getRuntime()); $response->header('Run-Time', $this->getRuntime());
foreach ($this->headers as $key => $header) { foreach ($this->headers as $key => $header) {
$response->header($key, $header); $response->header($key, $header);