From c2367c70f3d379c60879e4d6cfd39532ea6d4c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 23 Mar 2021 18:00:49 +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/Inject.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/Annotation/Inject.php b/Annotation/Inject.php index 00a3c5d2..3914e83c 100644 --- a/Annotation/Inject.php +++ b/Annotation/Inject.php @@ -31,12 +31,36 @@ use Snowflake\Snowflake; /** * @param array $handler * @return mixed + * @throws ComponentException * @throws NotFindClassException - * @throws ReflectionException|ComponentException + * @throws ReflectionException */ public function execute(array $handler): mixed { - [$object, $property] = $handler; + $injectValue = $this->parseInjectValue(); + + /** @var ReflectionProperty $property */ + if ($property->isPrivate() || $property->isProtected()) { + $method = 'set' . ucfirst($property->getName()); + if (!method_exists($handler[0], $method)) { + return false; + } + $handler[0]->$method($injectValue); + } else { + $handler[0]->{$property->getName()} = $injectValue; + } + return $handler[0]; + } + + + /** + * @return mixed + * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException + */ + private function parseInjectValue(): mixed + { if (class_exists($this->className)) { $injectValue = Snowflake::createObject($this->className, $this->args); } else if (Snowflake::app()->has($this->className)) { @@ -47,15 +71,7 @@ use Snowflake\Snowflake; if (!empty($this->args) && is_object($injectValue)) { Snowflake::configure($injectValue, $this->args); } - /** @var ReflectionProperty $property */ - if ($property->isPrivate() || $property->isProtected()) { - if (!method_exists($handler[0], 'set' . ucfirst($property->getName()))) { - return false; - } - $object->{'set' . ucfirst($property->getName())}($injectValue); - } else { - $object->$property = $injectValue; - } - return $object; + return $injectValue; } + }