feat : Decorator Aop

This commit is contained in:
2025-09-06 23:53:15 +08:00
parent aaa394a2a7
commit dfbb4b73ae
9 changed files with 195 additions and 9 deletions

View File

@@ -0,0 +1,31 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
* @web_site https://ctexthuang.com
*/
declare(strict_types=1);
namespace App\Service\Test\Decorator\Aop;
use App\Interface\Test\Decorator\UserServiceInterface;
class UserService implements UserServiceInterface
{
/**
* @param int $userId
* @return array
*/
public function getUserInfo(int $userId): array
{
// 模拟数据库查询
return [
'id' => $userId,
'name' => 'User ' . $userId,
'email' => 'user' . $userId . '@ctexthuang.com'
];
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}