From db2db63ee74d28a30b8d5c7953580bba7bd4249f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 19 Apr 2021 14:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Aop.php | 64 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/System/Aop.php b/System/Aop.php index 76d3db65..7403ab52 100644 --- a/System/Aop.php +++ b/System/Aop.php @@ -20,25 +20,25 @@ 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; - } + 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; + } /** @@ -47,26 +47,30 @@ class Aop extends Component * @throws ReflectionException * @throws Exception */ - final public function dispatch(): mixed - { - $get_args = func_get_args(); - if (($close = array_shift($get_args)) instanceof \Closure) { - return call_user_func($close, ...$get_args); - } + final public function dispatch(): mixed + { + $get_args = func_get_args(); + if (($close = array_shift($get_args)) instanceof \Closure) { + return call_user_func($close, ...$get_args); + } - $aopName = get_class($close[0]) . '::' . $close[1]; - if (!isset($this->_aop[$aopName])) { - return call_user_func($close, ...$get_args); - } + if (empty($close)) { + return response()->close(404); + } - $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'); + $aopName = get_class($close[0]) . '::' . $close[1]; + if (!isset($this->_aop[$aopName])) { + return call_user_func($close, ...$get_args); + } - return $method->invokeArgs($reflect->newInstance($close), $get_args); - } + $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($close), $get_args); + } }