diff --git a/http-server/Message/Response.php b/http-server/Message/Response.php index dffa4605..243ce208 100644 --- a/http-server/Message/Response.php +++ b/http-server/Message/Response.php @@ -122,7 +122,10 @@ class Response implements ResponseInterface, \Server\ResponseInterface */ public function json($data): ResponseInterface { - return $this->withBody(static::parser($data, self::CONTENT_TYPE_JSON)) + if (!is_array($data = $this->_toArray($data))) { + throw new Exception('Json data format error.'); + } + return $this->withBody(new Stream(json_encode($this->_toArray($data)))) ->withContentType(self::CONTENT_TYPE_JSON); } @@ -134,7 +137,7 @@ class Response implements ResponseInterface, \Server\ResponseInterface */ public function html($data): ResponseInterface { - return $this->withBody(static::parser($data, self::CONTENT_TYPE_HTML)) + return $this->withBody(new Stream((string)$this->_toArray($data))) ->withContentType(self::CONTENT_TYPE_HTML); } @@ -146,29 +149,24 @@ class Response implements ResponseInterface, \Server\ResponseInterface */ public function xml($data): ResponseInterface { - return $this->withBody(static::parser($data, self::CONTENT_TYPE_XML)) + if (!is_array($data = $this->_toArray($data))) { + throw new Exception('Xml data format error.'); + } + return $this->withBody(new Stream(Help::toXml($data))) ->withContentType(self::CONTENT_TYPE_XML); } /** * @param $responseData - * @param string $contentType - * @return Stream - * @throws Exception + * @return string|array|bool|int|null */ - public static function parser($responseData, $contentType = self::CONTENT_TYPE_JSON): Stream + public function _toArray($responseData): string|array|null|bool|int { if (is_object($responseData)) { $responseData = $responseData instanceof ToArray ? $responseData->toArray() : get_object_vars($responseData); } - if ($contentType == self::CONTENT_TYPE_JSON) { - return new Stream(json_encode($responseData)); - } - if ($contentType == self::CONTENT_TYPE_XML) { - return new Stream(Help::toXml($responseData)); - } - return new Stream((string)($responseData)); + return $responseData; } } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 942a58ab..a07c1848 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -6,7 +6,7 @@ namespace Server\Service; use Exception; use Http\Exception\RequestException; use Http\Route\Node; -use Kiri\ToArray; +use Kiri\Core\Help; use Server\Events\OnAfterRequest; use Server\Message\Stream; use Server\ResponseInterface; @@ -16,6 +16,7 @@ use Swoole\Error; use Swoole\Http\Request; use Swoole\Http\Response; use Swoole\Server; +use Server\Message\Response as MsgResponse; /** * @@ -24,38 +25,38 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect { - /** - * @param Server $server - * @param int $fd - */ - public function onConnect(Server $server, int $fd): void - { - // TODO: Implement onConnect() method. - } + /** + * @param Server $server + * @param int $fd + */ + public function onConnect(Server $server, int $fd): void + { + // TODO: Implement onConnect() method. + } - /** - * @param Request $request - * @param Response $response - */ - public function onRequest(Request $request, Response $response): void - { - // TODO: Implement onRequest() method. - try { - $node = $this->router->Branch_search(\Server\Constrict\Request::create($request)); - if (!($node instanceof Node)) { - throw new RequestException('