hyperf-gateway/app/Aspect/ResponseMiddlewareAspect.php
2022-11-04 15:37:04 +08:00

34 lines
1.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Aspect;
use App\Helper\Result;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\HttpServer\CoreMiddleware;
use Hyperf\Paginator\Paginator;
#[Aspect]
class ResponseMiddlewareAspect extends AbstractAspect
{
// 要切入的类或 Trait可以多个亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配
public $classes = [
CoreMiddleware::class . '::transferToResponse'
];
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$response = $proceedingJoinPoint->arguments['keys']['response'];
if ($response === null || is_array($response)) {
$proceedingJoinPoint->arguments['keys']['response'] = Result::result($response);
} else if ($response instanceof Paginator) {
$proceedingJoinPoint->arguments['keys']['response'] = Result::result($response->toArray());
}
return $proceedingJoinPoint->process();
}
}