改名
This commit is contained in:
@@ -43,16 +43,28 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
|||||||
*/
|
*/
|
||||||
public function withContentType(string $type): static
|
public function withContentType(string $type): static
|
||||||
{
|
{
|
||||||
if (!in_array($type, [
|
|
||||||
Response::CONTENT_TYPE_HTML, Response::CONTENT_TYPE_JSON,
|
|
||||||
Response::CONTENT_TYPE_STREAM, Response::CONTENT_TYPE_XML
|
|
||||||
])) {
|
|
||||||
throw new Exception('Wrong content type.');
|
|
||||||
}
|
|
||||||
return $this->withHeader('Content-Type', $type);
|
return $this->withHeader('Content-Type', $type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
#[Pure] public function hasContentType(): bool
|
||||||
|
{
|
||||||
|
return $this->hasHeader('Content-Type');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
#[Pure] public function getContentType(): string
|
||||||
|
{
|
||||||
|
return $this->getHeaderLine('Content-Type');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use Http\Exception\RequestException;
|
|||||||
use Http\Route\Node;
|
use Http\Route\Node;
|
||||||
use Kiri\Core\Help;
|
use Kiri\Core\Help;
|
||||||
use Server\Events\OnAfterRequest;
|
use Server\Events\OnAfterRequest;
|
||||||
use Server\Message\Stream;
|
use Server\Message\Response as MsgResponse;
|
||||||
use Server\ResponseInterface;
|
use Server\ResponseInterface;
|
||||||
use Server\SInterface\OnClose;
|
use Server\SInterface\OnClose;
|
||||||
use Server\SInterface\OnConnect;
|
use Server\SInterface\OnConnect;
|
||||||
@@ -16,7 +16,6 @@ use Swoole\Error;
|
|||||||
use Swoole\Http\Request;
|
use Swoole\Http\Request;
|
||||||
use Swoole\Http\Response;
|
use Swoole\Http\Response;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Server\Message\Response as MsgResponse;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -68,14 +67,13 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
|||||||
private function transferToResponse($responseData): ResponseInterface
|
private function transferToResponse($responseData): ResponseInterface
|
||||||
{
|
{
|
||||||
$interface = $this->response->withStatus(200);
|
$interface = $this->response->withStatus(200);
|
||||||
if (!$interface->hasHeader('Content-Type')) {
|
if (!$interface->hasContentType()) {
|
||||||
$interface->withContentType(MsgResponse::CONTENT_TYPE_JSON);
|
$interface->withContentType(MsgResponse::CONTENT_TYPE_JSON);
|
||||||
}
|
}
|
||||||
$responseData = $interface->_toArray($responseData);
|
$responseData = $interface->_toArray($responseData);
|
||||||
if ($interface->getHeader('Content-Type') == MsgResponse::CONTENT_TYPE_XML) {
|
if ($interface->getContentType() == MsgResponse::CONTENT_TYPE_XML) {
|
||||||
$responseData = Help::toXml($responseData);
|
$interface->stream->write(Help::toXml($responseData));
|
||||||
}
|
} else if (is_array($responseData)) {
|
||||||
if (is_array($responseData)) {
|
|
||||||
$interface->stream->write(json_encode($responseData));
|
$interface->stream->write(json_encode($responseData));
|
||||||
} else {
|
} else {
|
||||||
$interface->stream->write((string)$responseData);
|
$interface->stream->write((string)$responseData);
|
||||||
|
|||||||
Reference in New Issue
Block a user