feat : Proxy Subject and Database
This commit is contained in:
36
app/Controller/Test/ProxyController.php
Normal file
36
app/Controller/Test/ProxyController.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Controller\Test;
|
||||||
|
|
||||||
|
use App\Service\Test\Proxy\BasicSubjectService;
|
||||||
|
use App\Service\Test\Proxy\DynamicProxyService;
|
||||||
|
use Hyperf\HttpServer\Annotation\Controller;
|
||||||
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
|
||||||
|
#[Controller(prefix: 'proxy/test')]
|
||||||
|
class ProxyController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
#[RequestMapping(path: 'basic', methods: 'GET')]
|
||||||
|
public function basic(): array
|
||||||
|
{
|
||||||
|
return (new BasicSubjectService)->handle();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
#[RequestMapping(path: 'dynamic', methods: 'GET')]
|
||||||
|
public function dynamic(): array
|
||||||
|
{
|
||||||
|
return (new DynamicProxyService)->handle();
|
||||||
|
}
|
||||||
|
}
|
||||||
13
app/Interface/Test/Proxy/DatabaseQueryInterface.php
Normal file
13
app/Interface/Test/Proxy/DatabaseQueryInterface.php
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Interface\Test\Proxy;
|
||||||
|
|
||||||
|
interface DatabaseQueryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 数据库查询接口
|
||||||
|
* @param string $query
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function execute(string $query): array;
|
||||||
|
}
|
||||||
11
app/Interface/Test/Proxy/SubjectInterface.php
Normal file
11
app/Interface/Test/Proxy/SubjectInterface.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Interface\Test\Proxy;
|
||||||
|
|
||||||
|
interface SubjectInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function request(): void;
|
||||||
|
}
|
||||||
32
app/Service/Test/Proxy/BasicSubjectService.php
Normal file
32
app/Service/Test/Proxy/BasicSubjectService.php
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
<?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\Proxy;
|
||||||
|
|
||||||
|
use App\Service\Test\Proxy\Subject\ProxyService;
|
||||||
|
use App\Service\Test\Proxy\Subject\RealSubjectService;
|
||||||
|
use App\Service\Test\TestBaseService;
|
||||||
|
use function Hyperf\Support\make;
|
||||||
|
|
||||||
|
class BasicSubjectService extends TestBaseService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function handle(): array
|
||||||
|
{
|
||||||
|
$proxy = new ProxyService(make(RealSubjectService::class));
|
||||||
|
|
||||||
|
$proxy->request();
|
||||||
|
|
||||||
|
return $this->return->success();
|
||||||
|
}
|
||||||
|
}
|
||||||
60
app/Service/Test/Proxy/Dynamic/DatabaseQueryProxyHandler.php
Normal file
60
app/Service/Test/Proxy/Dynamic/DatabaseQueryProxyHandler.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?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\Proxy\Dynamic;
|
||||||
|
|
||||||
|
use App\Interface\Test\Decorator\LoggerInterface;
|
||||||
|
use App\Interface\Test\Proxy\DatabaseQueryInterface;
|
||||||
|
|
||||||
|
class DatabaseQueryProxyHandler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
private LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var DatabaseQueryInterface
|
||||||
|
*/
|
||||||
|
private DatabaseQueryInterface $realService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param DatabaseQueryInterface $realService
|
||||||
|
* @param LoggerInterface $logger
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
DatabaseQueryInterface $realService,
|
||||||
|
LoggerInterface $logger
|
||||||
|
)
|
||||||
|
{
|
||||||
|
$this->realService = $realService;
|
||||||
|
$this->logger = $logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param DatabaseQueryInterface $target
|
||||||
|
* @param string $method
|
||||||
|
* @param array $parameters
|
||||||
|
* @param LoggerInterface $logger
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function execute(string $query): array
|
||||||
|
{
|
||||||
|
$this->logger->log('Before executing query'.PHP_EOL);
|
||||||
|
|
||||||
|
// 调用原始方法
|
||||||
|
$result = $this->realService->execute($query);
|
||||||
|
|
||||||
|
$this->logger->log('After executing query'.PHP_EOL);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
40
app/Service/Test/Proxy/Dynamic/RealDatabaseQueryService.php
Normal file
40
app/Service/Test/Proxy/Dynamic/RealDatabaseQueryService.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?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\Proxy\Dynamic;
|
||||||
|
|
||||||
|
use App\Interface\Test\Decorator\LoggerInterface;
|
||||||
|
use App\Interface\Test\Proxy\DatabaseQueryInterface;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
|
|
||||||
|
class RealDatabaseQueryService implements DatabaseQueryInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public function execute(string $query): array
|
||||||
|
{
|
||||||
|
$this->logger->log('Executing query: ' . $query . PHP_EOL);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'result' => 'data',
|
||||||
|
'query' => $query,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
44
app/Service/Test/Proxy/DynamicProxyService.php
Normal file
44
app/Service/Test/Proxy/DynamicProxyService.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?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\Proxy;
|
||||||
|
|
||||||
|
use App\Interface\Test\Decorator\LoggerInterface;
|
||||||
|
use App\Service\Test\Proxy\Dynamic\DatabaseQueryProxyHandler;
|
||||||
|
use App\Service\Test\Proxy\Dynamic\RealDatabaseQueryService;
|
||||||
|
use App\Service\Test\TestBaseService;
|
||||||
|
use Hyperf\Context\ApplicationContext;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
|
||||||
|
|
||||||
|
class DynamicProxyService extends TestBaseService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
public function handle(): array
|
||||||
|
{
|
||||||
|
$container = ApplicationContext::getContainer();
|
||||||
|
|
||||||
|
// 获取真实服务
|
||||||
|
$realService = $container->get(RealDatabaseQueryService::class);
|
||||||
|
|
||||||
|
$proxy = new DatabaseQueryProxyHandler(
|
||||||
|
$realService,
|
||||||
|
$container->get(LoggerInterface::class),
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->return->success('数据查询成功',$proxy->execute('SELECT * FROM user'));
|
||||||
|
}
|
||||||
|
}
|
||||||
67
app/Service/Test/Proxy/Subject/ProxyService.php
Normal file
67
app/Service/Test/Proxy/Subject/ProxyService.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?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\Proxy\Subject;
|
||||||
|
|
||||||
|
use App\Interface\Test\Decorator\LoggerInterface;
|
||||||
|
use App\Interface\Test\Proxy\SubjectInterface;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
|
class ProxyService implements SubjectInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var RealSubjectService
|
||||||
|
*/
|
||||||
|
protected RealSubjectService $realSubjectService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注入真实类
|
||||||
|
* @param RealSubjectService $realSubjectService
|
||||||
|
*/
|
||||||
|
public function __construct(RealSubjectService $realSubjectService)
|
||||||
|
{
|
||||||
|
$this->realSubjectService = $realSubjectService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function request(): void
|
||||||
|
{
|
||||||
|
if (!$this->checkAccess()) return;
|
||||||
|
|
||||||
|
$this->realSubjectService->request();
|
||||||
|
$this->logAcces();
|
||||||
|
|
||||||
|
if ($this->checkAccess()) return;
|
||||||
|
$this->logAcces();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true
|
||||||
|
*/
|
||||||
|
private function checkAccess()
|
||||||
|
{
|
||||||
|
$this->logger->log('Proxy: Checking access prior to firing a real request'.PHP_EOL);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function logAcces()
|
||||||
|
{
|
||||||
|
$this->logger->log('Proxy: Logging the time of request'.PHP_EOL);
|
||||||
|
}
|
||||||
|
}
|
||||||
34
app/Service/Test/Proxy/Subject/RealSubjectService.php
Normal file
34
app/Service/Test/Proxy/Subject/RealSubjectService.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?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\Proxy\Subject;
|
||||||
|
|
||||||
|
use App\Interface\Test\Decorator\LoggerInterface;
|
||||||
|
use App\Interface\Test\Proxy\SubjectInterface;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
|
class RealSubjectService implements SubjectInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var LoggerInterface
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function request(): void
|
||||||
|
{
|
||||||
|
$res = 'RealSubjectService: Handling request...'.PHP_EOL;
|
||||||
|
$this->logger->log($res);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
document/proxy.md
Normal file
20
document/proxy.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
|
||||||
|
### 代理模式 (Proxy Pattern) 详解
|
||||||
|
|
||||||
|
> 代理模式是一种结构型设计模式,它允许你提供一个代理对象来控制对另一个对象的访问。代理可以在不修改原始对象的情况下,增强或控制对它的访问。
|
||||||
|
|
||||||
|
#### 核心概念
|
||||||
|
|
||||||
|
装饰器模式主要解决以下问题:
|
||||||
|
|
||||||
|
- 控制访问:代理可以决定是否允许客户端访问目标对象(如权限控制)。
|
||||||
|
- 增强功能:代理可以在调用目标对象前后添加额外逻辑(如缓存、日志、延迟加载)。
|
||||||
|
- 远程访问:代理可以代表远程对象(如RPC、数据库访问)。
|
||||||
|
- 虚拟代理:代理可以延迟创建开销大的对象(如图片懒加载)。
|
||||||
|
|
||||||
|
#### 常见应用场景
|
||||||
|
- Spring AOP:通过动态代理实现切面编程
|
||||||
|
- MyBatis:Mapper接口的代理实现
|
||||||
|
- RPC框架:远程服务调用的本地代理
|
||||||
|
- 图片懒加载:先显示缩略图代理,点击再加载原图
|
||||||
@@ -30,4 +30,14 @@ Content-Type: application/x-www-form-urlencoded
|
|||||||
|
|
||||||
### Decorator http test
|
### Decorator http test
|
||||||
GET {{host}}/decorator/test/http
|
GET {{host}}/decorator/test/http
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
|
||||||
|
### Proxy basic test
|
||||||
|
GET {{host}}/proxy/test/basic
|
||||||
|
Content-Type: application/x-www-form-urlencoded
|
||||||
|
|
||||||
|
|
||||||
|
### Proxy dynamic test
|
||||||
|
GET {{host}}/proxy/test/dynamic
|
||||||
Content-Type: application/x-www-form-urlencoded
|
Content-Type: application/x-www-form-urlencoded
|
||||||
Reference in New Issue
Block a user