modify
This commit is contained in:
+65
-42
@@ -20,55 +20,78 @@ class Aop extends Component
|
||||
{
|
||||
|
||||
|
||||
private array $_aop = [];
|
||||
private array $_aop = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $handler
|
||||
* @param string $aspect
|
||||
*/
|
||||
public function aop_add(array $handler, string $aspect)
|
||||
{
|
||||
[$class, $method] = $handler;
|
||||
$alias = get_class($class) . '::' . $method;
|
||||
if (!isset($this->_aop[$alias])) {
|
||||
$this->_aop[$alias] = [];
|
||||
}
|
||||
if (in_array($aspect, $this->_aop[$alias])) {
|
||||
return;
|
||||
}
|
||||
$this->_aop[$alias][] = $aspect;
|
||||
}
|
||||
/**
|
||||
* @param array $handler
|
||||
* @param string $aspect
|
||||
*/
|
||||
public function aop_add(array $handler, string $aspect)
|
||||
{
|
||||
[$class, $method] = $handler;
|
||||
$alias = get_class($class) . '::' . $method;
|
||||
if (!isset($this->_aop[$alias])) {
|
||||
$this->_aop[$alias] = [];
|
||||
}
|
||||
if (in_array($aspect, $this->_aop[$alias])) {
|
||||
return;
|
||||
}
|
||||
$this->_aop[$alias][] = $aspect;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function dispatch($handler, ...$params): mixed
|
||||
{
|
||||
if ($handler instanceof \Closure) {
|
||||
return call_user_func($handler, ...$params);
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
final public function dispatch($handler, $params): mixed
|
||||
{
|
||||
if ($handler instanceof \Closure) {
|
||||
return call_user_func($handler, ...$params);
|
||||
}
|
||||
$aopName = get_class($handler[0]) . '::' . $handler[1];
|
||||
if (!isset($this->_aop[$aopName])) {
|
||||
return $this->notFound($handler, $params);
|
||||
}
|
||||
return $this->invoke($handler, $params, $aopName);
|
||||
}
|
||||
|
||||
$aopName = get_class($handler) . '::' . $handler;
|
||||
if (!isset($this->_aop[$aopName])) {
|
||||
if (!method_exists($handler[0], $handler[1])) {
|
||||
return response()->close(404);
|
||||
}
|
||||
return call_user_func($handler, ...$params);
|
||||
}
|
||||
|
||||
$reflect = Snowflake::getDi()->getReflect(current($this->_aop[$aopName]));
|
||||
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
|
||||
throw new Exception(ASPECT_ERROR . IAspect::class);
|
||||
}
|
||||
$method = $reflect->getMethod('invoke');
|
||||
/**
|
||||
* @param $handler
|
||||
* @param $params
|
||||
* @param $aopName
|
||||
* @return mixed
|
||||
* @throws \ReflectionException
|
||||
* @throws \Snowflake\Exception\NotFindClassException
|
||||
*/
|
||||
private function invoke($handler, $params, $aopName)
|
||||
{
|
||||
$reflect = Snowflake::getDi()->getReflect(current($this->_aop[$aopName]));
|
||||
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
|
||||
throw new Exception(ASPECT_ERROR . IAspect::class);
|
||||
}
|
||||
$method = $reflect->getMethod('invoke');
|
||||
|
||||
return $method->invokeArgs($reflect->newInstance($handler), $params);
|
||||
}
|
||||
return $method->invokeArgs($reflect->newInstance($handler), $params);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $handler
|
||||
* @param $params
|
||||
* @return false|mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function notFound($handler, $params)
|
||||
{
|
||||
if (!method_exists($handler[0], $handler[1])) {
|
||||
return response()->close(404);
|
||||
}
|
||||
return call_user_func($handler, ...$params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user