From 998f8d83563dcc9565f329eefa37ebd9e27f0827 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E4=B8=8D=E5=8F=AE=E5=92=9A?= Date: Wed, 24 Sep 2025 15:51:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=8D=E6=AC=A1=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/Aether/PHP/Hyperf/AbstractModel.php | 9 +++------ extend/Aether/PHP/Hyperf/AetherController.php | 6 +++--- extend/Aether/PHP/Hyperf/RpcExceptionHandler.php | 10 +++++++++- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/extend/Aether/PHP/Hyperf/AbstractModel.php b/extend/Aether/PHP/Hyperf/AbstractModel.php index f361260..eb4f8aa 100644 --- a/extend/Aether/PHP/Hyperf/AbstractModel.php +++ b/extend/Aether/PHP/Hyperf/AbstractModel.php @@ -60,9 +60,9 @@ abstract class AbstractModel extends AetherModel * @param array $columns * @return AbstractModel */ - public static function findOrFail(int $id, $columns = []): self + public static function findOrFail(mixed $id, $columns = []): self { - $model = self::find($id); + $model = self::find($id, $columns); if (!$model) { throw new ModelNotFoundException(); } @@ -74,10 +74,7 @@ abstract class AbstractModel extends AetherModel */ public static function createOne(array $data): self { - $model = new static(); - $model->fill($data); - $model->save(); - return $model; + return static::query()->create($data); } /** diff --git a/extend/Aether/PHP/Hyperf/AetherController.php b/extend/Aether/PHP/Hyperf/AetherController.php index 446097c..455c961 100644 --- a/extend/Aether/PHP/Hyperf/AetherController.php +++ b/extend/Aether/PHP/Hyperf/AetherController.php @@ -43,7 +43,7 @@ abstract class AetherController /** * 创建资源 (RESTFul: POST /resources) */ - public function store() + public function store(): array { $data = $this->request->all(); $id = $this->getService()->create($data); @@ -71,8 +71,8 @@ abstract class AetherController /** * 获取对应的服务类 - * @return \Aether\AbstractService + * @return AbstractService */ - abstract protected function getService(); + abstract protected function getService(): AbstractService; } diff --git a/extend/Aether/PHP/Hyperf/RpcExceptionHandler.php b/extend/Aether/PHP/Hyperf/RpcExceptionHandler.php index fb55e20..1cde0fa 100644 --- a/extend/Aether/PHP/Hyperf/RpcExceptionHandler.php +++ b/extend/Aether/PHP/Hyperf/RpcExceptionHandler.php @@ -4,14 +4,21 @@ declare(strict_types=1); namespace Aether; +use Hyperf\Context\ApplicationContext; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\Rpc\Protocol; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; use Throwable; use Hyperf\Context\Context; class RpcExceptionHandler extends ExceptionHandler { + /** + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface { // 微服务间调用返回更精简的错误信息 @@ -21,7 +28,8 @@ class RpcExceptionHandler extends ExceptionHandler 'request_id' => Context::get('request_id', ''), ]; - $protocol = make(Protocol::class); + $protocol = ApplicationContext::getContainer()->get(Protocol::class); + // $response->getBody()->write($protocol->pack($data)); $response->getBody()->write($protocol->pack($data)); return $response;