This commit is contained in:
2021-08-02 18:12:32 +08:00
parent 3fb79847ce
commit f1a7928192
6 changed files with 97 additions and 39 deletions
+5 -10
View File
@@ -5,11 +5,9 @@ namespace Snowflake;
use Exception;
use Reflection;
use ReflectionClass;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Snowflake\Exception\NotFindClassException;
defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implement ');
@@ -57,7 +55,6 @@ class Aop extends Component
* @param $handler
* @param $params
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
@@ -75,20 +72,18 @@ class Aop extends Component
}
/**
* @param array $handler
* @return ReflectionClass
* @throws NotFindClassException
* @throws ReflectionException
* @return IAspect
* @throws Exception
* @throws ReflectionException
*/
public function getAop(array $handler): ReflectionClass
public function getAop(array $handler): IAspect
{
$aopName = $handler[0]::class . '::' . $handler[1];
$reflect = Snowflake::getDi()->getReflect(current(static::$_aop[$aopName]));
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
$reflect = Snowflake::getDi()->get(current(static::$_aop[$aopName]));
if (!method_exists($reflect, 'invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class);
}
return $reflect;
+11
View File
@@ -70,6 +70,17 @@ trait Attributes
}
/**
* @param string $class
* @param string $method
* @return bool
*/
public function hasMethod(string $class, string $method): bool
{
return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]);
}
/**
* @param ReflectionClass $class
* @return array
+39 -9
View File
@@ -14,6 +14,7 @@ use Exception;
use JetBrains\PhpStorm\Pure;
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionMethod;
use ReflectionProperty;
use Snowflake\Abstracts\BaseObject;
@@ -280,26 +281,55 @@ class Container extends BaseObject
if (!($reflectMethod instanceof ReflectionMethod)) {
throw new ReflectionException("Class does not have a function $className::$method");
}
return $this->resolveMethodParameters($reflectMethod);
$className = $reflectMethod->getDeclaringClass()->getName();
if (isset($this->_parameters[$className]) && isset($this->_parameters[$className][$reflectMethod->getName()])) {
return $this->_parameters[$className][$reflectMethod->getName()];
}
return $this->setParameters($className, $reflectMethod->getName(), $this->resolveMethodParameters($reflectMethod));
}
/**
* @param ReflectionMethod $reflectionMethod
* @param $class
* @param $method
* @param $parameters
*/
private function setParameters($class, $method, $parameters)
{
if (!isset($this->_parameters[$class])) {
$this->_parameters[$class] = [];
}
if (!isset($this->_parameters[$class][$method])) {
$this->_parameters[$class][$method] = [];
}
return $this->_parameters[$class][$method] = $parameters;
}
/**
* @param \Closure $reflectionMethod
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveMethodParameters(ReflectionMethod $reflectionMethod): array
public function resolveFunctionParameters(\Closure $reflectionMethod): array
{
return $this->resolveMethodParameters(new ReflectionFunction($reflectionMethod));
}
/**
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveMethodParameters(ReflectionMethod|ReflectionFunction $reflectionMethod): array
{
$className = $reflectionMethod->getDeclaringClass()->getName();
if (isset($this->_parameters[$className]) && isset($this->_parameters[$className][$reflectionMethod->getName()])) {
return $this->_parameters[$className][$reflectionMethod->getName()];
}
if ($reflectionMethod->getNumberOfParameters() < 1) {
return [];
}
$this->_parameters[$className][$reflectionMethod->getName()] = $params = [];
$params = [];
foreach ($reflectionMethod->getParameters() as $key => $parameter) {
if ($parameter->isDefaultValueAvailable()) {
$params[$key] = $parameter->getDefaultValue();
@@ -317,7 +347,7 @@ class Container extends BaseObject
};
}
}
return $this->_parameters[$className][$reflectionMethod->getName()] = $params;
return $params;
}
+3 -2
View File
@@ -20,13 +20,14 @@ class LoggerAspect implements IAspect
/**
* @param mixed $handler
* @param array $params
* @return mixed
*/
public function invoke(mixed $handler): mixed
public function invoke(mixed $handler, array $params = []): mixed
{
$startTime = microtime(true);
$data = call_user_func($handler);
$data = call_user_func($handler, ...$params);
$this->print_runtime($handler, $startTime);
+6 -5
View File
@@ -8,10 +8,11 @@ interface IAspect
{
/**
* @return mixed|void
*/
public function invoke(mixed $handler);
/**
* @param mixed $handler
* @param array $params
* @return mixed
*/
public function invoke(mixed $handler, array $params = []): mixed;
}