From 66c656690ac26f67333da61f59ee7ac26b5fef77 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Wed, 21 Apr 2021 01:45:59 +0800 Subject: [PATCH] modify --- System/Aop.php | 107 ++++++++++++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 42 deletions(-) diff --git a/System/Aop.php b/System/Aop.php index 4a843226..61827fd3 100644 --- a/System/Aop.php +++ b/System/Aop.php @@ -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); + } + }