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 array $handler
|
||||||
* @param string $aspect
|
* @param string $aspect
|
||||||
*/
|
*/
|
||||||
public function aop_add(array $handler, string $aspect)
|
public function aop_add(array $handler, string $aspect)
|
||||||
{
|
{
|
||||||
[$class, $method] = $handler;
|
[$class, $method] = $handler;
|
||||||
$alias = get_class($class) . '::' . $method;
|
$alias = get_class($class) . '::' . $method;
|
||||||
if (!isset($this->_aop[$alias])) {
|
if (!isset($this->_aop[$alias])) {
|
||||||
$this->_aop[$alias] = [];
|
$this->_aop[$alias] = [];
|
||||||
}
|
}
|
||||||
if (in_array($aspect, $this->_aop[$alias])) {
|
if (in_array($aspect, $this->_aop[$alias])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->_aop[$alias][] = $aspect;
|
$this->_aop[$alias][] = $aspect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
final public function dispatch($handler, ...$params): mixed
|
final public function dispatch($handler, $params): mixed
|
||||||
{
|
{
|
||||||
if ($handler instanceof \Closure) {
|
if ($handler instanceof \Closure) {
|
||||||
return call_user_func($handler, ...$params);
|
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')) {
|
* @param $handler
|
||||||
throw new Exception(ASPECT_ERROR . IAspect::class);
|
* @param $params
|
||||||
}
|
* @param $aopName
|
||||||
$method = $reflect->getMethod('invoke');
|
* @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