改名
This commit is contained in:
@@ -122,7 +122,10 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
|||||||
*/
|
*/
|
||||||
public function json($data): 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);
|
->withContentType(self::CONTENT_TYPE_JSON);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +137,7 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
|||||||
*/
|
*/
|
||||||
public function html($data): 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);
|
->withContentType(self::CONTENT_TYPE_HTML);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,29 +149,24 @@ class Response implements ResponseInterface, \Server\ResponseInterface
|
|||||||
*/
|
*/
|
||||||
public function xml($data): 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);
|
->withContentType(self::CONTENT_TYPE_XML);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $responseData
|
* @param $responseData
|
||||||
* @param string $contentType
|
* @return string|array|bool|int|null
|
||||||
* @return Stream
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
public static function parser($responseData, $contentType = self::CONTENT_TYPE_JSON): Stream
|
public function _toArray($responseData): string|array|null|bool|int
|
||||||
{
|
{
|
||||||
if (is_object($responseData)) {
|
if (is_object($responseData)) {
|
||||||
$responseData = $responseData instanceof ToArray ? $responseData->toArray() : get_object_vars($responseData);
|
$responseData = $responseData instanceof ToArray ? $responseData->toArray() : get_object_vars($responseData);
|
||||||
}
|
}
|
||||||
if ($contentType == self::CONTENT_TYPE_JSON) {
|
return $responseData;
|
||||||
return new Stream(json_encode($responseData));
|
|
||||||
}
|
|
||||||
if ($contentType == self::CONTENT_TYPE_XML) {
|
|
||||||
return new Stream(Help::toXml($responseData));
|
|
||||||
}
|
|
||||||
return new Stream((string)($responseData));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Server\Service;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Http\Exception\RequestException;
|
use Http\Exception\RequestException;
|
||||||
use Http\Route\Node;
|
use Http\Route\Node;
|
||||||
use Kiri\ToArray;
|
use Kiri\Core\Help;
|
||||||
use Server\Events\OnAfterRequest;
|
use Server\Events\OnAfterRequest;
|
||||||
use Server\Message\Stream;
|
use Server\Message\Stream;
|
||||||
use Server\ResponseInterface;
|
use Server\ResponseInterface;
|
||||||
@@ -16,6 +16,7 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -24,38 +25,38 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
|||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
*/
|
*/
|
||||||
public function onConnect(Server $server, int $fd): void
|
public function onConnect(Server $server, int $fd): void
|
||||||
{
|
{
|
||||||
// TODO: Implement onConnect() method.
|
// TODO: Implement onConnect() method.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param Response $response
|
||||||
*/
|
*/
|
||||||
public function onRequest(Request $request, Response $response): void
|
public function onRequest(Request $request, Response $response): void
|
||||||
{
|
{
|
||||||
// TODO: Implement onRequest() method.
|
// TODO: Implement onRequest() method.
|
||||||
try {
|
try {
|
||||||
$node = $this->router->Branch_search(\Server\Constrict\Request::create($request));
|
$node = $this->router->Branch_search(\Server\Constrict\Request::create($request));
|
||||||
if (!($node instanceof Node)) {
|
if (!($node instanceof Node)) {
|
||||||
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
||||||
}
|
}
|
||||||
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
|
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
|
||||||
$responseData = $this->transferToResponse($responseData);
|
$responseData = $this->transferToResponse($responseData);
|
||||||
}
|
}
|
||||||
} catch (Error | \Throwable $exception) {
|
} catch (Error | \Throwable $exception) {
|
||||||
$responseData = $this->exceptionHandler->emit($exception, $this->response);
|
$responseData = $this->exceptionHandler->emit($exception, $this->response);
|
||||||
} finally {
|
} finally {
|
||||||
$this->responseEmitter->sender($response, $responseData);
|
$this->responseEmitter->sender($response, $responseData);
|
||||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,45 +64,42 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
|||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function transferToResponse($responseData): ResponseInterface
|
private function transferToResponse($responseData): ResponseInterface
|
||||||
{
|
{
|
||||||
$interface = $this->response->withStatus(200);
|
$interface = $this->response->withStatus(200);
|
||||||
if (is_object($responseData)) {
|
if (!$interface->hasHeader('Content-Type')) {
|
||||||
if (!($responseData instanceof ToArray)) {
|
$interface->withContentType(MsgResponse::CONTENT_TYPE_JSON);
|
||||||
$responseData = get_object_vars($responseData);
|
}
|
||||||
} else {
|
$responseData = $interface->_toArray($responseData);
|
||||||
$responseData = $responseData->toArray();
|
if ($interface->getHeader('Content-Type') == MsgResponse::CONTENT_TYPE_XML) {
|
||||||
}
|
$responseData = Help::toXml($responseData);
|
||||||
}
|
}
|
||||||
if (is_array($responseData)) {
|
if (is_array($responseData)) {
|
||||||
$responseData = new Stream(json_encode($responseData, JSON_UNESCAPED_UNICODE));
|
$responseData = new Stream(json_encode($responseData, JSON_UNESCAPED_UNICODE));
|
||||||
} else {
|
} else {
|
||||||
$responseData = new Stream((string)$responseData);
|
$responseData = new Stream((string)$responseData);
|
||||||
}
|
}
|
||||||
if (!$interface->hasHeader('Content-Type')) {
|
return $interface->withBody($responseData);
|
||||||
$interface->withContentType(\Server\Message\Response::CONTENT_TYPE_JSON);
|
}
|
||||||
}
|
|
||||||
return $interface->withBody($responseData);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onDisconnect(Server $server, int $fd): void
|
public function onDisconnect(Server $server, int $fd): void
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Server $server
|
* @param Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onClose(Server $server, int $fd): void
|
public function onClose(Server $server, int $fd): void
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user