From 8974ee595b3411bac086c3046eb24ba44087ecc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Mon, 19 Jun 2023 18:15:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(handler):=20=E5=A2=9E=E5=8A=A0=E4=BA=86=20?= =?UTF-8?q?restful=20=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李东云 --- src/Exceptions/Handler/CommonHandler.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Exceptions/Handler/CommonHandler.php b/src/Exceptions/Handler/CommonHandler.php index f12ced0..018de1a 100644 --- a/src/Exceptions/Handler/CommonHandler.php +++ b/src/Exceptions/Handler/CommonHandler.php @@ -31,6 +31,7 @@ use Psr\Http\Message\ResponseInterface; use RedisException; use Singularity\HDK\Core\Constants\CommonErrorCode; use Singularity\HDK\Core\Exceptions\ValidateException; +use Singularity\HDK\Core\Service\ApiStyleService; use Symfony\Component\Mailer\Exception\TransportException; use Teapot\StatusCode\RFC\RFC4918; use Teapot\StatusCode\RFC\RFC7231; @@ -46,18 +47,15 @@ use Throwable; */ class CommonHandler extends ExceptionHandler { - /** - * - * @var RequestInterface|null - */ #[Inject(required: false)] private ?RequestInterface $request; - /** - * @var StdoutLogger - */ + #[Inject] private StdoutLogger $logger; + #[Inject] + private ApiStyleService $apiStyleService; + /** * {@inheritDoc} */ @@ -65,7 +63,8 @@ class CommonHandler extends ExceptionHandler { // 阻止异常冒泡 $this->stopPropagation(); - $restful = config('common.response.restful'); + $restful = $this->apiStyleService->get(); + // $restful = config('common.response.restful'); $code_name = config('common.response.code_name'); $message_name = config('common.response.message_name'); $is_testing = config('app_status') === true; @@ -243,7 +242,7 @@ TRACE: ERROR_LOG ); $data = Json::encode($data); - if ($restful) { + if ($restful === ApiStyleService::RESTFUL) { $response = $response->withStatus( $status_code ?? $throwable->status ?? $throwable->statusCode ?? RFC7231::INTERNAL_SERVER_ERROR );