modify
This commit is contained in:
+49
-58
@@ -9,12 +9,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Snowflake\Di;
|
||||
|
||||
use Exception;
|
||||
use ReflectionClass;
|
||||
use Snowflake\Abstracts\BaseObject;
|
||||
use ReflectionException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Container
|
||||
@@ -100,10 +98,21 @@ class Container extends BaseObject
|
||||
*/
|
||||
private function resolveDefinition($definition, $class, $config, $constrict): mixed
|
||||
{
|
||||
if (!isset($definition['class'])) {
|
||||
throw new NotFindClassException($class);
|
||||
}
|
||||
$_className = $definition['class'];
|
||||
unset($definition['class']);
|
||||
|
||||
$config = array_merge($definition, $config);
|
||||
$definition = $this->mergeParam($class, $constrict);
|
||||
|
||||
return $this->resolve($class, $definition, $config);
|
||||
if ($_className === $class) {
|
||||
$object = $this->resolve($class, $definition, $config);
|
||||
} else {
|
||||
$object = $this->get($class, $definition, $config);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,16 +123,19 @@ class Container extends BaseObject
|
||||
* @return object
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
private function resolve($class, $constrict, $config): object
|
||||
{
|
||||
[$reflect, $dependencies] = $this->resolveDependencies($class);
|
||||
/**
|
||||
* @var ReflectionClass $reflect
|
||||
* @var array $dependencies
|
||||
*/
|
||||
list($reflect, $dependencies) = $this->resolveDependencies($class);
|
||||
foreach ($constrict as $index => $param) {
|
||||
$dependencies[$index] = $param;
|
||||
}
|
||||
if (!$reflect->isInstantiable()) {
|
||||
throw new Exception($reflect->getName() . ' con\'t instantiable');
|
||||
throw new NotFindClassException($reflect->getName());
|
||||
}
|
||||
if (empty($config)) {
|
||||
return $reflect->newInstanceArgs($dependencies ?? []);
|
||||
@@ -148,20 +160,29 @@ class Container extends BaseObject
|
||||
*
|
||||
* @return array
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function resolveDependencies($class): array
|
||||
{
|
||||
$dependencies = [];
|
||||
if (!class_exists($class) || !($reflection = $this->resolveClass($class))) {
|
||||
return [];
|
||||
};
|
||||
if (isset($this->_reflection[$class])) {
|
||||
$reflection = $this->_reflection[$class];
|
||||
} else {
|
||||
$reflection = new ReflectionClass($class);
|
||||
$this->_reflection[$class] = $reflection;
|
||||
}
|
||||
$constructs = $reflection->getConstructor();
|
||||
if (!($constructs instanceof \ReflectionMethod)) {
|
||||
return [$reflection, $this->_constructs[$class] = []];
|
||||
if (empty($constructs) || !is_array($constructs)) {
|
||||
return [$reflection, []];
|
||||
}
|
||||
foreach ($constructs->getParameters() as $key => $param) {
|
||||
$dependencies[] = $this->resolveDefaultValue($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];
|
||||
@@ -169,68 +190,38 @@ class Container extends BaseObject
|
||||
|
||||
|
||||
/**
|
||||
* @param $param
|
||||
* @param $class
|
||||
* @return mixed
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function resolveDefaultValue(\ReflectionParameter $param): mixed
|
||||
{
|
||||
if ($param->isOptional()) {
|
||||
return $param->getDefaultValue();
|
||||
}
|
||||
return match ($type = $param->getType()) {
|
||||
'array' => [],
|
||||
'int', 'float' => 0,
|
||||
'bool' => false,
|
||||
'string', null => '',
|
||||
'', 'object', 'mixed' => NULL,
|
||||
default => Snowflake::createObject($type)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return ReflectionClass|null
|
||||
*/
|
||||
private function resolveClass($class): ?ReflectionClass
|
||||
public function getReflect($class): ReflectionClass
|
||||
{
|
||||
if (!isset($this->_reflection[$class])) {
|
||||
$reflection = new ReflectionClass($class);
|
||||
if (!$reflection->isInstantiable()) {
|
||||
return null;
|
||||
}
|
||||
$this->_reflection[$class] = $reflection;
|
||||
$this->resolveDependencies($class);
|
||||
}
|
||||
return $this->_reflection[$class];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDependencies($class): mixed
|
||||
{
|
||||
return $this->_constructs[$class] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
* @return ReflectionClass|null
|
||||
* @param string $class
|
||||
* @return array
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function getReflect($class): ?ReflectionClass
|
||||
public function getAttributes(string $class): array
|
||||
{
|
||||
if (!isset($this->_reflection[$class])) {
|
||||
$this->resolveDependencies($class);
|
||||
$reflection = $this->getReflect($class);
|
||||
$methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||
|
||||
$classAttribute = $reflection->getAttributes();
|
||||
|
||||
foreach ($methods as $method) {
|
||||
$this->_attributes[$reflection->getName()][$method->getName()] = $method->getAttributes();
|
||||
}
|
||||
return $this->_reflection[$class] ?? null;
|
||||
return $this->_attributes[$reflection->getName()];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user