You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperf-gateway/app/Aspect/ResponseMiddlewareAspect.php

34 lines
1.0 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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