From b06c8e3da15621433eefab1580c60df68f5ddadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 22 Feb 2021 18:11:23 +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 --- Annotation/Annotation.php | 7 +++++-- HttpServer/Route/Node.php | 3 +-- System/Di/Container.php | 29 ++++++----------------------- 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/Annotation/Annotation.php b/Annotation/Annotation.php index 040fa5ed..4033e9fd 100644 --- a/Annotation/Annotation.php +++ b/Annotation/Annotation.php @@ -119,6 +119,9 @@ class Annotation extends Component private function getReflect(string $class, string $alias): array { $reflect = $this->reflectClass($class); + if (empty($reflect)) { + return []; + } if (!$reflect->isInstantiable()) { return $this->targets($reflect); } @@ -190,10 +193,10 @@ class Annotation extends Component /** * @param string $class - * @return ReflectionClass + * @return ReflectionClass|null * @throws ReflectionException */ - private function reflectClass(string $class): ReflectionClass + private function reflectClass(string $class): ?ReflectionClass { return Snowflake::getDi()->getReflect($class); } diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index f7d55f42..5396afa7 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -215,7 +215,7 @@ class Node extends HttpService { try { $reflect = Snowflake::getDi()->getReflect($controller); - if (!$reflect->isInstantiable()) { + if (empty($reflect)) { throw new Exception($controller . ' Class is con\'t Instantiable.'); } if (!empty($action) && !$reflect->hasMethod($action)) { @@ -446,7 +446,6 @@ class Node extends HttpService */ private function restructure(): static { - var_dump($this, 'restructure'); if (empty($this->handler)) { return $this; } diff --git a/System/Di/Container.php b/System/Di/Container.php index 9fac14fa..f74d8b37 100644 --- a/System/Di/Container.php +++ b/System/Di/Container.php @@ -168,6 +168,9 @@ class Container extends BaseObject $reflection = $this->_reflection[$class]; } else { $reflection = new ReflectionClass($class); + if (!$reflection->isInstantiable()) { + return []; + } $this->_reflection[$class] = $reflection; } $constructs = $reflection->getConstructor(); @@ -191,37 +194,17 @@ class Container extends BaseObject /** * @param $class - * @return mixed + * @return ReflectionClass|null * @throws ReflectionException */ - public function getReflect($class): ReflectionClass + public function getReflect($class): ?ReflectionClass { if (!isset($this->_reflection[$class])) { $this->resolveDependencies($class); } - return $this->_reflection[$class]; + return $this->_reflection[$class] ?? null; } - - /** - * @param string $class - * @return array - * @throws ReflectionException - */ - public function getAttributes(string $class): array - { - $reflection = $this->getReflect($class); - $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); - - $classAttribute = $reflection->getAttributes(); - - foreach ($methods as $method) { - $this->_attributes[$reflection->getName()][$method->getName()] = $method->getAttributes(); - } - return $this->_attributes[$reflection->getName()]; - } - - /** * @param $class */