feat : Proxy Subject and Database

This commit is contained in:
2025-09-07 11:37:13 +08:00
parent 7d489eecc4
commit 14ba4674ee
11 changed files with 367 additions and 0 deletions

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

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Interface\Test\Proxy;
interface DatabaseQueryInterface
{
/**
* 数据库查询接口
* @param string $query
* @return array
*/
public function execute(string $query): array;
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Interface\Test\Proxy;
interface SubjectInterface
{
/**
* @return void
*/
public function request(): void;
}

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

View 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;
}
}

View 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,
];
}
}

View 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'));
}
}

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

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