mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 18:17:49 +08:00
first commit
This commit is contained in:
51
app/Aspect/ResponseFormatAspect.php
Normal file
51
app/Aspect/ResponseFormatAspect.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Aspect;
|
||||
|
||||
use App\Annotation\ResponseFormat;
|
||||
use Hyperf\Di\Annotation\Aspect;
|
||||
use Hyperf\Di\Aop\AbstractAspect;
|
||||
use Hyperf\Di\Exception\Exception;
|
||||
use Hyperf\HttpServer\Request;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
|
||||
#[Aspect]
|
||||
class ResponseFormatAspect extends AbstractAspect
|
||||
{
|
||||
/**
|
||||
* @var array|class-string[]
|
||||
*/
|
||||
public array $annotations = [
|
||||
ResponseFormat::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(protected ContainerInterface $container) {}
|
||||
|
||||
/**
|
||||
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
||||
{
|
||||
// 获取注解定义的格式
|
||||
$annotation = $proceedingJoinPoint->getAnnotationMetadata()->class[ResponseFormat::class]
|
||||
?? $proceedingJoinPoint->getAnnotationMetadata()->method[ResponseFormat::class] ?? null;
|
||||
|
||||
if ($annotation) {
|
||||
// 将注解格式存入请求属性(覆盖中间件的默认值)
|
||||
$request = $proceedingJoinPoint->arguments['request'] ?? null;
|
||||
if ($request instanceof Request) {
|
||||
$request->withAttribute('response_format', $annotation->format);
|
||||
}
|
||||
}
|
||||
|
||||
return $proceedingJoinPoint->process();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user