From dfbb4b73aecd8b081025571b8514448ea300ce70 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Sat, 6 Sep 2025 23:53:15 +0800 Subject: [PATCH] feat : Decorator Aop --- app/Aspect/CommonReturnAspect.php | 16 ++++-- .../Test/Decorator/Aop/CacheableAspect.php | 54 +++++++++++++++++++ .../Test/Decorator/Aop/LoggableAspect.php | 49 +++++++++++++++++ app/Controller/Test/DecoratorController.php | 8 ++- .../Test/Decorator/UserServiceInterface.php | 13 +++++ .../Test/Decorator/Aop/UserService.php | 31 +++++++++++ app/Service/Test/Decorator/AopService.php | 17 +++++- app/Service/Test/TestBaseService.php | 7 +++ request/test.http | 9 +++- 9 files changed, 195 insertions(+), 9 deletions(-) create mode 100644 app/Aspect/Test/Decorator/Aop/CacheableAspect.php create mode 100644 app/Aspect/Test/Decorator/Aop/LoggableAspect.php create mode 100644 app/Interface/Test/Decorator/UserServiceInterface.php create mode 100644 app/Service/Test/Decorator/Aop/UserService.php diff --git a/app/Aspect/CommonReturnAspect.php b/app/Aspect/CommonReturnAspect.php index 2a890db..d63b63e 100644 --- a/app/Aspect/CommonReturnAspect.php +++ b/app/Aspect/CommonReturnAspect.php @@ -2,11 +2,13 @@ namespace App\Aspect; +use App\Interface\Test\Decorator\LoggerInterface; use App\Lib\Return\AdminReturn; use App\Lib\Return\CommonReturn; use App\Lib\Return\TestReturn; use Hyperf\Context\Context; use Hyperf\Di\Annotation\Aspect; +use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Di\Exception\Exception; @@ -31,6 +33,12 @@ class CommonReturnAspect extends AbstractAspect */ public array $annotations = []; + /** + * @var LoggerInterface + */ + #[Inject] + protected LoggerInterface $logger; + /** * 依赖注入容器 * @param RequestInterface $request @@ -49,8 +57,6 @@ class CommonReturnAspect extends AbstractAspect */ public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed { - echo 1; - var_dump(1); // 在调用前进行处理 $result = $proceedingJoinPoint->process(); // 在调用后进行处理 @@ -70,8 +76,8 @@ class CommonReturnAspect extends AbstractAspect */ private function writeResponseLog(string $content): void { - echo json_encode($this->request->all()); - echo $this->userId; - echo json_encode($content); + $this->logger->log('返回切入请求体:'.json_encode($this->request->all())); + $this->logger->log('返回切入用户id:'.$this->userId); + $this->logger->log('返回切入返回体:'.$content); } } \ No newline at end of file diff --git a/app/Aspect/Test/Decorator/Aop/CacheableAspect.php b/app/Aspect/Test/Decorator/Aop/CacheableAspect.php new file mode 100644 index 0000000..e0e557b --- /dev/null +++ b/app/Aspect/Test/Decorator/Aop/CacheableAspect.php @@ -0,0 +1,54 @@ +arguments['keys']['userId']; + print_r('切入cache数据:'.json_encode($proceedingJoinPoint->arguments)); + + $cacheKey = 'list:userInfo:' . $userId; + $cache = ApplicationContext::getContainer()->get(Cache::class); + + if ($cache->has($cacheKey)) return $cache->get($cacheKey); + + $result = $proceedingJoinPoint->process(); + + $cache->set($cacheKey, $result, 3600); + + return $result; + } +} \ No newline at end of file diff --git a/app/Aspect/Test/Decorator/Aop/LoggableAspect.php b/app/Aspect/Test/Decorator/Aop/LoggableAspect.php new file mode 100644 index 0000000..7bf60e1 --- /dev/null +++ b/app/Aspect/Test/Decorator/Aop/LoggableAspect.php @@ -0,0 +1,49 @@ +arguments['keys']['userId']; + +// $logger = ApplicationContext::getContainer()->get(LoggerFactory::class)->get('app'); + print_r('切入logger数据:'.json_encode($proceedingJoinPoint->arguments)); + $this->logger->debug('调用前获取用户信息的ID' . $userId); + + $result = $proceedingJoinPoint->process(); + + $this->logger->debug('调用后检索用户信息的ID' . $userId); + + return $result; + } +} \ No newline at end of file diff --git a/app/Controller/Test/DecoratorController.php b/app/Controller/Test/DecoratorController.php index e4945a3..93f965e 100644 --- a/app/Controller/Test/DecoratorController.php +++ b/app/Controller/Test/DecoratorController.php @@ -26,6 +26,7 @@ class DecoratorController extends AbstractController } /** + * container 装饰器(依赖注入) * @return array */ #[RequestMapping(path: 'container', methods: 'GET')] @@ -34,7 +35,12 @@ class DecoratorController extends AbstractController return (new ContainerService)->handle(); } - public function aop() + /** + * aop 装饰器(切面) + * @return array + */ + #[RequestMapping(path: 'aop', methods: 'GET')] + public function aop(): array { return (new AopService)->handle(); } diff --git a/app/Interface/Test/Decorator/UserServiceInterface.php b/app/Interface/Test/Decorator/UserServiceInterface.php new file mode 100644 index 0000000..2d1ed93 --- /dev/null +++ b/app/Interface/Test/Decorator/UserServiceInterface.php @@ -0,0 +1,13 @@ + $userId, + 'name' => 'User ' . $userId, + 'email' => 'user' . $userId . '@ctexthuang.com' + ]; + } +} \ No newline at end of file diff --git a/app/Service/Test/Decorator/AopService.php b/app/Service/Test/Decorator/AopService.php index 1ba033e..0fff141 100644 --- a/app/Service/Test/Decorator/AopService.php +++ b/app/Service/Test/Decorator/AopService.php @@ -11,12 +11,25 @@ declare(strict_types=1); namespace App\Service\Test\Decorator; +use App\Service\Test\Decorator\Aop\UserService; use App\Service\Test\TestBaseService; +use Hyperf\Di\Annotation\Inject; class AopService extends TestBaseService { - public function handle() + /** + * @var UserService + */ + #[Inject] + protected UserService $userService; + + /** + * @return array + */ + public function handle(): array { - return $this->return->success(); + $userInfo = $this->userService->getUserInfo($this->request->input('user_id',1)); + + return $this->return->success('获取用户信息成功', $userInfo); } } \ No newline at end of file diff --git a/app/Service/Test/TestBaseService.php b/app/Service/Test/TestBaseService.php index f6aeb8f..719ca79 100644 --- a/app/Service/Test/TestBaseService.php +++ b/app/Service/Test/TestBaseService.php @@ -5,6 +5,7 @@ namespace App\Service\Test; use App\Lib\Return\TestReturn; use App\Service\BaseService; use Hyperf\Di\Annotation\Inject; +use Hyperf\HttpServer\Contract\RequestInterface; abstract class TestBaseService extends BaseService { @@ -14,5 +15,11 @@ abstract class TestBaseService extends BaseService #[Inject] protected TestReturn $return; + /** + * @var RequestInterface + */ + #[Inject] + protected RequestInterface $request; + abstract public function handle(); } \ No newline at end of file diff --git a/request/test.http b/request/test.http index 8148e59..519e16d 100644 --- a/request/test.http +++ b/request/test.http @@ -20,4 +20,11 @@ Content-Type: application/x-www-form-urlencoded ### Decorator container test GET {{host}}/decorator/test/container -Content-Type: application/x-www-form-urlencoded \ No newline at end of file +Content-Type: application/x-www-form-urlencoded + + +### Decorator aop test +GET {{host}}/decorator/test/aop +Content-Type: application/x-www-form-urlencoded + +user_id=12 \ No newline at end of file