This commit is contained in:
as2252258@163.com
2021-02-23 03:17:32 +08:00
parent 74460526a4
commit 742879e674
2 changed files with 101 additions and 123 deletions
+74 -98
View File
@@ -9,13 +9,10 @@ declare(strict_types=1);
namespace Snowflake\Di; namespace Snowflake\Di;
use Database\Connection;
use HttpServer\Http\HttpHeaders;
use ReflectionClass; use ReflectionClass;
use Snowflake\Abstracts\BaseObject; use Snowflake\Abstracts\BaseObject;
use ReflectionException; use ReflectionException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/** /**
* Class Container * Class Container
@@ -29,28 +26,29 @@ class Container extends BaseObject
* *
* instance class by className * instance class by className
*/ */
private array $_singletons = []; private $_singletons = [];
/** /**
* @var array * @var array
* *
* class new instance construct parameter * class new instance construct parameter
*/ */
private array $_constructs = []; private $_constructs = [];
/** /**
* @var array * @var array
* *
* implements \ReflectClass * implements \ReflectClass
*/ */
private array $_reflection = []; private $_reflection = [];
/** /**
* @var array * @var array
* *
* The construct parameter * The construct parameter
*/ */
private array $_param = []; private $_param = [];
/** /**
* @param $class * @param $class
@@ -61,73 +59,91 @@ class Container extends BaseObject
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
public function get($class, $constrict = [], $config = []): mixed public function get($class, $constrict = [], $config = [])
{ {
if (isset($this->_singletons[$class])) { if (isset($this->_singletons[$class])) {
return $this->_singletons[$class]; return $this->_singletons[$class];
} } else if (!isset($this->_constructs[$class])) {
if (!isset($this->_constructs[$class])) {
return $this->resolve($class, $constrict, $config); return $this->resolve($class, $constrict, $config);
} }
$definition = $this->_constructs[$class];
$definition = $this->_param[$class] ?? ['class' => $class];
if (is_callable($definition, TRUE)) { if (is_callable($definition, TRUE)) {
return call_user_func($definition, $this, $constrict, $config); return call_user_func($definition, $this, $constrict, $config);
} else if (is_array($definition)) { } else if (is_array($definition)) {
$constructs = $this->_constructs[$class] ?? []; $object = $this->resolveDefinition($definition, $class, $config, $constrict);
if ($class === $definition['class']) {
$object = $this->resolve($class, $constructs, $config);
} else {
$object = $this->get($class, $constructs, $config);
}
return $this->_singletons[$class] = $object;
} else if (is_object($definition)) { } else if (is_object($definition)) {
return $this->_singletons[$class] = $definition; return $this->_singletons[$class] = $definition;
} else { } else {
throw new NotFindClassException($class); throw new NotFindClassException($class);
} }
return $this->_singletons[$class] = $object;
} }
/**
* @param $definition
* @param $class
* @param $config
* @param $constrict
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveDefinition($definition, $class, $config, $constrict)
{
if (!isset($definition['class'])) {
throw new NotFindClassException($class);
}
$_className = $definition['class'];
unset($definition['class']);
$config = array_merge($definition, $config);
$definition = $this->mergeParam($class, $constrict);
if ($_className === $class) {
$object = $this->resolve($class, $definition, $config);
} else {
$object = $this->get($class, $definition, $config);
}
return $object;
}
/** /**
* @param $class * @param $class
* @param $constrict * @param $constrict
* @param $config * @param $config
* *
* @return object * @return mixed
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolve($class, $constrict, $config): object private function resolve($class, $constrict, $config)
{ {
$reflect = $this->resolveDependencies($class, $constrict); /**
* @var ReflectionClass $reflect
* @var array $dependencies
*/
list($reflect, $dependencies) = $this->resolveDependencies($class);
foreach ($constrict as $index => $param) {
$dependencies[$index] = $param;
}
if (!$reflect->isInstantiable()) { if (!$reflect->isInstantiable()) {
throw new NotFindClassException($reflect->getName()); throw new NotFindClassException($reflect->getName());
} }
$this->_param[$class] = $config;
$this->_param[$class]['class'] = $class;
$dependencies = $this->_constructs[$class] ?? [];
if (empty($config)) { if (empty($config)) {
return $reflect->newInstanceArgs($dependencies); return $reflect->newInstanceArgs($dependencies ?? []);
} }
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) { if (!empty($dependencies) && $reflect->implementsInterface('BeReborn\Base\Configure')) {
$dependencies[count($dependencies) - 1] = $config; $dependencies[count($dependencies) - 1] = $config;
return $reflect->newInstanceArgs($dependencies); return $reflect->newInstanceArgs($dependencies);
} }
if (!empty($config)) {
return $this->onAfterInit($reflect->newInstanceArgs($dependencies), $config); $this->_param[$class] = $config;
}
$object = $reflect->newInstanceArgs($dependencies ?? []);
foreach ($config as $key => $val) {
$object->{$key} = $val;
} }
/**
* @param $object
* @param $config
* @return mixed
*/
private function onAfterInit($object, $config)
{
Snowflake::configure($object, $config);
if (method_exists($object, 'afterInit')) { if (method_exists($object, 'afterInit')) {
call_user_func([$object, 'afterInit']); call_user_func([$object, 'afterInit']);
} }
@@ -140,71 +156,31 @@ class Container extends BaseObject
* @return array * @return array
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolveDependencies($class, $constrict = []): ?ReflectionClass private function resolveDependencies($class)
{ {
if (!isset($this->_reflection[$class])) { $dependencies = [];
$reflection = new ReflectionClass($class); if (isset($this->_reflection[$class])) {
if (!$reflection->isInstantiable()) {
return null;
}
$this->_reflection[$class] = $reflection;
} else {
$reflection = $this->_reflection[$class]; $reflection = $this->_reflection[$class];
}
if (!is_null($construct = $reflection->getConstructor())) {
$this->_constructs[$class] = $this->resolveMethodParam($construct, $constrict);
} else { } else {
$this->_constructs[$class] = $constrict; $reflection = new ReflectionClass($class);
$this->_reflection[$class] = $reflection;
} }
return $reflection; $constructs = $reflection->getConstructor();
if (empty($constructs) || !is_array($constructs)) {
return [$reflection, []];
} }
foreach ($constructs->getParameters() as $key => $param) {
if (version_compare(PHP_VERSION, '5.6.0', '>=') && $param->isVariadic()) {
/** break;
* @param \ReflectionMethod|null $method } else if ($param->isDefaultValueAvailable()) {
* @return array $dependencies[] = $param->getDefaultValue();
* @throws NotFindClassException
* @throws ReflectionException
*/
private function resolveMethodParam(?\ReflectionMethod $method, $default = []): array
{
$array = [];
foreach ($method->getParameters() as $key => $parameter) {
if (!is_null($default[$key] ?? null)) {
$array[] = $default[$key];
} else if ($parameter->isDefaultValueAvailable()) {
$array[] = $parameter->getDefaultValue();
} else { } else {
$type = $parameter->getType(); $c = $param->getClass();
if (is_string($type) && class_exists($type)) { $dependencies[] = $c === NULL ? NULL : $c->getName();
$type = Snowflake::createObject($type);
}
$array[] = match ($parameter->getType()) {
'string' => '',
'int', 'float' => 0,
'', null, 'object', 'mixed' => NULL,
'bool' => false,
default => $type
};
} }
} }
return $array; $this->_constructs[$class] = $dependencies;
} return [$reflection, $dependencies];
/**
* @param $class
* @return mixed
* @throws ReflectionException
*/
public function getReflect($class): ?ReflectionClass
{
$reflect = $this->_reflection[$class] ?? null;
if (!is_null($reflect)) {
return $reflect;
}
return $this->resolveDependencies($class);
} }
/** /**
@@ -226,7 +202,7 @@ class Container extends BaseObject
/** /**
* @return $this * @return $this
*/ */
public function flush(): static public function flush()
{ {
$this->_reflection = []; $this->_reflection = [];
$this->_singletons = []; $this->_singletons = [];
@@ -241,7 +217,7 @@ class Container extends BaseObject
* *
* @return mixed * @return mixed
*/ */
private function mergeParam($class, $newParam): array private function mergeParam($class, $newParam)
{ {
if (empty($this->_param[$class])) { if (empty($this->_param[$class])) {
return $newParam; return $newParam;
+23 -21
View File
@@ -40,24 +40,21 @@ class Service extends Component
*/ */
public function get($id): mixed public function get($id): mixed
{ {
if (isset($this->_components[$id])) { if (!isset($this->_components[$id])) {
return $this->_components[$id]; if (!isset($this->_definition[$id])) {
}
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
throw new ComponentException("Unknown component ID: $id"); throw new ComponentException("Unknown component ID: $id");
} }
if (isset($this->_definition[$id])) {
$config = $this->_definition[$id]; $config = $this->_definition[$id];
if (is_object($config)) { if (is_object($config)) {
return $this->_components[$id] = $config; return $config;
} }
$object = Snowflake::createObject($config); $this->_components[$id] = Snowflake::createObject($config);
} else {
$config = $this->_alias[$id];
$object = Snowflake::createObject($config);
} }
return $this->_components[$id] = Snowflake::configure($object, $config); $object = $this->_components[$id];
if (method_exists($object, 'afterInit')) {
$object->afterInit();
}
return $object;
} }
/** /**
@@ -81,20 +78,25 @@ class Service extends Component
public function set($id, $definition): mixed public function set($id, $definition): mixed
{ {
if ($definition === NULL) { if ($definition === NULL) {
return $this->remove($id); $this->remove($id);
return;
} }
unset($this->_components[$id]); unset($this->_components[$id]);
if (is_object($definition) || is_callable($definition, TRUE)) { if (is_object($definition) || is_callable($definition, TRUE)) {
return $this->_definition[$id] = $definition; $this->_definition[$id] = $definition;
} else if (!is_array($definition)) { return;
} else if (is_array($definition)) {
if (isset($definition['class'])) {
$this->_definition[$id] = $definition;
} else {
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
}
} else {
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition)); throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
} }
if (!isset($definition['class'])) { $this->_components[$id] = $object = Snowflake::createObject($definition);
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
} else {
$this->_definition[$id] = $definition;
}
return $this->get($id);
} }
/** /**