From 78d19c26949386780737a8434bdd8360055e0b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 23 Mar 2021 17:56:28 +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 | 25 ++++++++++++++++--------- Annotation/Loader.php | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Annotation/Inject.php b/Annotation/Inject.php index 27f38454..00a3c5d2 100644 --- a/Annotation/Inject.php +++ b/Annotation/Inject.php @@ -5,6 +5,7 @@ namespace Annotation; use ReflectionException; +use ReflectionProperty; use Snowflake\Exception\ComponentException; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -37,17 +38,23 @@ use Snowflake\Snowflake; { [$object, $property] = $handler; if (class_exists($this->className)) { - return $object->$property = Snowflake::createObject($this->className, $this->args); + $injectValue = Snowflake::createObject($this->className, $this->args); + } else if (Snowflake::app()->has($this->className)) { + $injectValue = Snowflake::app()->get($this->className); + } else { + $injectValue = $this->className; } - - $application = Snowflake::app(); - if (!$application->has($this->className)) { - return $object; + if (!empty($this->args) && is_object($injectValue)) { + Snowflake::configure($injectValue, $this->args); } - - $object->$property = $application->get($this->className); - if (!empty($this->args) && is_object($object->$property)) { - Snowflake::configure($object->$property, $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; } diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 1a6a62c4..f8251a84 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -187,7 +187,7 @@ class Loader extends BaseObject } $property = $attribute->newInstance(); if ($property instanceof Inject) { - $property->execute([$_array['handler'], $method->getName()]); + $property->execute([$_array['handler'], $method]); } else { $_property[] = $attribute->newInstance(); }