modify
This commit is contained in:
+229
-238
@@ -9,12 +9,10 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Snowflake\Di;
|
namespace Snowflake\Di;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
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
|
||||||
@@ -23,259 +21,252 @@ use Snowflake\Snowflake;
|
|||||||
class Container extends BaseObject
|
class Container extends BaseObject
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* instance class by className
|
* instance class by className
|
||||||
*/
|
*/
|
||||||
private array $_singletons = [];
|
private array $_singletons = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* class new instance construct parameter
|
* class new instance construct parameter
|
||||||
*/
|
*/
|
||||||
private array $_constructs = [];
|
private array $_constructs = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* implements \ReflectClass
|
* implements \ReflectClass
|
||||||
*/
|
*/
|
||||||
private array $_reflection = [];
|
private array $_reflection = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* The construct parameter
|
* The construct parameter
|
||||||
*/
|
*/
|
||||||
private array $_param = [];
|
private array $_param = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*
|
*
|
||||||
* The method attributes
|
* The method attributes
|
||||||
*/
|
*/
|
||||||
private array $_attributes = [];
|
private array $_attributes = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param array $constrict
|
* @param array $constrict
|
||||||
* @param array $config
|
* @param array $config
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function get($class, $constrict = [], $config = []): mixed
|
public function get($class, $constrict = [], $config = []): mixed
|
||||||
{
|
{
|
||||||
if (isset($this->_singletons[$class])) {
|
if (isset($this->_singletons[$class])) {
|
||||||
return $this->_singletons[$class];
|
return $this->_singletons[$class];
|
||||||
} else if (!isset($this->_constructs[$class])) {
|
} else if (!isset($this->_constructs[$class])) {
|
||||||
return $this->resolve($class, $constrict, $config);
|
return $this->resolve($class, $constrict, $config);
|
||||||
}
|
}
|
||||||
$definition = $this->_constructs[$class];
|
$definition = $this->_constructs[$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)) {
|
||||||
$object = $this->resolveDefinition($definition, $class, $config, $constrict);
|
$object = $this->resolveDefinition($definition, $class, $config, $constrict);
|
||||||
} 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;
|
return $this->_singletons[$class] = $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $definition
|
* @param $definition
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $config
|
* @param $config
|
||||||
* @param $constrict
|
* @param $constrict
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function resolveDefinition($definition, $class, $config, $constrict): mixed
|
private function resolveDefinition($definition, $class, $config, $constrict): mixed
|
||||||
{
|
{
|
||||||
$config = array_merge($definition, $config);
|
if (!isset($definition['class'])) {
|
||||||
$definition = $this->mergeParam($class, $constrict);
|
throw new NotFindClassException($class);
|
||||||
|
}
|
||||||
|
$_className = $definition['class'];
|
||||||
|
unset($definition['class']);
|
||||||
|
|
||||||
return $this->resolve($class, $definition, $config);
|
$config = array_merge($definition, $config);
|
||||||
}
|
$definition = $this->mergeParam($class, $constrict);
|
||||||
|
|
||||||
/**
|
if ($_className === $class) {
|
||||||
* @param $class
|
$object = $this->resolve($class, $definition, $config);
|
||||||
* @param $constrict
|
} else {
|
||||||
* @param $config
|
$object = $this->get($class, $definition, $config);
|
||||||
*
|
}
|
||||||
* @return object
|
return $object;
|
||||||
* @throws NotFindClassException
|
}
|
||||||
* @throws ReflectionException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function resolve($class, $constrict, $config): object
|
|
||||||
{
|
|
||||||
[$reflect, $dependencies] = $this->resolveDependencies($class);
|
|
||||||
foreach ($constrict as $index => $param) {
|
|
||||||
$dependencies[$index] = $param;
|
|
||||||
}
|
|
||||||
if (!$reflect->isInstantiable()) {
|
|
||||||
throw new Exception($reflect->getName() . ' con\'t instantiable');
|
|
||||||
}
|
|
||||||
if (empty($config)) {
|
|
||||||
return $reflect->newInstanceArgs($dependencies ?? []);
|
|
||||||
}
|
|
||||||
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
|
|
||||||
$dependencies[count($dependencies) - 1] = $config;
|
|
||||||
return $reflect->newInstanceArgs($dependencies);
|
|
||||||
}
|
|
||||||
$this->_param[$class] = $config;
|
|
||||||
$object = $reflect->newInstanceArgs($dependencies ?? []);
|
|
||||||
foreach ($config as $key => $val) {
|
|
||||||
$object->{$key} = $val;
|
|
||||||
}
|
|
||||||
if (method_exists($object, 'afterInit')) {
|
|
||||||
call_user_func([$object, 'afterInit']);
|
|
||||||
}
|
|
||||||
return $object;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
*
|
* @param $constrict
|
||||||
* @return array
|
* @param $config
|
||||||
* @throws ReflectionException
|
*
|
||||||
* @throws NotFindClassException
|
* @return object
|
||||||
*/
|
* @throws NotFindClassException
|
||||||
private function resolveDependencies($class): array
|
* @throws ReflectionException
|
||||||
{
|
*/
|
||||||
$dependencies = [];
|
private function resolve($class, $constrict, $config): object
|
||||||
if (!class_exists($class) || !($reflection = $this->resolveClass($class))) {
|
{
|
||||||
return [];
|
/**
|
||||||
};
|
* @var ReflectionClass $reflect
|
||||||
$constructs = $reflection->getConstructor();
|
* @var array $dependencies
|
||||||
if (!($constructs instanceof \ReflectionMethod)) {
|
*/
|
||||||
return [$reflection, $this->_constructs[$class] = []];
|
list($reflect, $dependencies) = $this->resolveDependencies($class);
|
||||||
}
|
foreach ($constrict as $index => $param) {
|
||||||
foreach ($constructs->getParameters() as $key => $param) {
|
$dependencies[$index] = $param;
|
||||||
$dependencies[] = $this->resolveDefaultValue($param);
|
}
|
||||||
}
|
if (!$reflect->isInstantiable()) {
|
||||||
$this->_constructs[$class] = $dependencies;
|
throw new NotFindClassException($reflect->getName());
|
||||||
return [$reflection, $dependencies];
|
}
|
||||||
}
|
if (empty($config)) {
|
||||||
|
return $reflect->newInstanceArgs($dependencies ?? []);
|
||||||
|
}
|
||||||
|
if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) {
|
||||||
|
$dependencies[count($dependencies) - 1] = $config;
|
||||||
|
return $reflect->newInstanceArgs($dependencies);
|
||||||
|
}
|
||||||
|
$this->_param[$class] = $config;
|
||||||
|
$object = $reflect->newInstanceArgs($dependencies ?? []);
|
||||||
|
foreach ($config as $key => $val) {
|
||||||
|
$object->{$key} = $val;
|
||||||
|
}
|
||||||
|
if (method_exists($object, 'afterInit')) {
|
||||||
|
call_user_func([$object, 'afterInit']);
|
||||||
|
}
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $class
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws ReflectionException
|
||||||
|
*/
|
||||||
|
private function resolveDependencies($class): array
|
||||||
|
{
|
||||||
|
$dependencies = [];
|
||||||
|
if (isset($this->_reflection[$class])) {
|
||||||
|
$reflection = $this->_reflection[$class];
|
||||||
|
} else {
|
||||||
|
$reflection = new ReflectionClass($class);
|
||||||
|
$this->_reflection[$class] = $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;
|
||||||
|
} else if ($param->isDefaultValueAvailable()) {
|
||||||
|
$dependencies[] = $param->getDefaultValue();
|
||||||
|
} else {
|
||||||
|
$c = $param->getClass();
|
||||||
|
$dependencies[] = $c === NULL ? NULL : $c->getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->_constructs[$class] = $dependencies;
|
||||||
|
return [$reflection, $dependencies];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $param
|
* @param $class
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
* @throws ReflectionException
|
||||||
* @throws ReflectionException
|
*/
|
||||||
*/
|
public function getReflect($class): ReflectionClass
|
||||||
private function resolveDefaultValue(\ReflectionParameter $param): mixed
|
{
|
||||||
{
|
if (!isset($this->_reflection[$class])) {
|
||||||
if ($param->isOptional()) {
|
$this->resolveDependencies($class);
|
||||||
return $param->getDefaultValue();
|
}
|
||||||
}
|
return $this->_reflection[$class];
|
||||||
return match ($type = $param->getType()) {
|
}
|
||||||
'array' => [],
|
|
||||||
'int', 'float' => 0,
|
|
||||||
'bool' => false,
|
|
||||||
'string', null => '',
|
|
||||||
'', 'object', 'mixed' => NULL,
|
|
||||||
default => Snowflake::createObject($type)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param string $class
|
||||||
* @return ReflectionClass|null
|
* @return array
|
||||||
*/
|
* @throws ReflectionException
|
||||||
private function resolveClass($class): ?ReflectionClass
|
*/
|
||||||
{
|
public function getAttributes(string $class): array
|
||||||
if (!isset($this->_reflection[$class])) {
|
{
|
||||||
$reflection = new ReflectionClass($class);
|
$reflection = $this->getReflect($class);
|
||||||
if (!$reflection->isInstantiable()) {
|
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||||
return null;
|
|
||||||
}
|
$classAttribute = $reflection->getAttributes();
|
||||||
$this->_reflection[$class] = $reflection;
|
|
||||||
}
|
foreach ($methods as $method) {
|
||||||
return $this->_reflection[$class];
|
$this->_attributes[$reflection->getName()][$method->getName()] = $method->getAttributes();
|
||||||
}
|
}
|
||||||
|
return $this->_attributes[$reflection->getName()];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @return mixed
|
*/
|
||||||
*/
|
public function unset($class)
|
||||||
public function getDependencies($class): mixed
|
{
|
||||||
{
|
if (is_array($class) && isset($class['class'])) {
|
||||||
return $this->_constructs[$class] ?? [];
|
$class = $class['class'];
|
||||||
}
|
} else if (is_object($class)) {
|
||||||
|
$class = get_class($class);
|
||||||
|
}
|
||||||
|
unset(
|
||||||
|
$this->_reflection[$class], $this->_singletons[$class],
|
||||||
|
$this->_param[$class], $this->_constructs[$class]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function flush(): static
|
||||||
|
{
|
||||||
|
$this->_reflection = [];
|
||||||
|
$this->_singletons = [];
|
||||||
|
$this->_param = [];
|
||||||
|
$this->_constructs = [];
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @return ReflectionClass|null
|
* @param $newParam
|
||||||
* @throws ReflectionException
|
*
|
||||||
* @throws NotFindClassException
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function getReflect($class): ?ReflectionClass
|
private function mergeParam($class, $newParam): array
|
||||||
{
|
{
|
||||||
if (!isset($this->_reflection[$class])) {
|
if (empty($this->_param[$class])) {
|
||||||
$this->resolveDependencies($class);
|
return $newParam;
|
||||||
}
|
} else if (empty($newParam)) {
|
||||||
return $this->_reflection[$class] ?? null;
|
return $this->_param[$class];
|
||||||
}
|
}
|
||||||
|
$old = $this->_param[$class];
|
||||||
/**
|
foreach ($newParam as $key => $val) {
|
||||||
* @param $class
|
$old[$key] = $val;
|
||||||
*/
|
}
|
||||||
public function unset($class)
|
return $old;
|
||||||
{
|
}
|
||||||
if (is_array($class) && isset($class['class'])) {
|
|
||||||
$class = $class['class'];
|
|
||||||
} else if (is_object($class)) {
|
|
||||||
$class = get_class($class);
|
|
||||||
}
|
|
||||||
unset(
|
|
||||||
$this->_reflection[$class], $this->_singletons[$class],
|
|
||||||
$this->_param[$class], $this->_constructs[$class]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function flush(): static
|
|
||||||
{
|
|
||||||
$this->_reflection = [];
|
|
||||||
$this->_singletons = [];
|
|
||||||
$this->_param = [];
|
|
||||||
$this->_constructs = [];
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $class
|
|
||||||
* @param $newParam
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
private function mergeParam($class, $newParam): array
|
|
||||||
{
|
|
||||||
if (empty($this->_param[$class])) {
|
|
||||||
return $newParam;
|
|
||||||
} else if (empty($newParam)) {
|
|
||||||
return $this->_param[$class];
|
|
||||||
}
|
|
||||||
$old = $this->_param[$class];
|
|
||||||
foreach ($newParam as $key => $val) {
|
|
||||||
$old[$key] = $val;
|
|
||||||
}
|
|
||||||
return $old;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user