This commit is contained in:
2021-03-23 17:56:28 +08:00
parent 48472ccec1
commit 78d19c2694
2 changed files with 17 additions and 10 deletions
+16 -9
View File
@@ -5,6 +5,7 @@ namespace Annotation;
use ReflectionException; use ReflectionException;
use ReflectionProperty;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -37,17 +38,23 @@ use Snowflake\Snowflake;
{ {
[$object, $property] = $handler; [$object, $property] = $handler;
if (class_exists($this->className)) { 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;
} }
if (!empty($this->args) && is_object($injectValue)) {
$application = Snowflake::app(); Snowflake::configure($injectValue, $this->args);
if (!$application->has($this->className)) {
return $object;
} }
/** @var ReflectionProperty $property */
$object->$property = $application->get($this->className); if ($property->isPrivate() || $property->isProtected()) {
if (!empty($this->args) && is_object($object->$property)) { if (!method_exists($handler[0], 'set' . ucfirst($property->getName()))) {
Snowflake::configure($object->$property, $this->args); return false;
}
$object->{'set' . ucfirst($property->getName())}($injectValue);
} else {
$object->$property = $injectValue;
} }
return $object; return $object;
} }
+1 -1
View File
@@ -187,7 +187,7 @@ class Loader extends BaseObject
} }
$property = $attribute->newInstance(); $property = $attribute->newInstance();
if ($property instanceof Inject) { if ($property instanceof Inject) {
$property->execute([$_array['handler'], $method->getName()]); $property->execute([$_array['handler'], $method]);
} else { } else {
$_property[] = $attribute->newInstance(); $_property[] = $attribute->newInstance();
} }