feat : Proxy Subject and Database
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user