This commit is contained in:
2021-08-23 19:10:44 +08:00
parent 0744a4fac8
commit ebb23807cf
3 changed files with 41 additions and 19 deletions
+19 -1
View File
@@ -5,8 +5,8 @@ namespace Kiri;
use Exception;
use ReflectionException;
use Kiri\Abstracts\Component;
use ReflectionException;
defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implement ');
@@ -40,6 +40,24 @@ class AspectManager extends Component
}
/**
* @param string $class
* @param string $method
* @param string $aspect
*/
public function addAspect(string $class, string $method, string $aspect)
{
$alias = $class . '::' . $method;
if (!isset(static::$_aop[$alias])) {
static::$_aop[$alias] = [];
}
if (in_array($aspect, static::$_aop[$alias])) {
return;
}
static::$_aop[$alias][] = $aspect;
}
/**
* @param $handler
* @return bool
+15 -6
View File
@@ -70,8 +70,9 @@ trait Attributes
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $method
* @param mixed $instance
*/
private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method)
private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
{
$this->setMappingClass($attribute, $class);
@@ -80,7 +81,7 @@ trait Attributes
$mapping['method'] = [];
}
if (!in_array($method, $mapping['method'])) {
$mapping['method'][] = $method;
$mapping['method'][] = [$method => $instance];
}
$this->_mapping[$attribute->getName()][$class] = $mapping;
}
@@ -135,9 +136,11 @@ trait Attributes
if (!class_exists($attribute->getName())) {
continue;
}
$this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $attribute->newInstance();
$instance = $attribute->newInstance();
$this->setMappingMethod($attribute, $className, $ReflectionMethod->getName());
$this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
$this->setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
@@ -207,13 +210,19 @@ trait Attributes
* @param string|null $method
* @return array
*/
public function getMethodByAnnotation(string $attribute, string $class, string $method = null): array
public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
{
$class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['method']) || empty($method)) {
return $class['method'] ?? [];
}
return $class['method'][$method] ?? [];
foreach ($class['method'] as $value) {
$key = key($value);
if ($method == $value[$key]) {
return $value[$key];
}
}
return null;
}
+7 -12
View File
@@ -8,15 +8,15 @@ namespace Http\Route;
use Annotation\Aspect;
use Closure;
use Exception;
use Http\Exception\RequestException;
use Http\Context\Request;
use Http\Exception\RequestException;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Server\Events\OnAfterWorkerStart;
use Kiri\Events\EventProvider;
use Kiri\Exception\NotFindClassException;
use Kiri\IAspect;
use Kiri\Kiri;
use ReflectionException;
use Server\Events\OnAfterWorkerStart;
/**
* Class Node
@@ -112,7 +112,6 @@ class Node
/**
* @param string $handler
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function splitHandler(string $handler): array
@@ -144,7 +143,6 @@ class Node
/**
* @return HandlerProviders
* @throws NotFindClassException
* @throws ReflectionException
*/
private function getHandlerProviders(): HandlerProviders
@@ -235,16 +233,13 @@ class Node
private function getAop($handler): ?IAspect
{
[$controller, $action] = $handler;
$aspect = Kiri::getDi()->getMethodAttribute($controller::class, $action);
/** @var Aspect $aspect */
$aspect = Kiri::getDi()->getMethodByAnnotation(Aspect::class, $controller, $action);
if (empty($aspect)) {
return null;
}
foreach ($aspect as $value) {
if ($value instanceof Aspect) {
return di($value->aspect);
}
}
return null;
return di($aspect->aspect);
}