This commit is contained in:
2021-03-24 18:08:50 +08:00
parent 6df0cb4003
commit 808489382d
+40 -10
View File
@@ -9,11 +9,14 @@ declare(strict_types=1);
namespace Snowflake\Di; namespace Snowflake\Di;
use Annotation\Inject;
use Annotation\Target;
use Database\Connection; use Database\Connection;
use HttpServer\Http\HttpHeaders; use HttpServer\Http\HttpHeaders;
use ReflectionClass; use ReflectionClass;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use ReflectionException; use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -120,9 +123,11 @@ class Container extends BaseObject
* @return object * @return object
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @throws ComponentException
*/ */
private function resolve($class, $constrict, $config): object private function resolve($class, $constrict, $config): object
{ {
/** @var ReflectionClass $reflect */
[$reflect, $dependencies] = $this->resolveDependencies($class, $constrict); [$reflect, $dependencies] = $this->resolveDependencies($class, $constrict);
if (empty($reflect)) { if (empty($reflect)) {
throw new NotFindClassException($class); throw new NotFindClassException($class);
@@ -136,20 +141,45 @@ class Container extends BaseObject
} }
if (empty($config) || !is_array($config)) { if (empty($config) || !is_array($config)) {
return $reflect->newInstanceArgs($dependencies); $object = $reflect->newInstanceArgs($dependencies);
} } else if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
$dependencies[count($dependencies) - 1] = $config; $dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies); $object = $reflect->newInstanceArgs($dependencies);
} else {
if (!empty($config)) $this->_param[$class] = $config;
$object = $this->onAfterInit($reflect->newInstanceArgs($dependencies), $config);
} }
return $this->propertyInject($reflect, $object);
if (!empty($config)) $this->_param[$class] = $config;
return $this->onAfterInit($reflect->newInstanceArgs($dependencies), $config);
} }
/**
* @param ReflectionClass $reflect
* @param $object
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws ComponentException
*/
private function propertyInject(ReflectionClass $reflect, $object): mixed
{
$inject = $reflect->getProperties(
\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED |
\ReflectionProperty::IS_PUBLIC
);
foreach ($inject as $reflectionProperty) {
$attributes = $reflectionProperty->getAttributes(Inject::class);
foreach ($attributes as $attribute) {
/** @var Inject $class */
$class = $attribute->newInstance();
$class->execute([$object, $reflectionProperty->getName()]);
}
}
return $object;
}
/** /**
* @param $object * @param $object
* @param $config * @param $config