Skip to content

Commit

Permalink
支持方法定义PreCall
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Mar 7, 2024
1 parent fc1d0de commit 8d175ed
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/AnnotationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use EasySwoole\Http\ReflectionCache;
use EasySwoole\Http\Request;
use EasySwoole\HttpAnnotation\Attributes\Param;
use EasySwoole\HttpAnnotation\Attributes\PreCall;
use EasySwoole\HttpAnnotation\Attributes\Property\Context;
use EasySwoole\HttpAnnotation\Attributes\Property\Di;
use EasySwoole\HttpAnnotation\Attributes\Property\Inject;
Expand Down Expand Up @@ -79,6 +80,21 @@ public function __hook(?array $actionArg = [],?array $onRequestArg = null)
$this->onException($exception);
return ;
}
try {
$preCalls = AttributeCache::getInstance()->getClassMethodPreCallTag(static::class,$this->getActionName());
if($preCalls){
/** @var PreCall $call */
foreach ($preCalls as $call){
$ret = call_user_func($call->call,$this->request(),$this->response(),$this);
if($ret === false){
return;
}
}
}
}catch (\Throwable $exception){
$this->onException($exception);
return ;
}
/*
* $onRequestArg 是全部定义的参数,而$actionArg 是方法定义参数
*/
Expand Down
40 changes: 40 additions & 0 deletions src/AttributeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EasySwoole\Http\ReflectionCache;
use EasySwoole\HttpAnnotation\Attributes\Api;
use EasySwoole\HttpAnnotation\Attributes\ApiGroup;
use EasySwoole\HttpAnnotation\Attributes\PreCall;
use EasySwoole\HttpAnnotation\Enum\HttpMethod;
use EasySwoole\HttpAnnotation\Exception\Annotation;
use EasySwoole\HttpAnnotation\Exception\RequestMethodNotAllow;
Expand All @@ -23,6 +24,8 @@ class AttributeCache

protected array $classMethodApiTags = [];

protected array $classMethodPreCallTags = [];

function setClassActionParams(string $class, $action, array $data):void
{
$key = md5($class);
Expand Down Expand Up @@ -89,4 +92,41 @@ function getClassMethodApiTag(string $class,string $action)
}
return null;
}

function getClassMethodPreCallTag(string $class,string $action):?array
{
$key = md5($class);
if(isset($this->classMethodPreCallTags[$key][$action])){
if(is_array($this->classMethodPreCallTags[$key][$action])){
return $this->classMethodPreCallTags[$key][$action];
}
return null;
}
$class = ReflectionCache::getInstance()->getClassReflection($class);
$ref = ReflectionCache::getInstance()->allowMethodReflections($class);
if(!isset($ref[$action])){
return null;
}
/** @var \ReflectionMethod $ref */
$ref = $ref[$action];
$actionPreCallTags = $ref->getAttributes(PreCall::class);
if(!empty($actionPreCallTags)){
$final = [];
foreach ($actionPreCallTags as $callTag){
try{
$callTag = new Api(...$callTag->getArguments());
$final[] = $callTag;
}catch (\Throwable $exception){
$class = static::class;
$msg = "{$exception->getMessage()} in controller: {$class} method: {$action}";
throw new Annotation($msg);
}
}
$this->classMethodPreCallTags[$key][$action] = $final;
return $final;
}else{
$this->classMethodPreCallTags[$key][$action] = true;
}
return null;
}
}
15 changes: 15 additions & 0 deletions src/Attributes/PreCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace EasySwoole\HttpAnnotation\Attributes;

use EasySwoole\HttpAnnotation\Enum\HttpMethod;

#[\Attribute]
class PreCall
{
public $call;
function __construct(callable $call)
{
$this->call = $call;
}
}

0 comments on commit 8d175ed

Please sign in to comment.