modify
This commit is contained in:
+49
-58
@@ -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
|
||||||
@@ -100,10 +98,21 @@ class Container extends BaseObject
|
|||||||
*/
|
*/
|
||||||
private function resolveDefinition($definition, $class, $config, $constrict): mixed
|
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);
|
$config = array_merge($definition, $config);
|
||||||
$definition = $this->mergeParam($class, $constrict);
|
$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
|
* @return object
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
|
||||||
*/
|
*/
|
||||||
private function resolve($class, $constrict, $config): object
|
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) {
|
foreach ($constrict as $index => $param) {
|
||||||
$dependencies[$index] = $param;
|
$dependencies[$index] = $param;
|
||||||
}
|
}
|
||||||
if (!$reflect->isInstantiable()) {
|
if (!$reflect->isInstantiable()) {
|
||||||
throw new Exception($reflect->getName() . ' con\'t instantiable');
|
throw new NotFindClassException($reflect->getName());
|
||||||
}
|
}
|
||||||
if (empty($config)) {
|
if (empty($config)) {
|
||||||
return $reflect->newInstanceArgs($dependencies ?? []);
|
return $reflect->newInstanceArgs($dependencies ?? []);
|
||||||
@@ -148,20 +160,29 @@ class Container extends BaseObject
|
|||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
|
||||||
*/
|
*/
|
||||||
private function resolveDependencies($class): array
|
private function resolveDependencies($class): array
|
||||||
{
|
{
|
||||||
$dependencies = [];
|
$dependencies = [];
|
||||||
if (!class_exists($class) || !($reflection = $this->resolveClass($class))) {
|
if (isset($this->_reflection[$class])) {
|
||||||
return [];
|
$reflection = $this->_reflection[$class];
|
||||||
};
|
} else {
|
||||||
|
$reflection = new ReflectionClass($class);
|
||||||
|
$this->_reflection[$class] = $reflection;
|
||||||
|
}
|
||||||
$constructs = $reflection->getConstructor();
|
$constructs = $reflection->getConstructor();
|
||||||
if (!($constructs instanceof \ReflectionMethod)) {
|
if (empty($constructs) || !is_array($constructs)) {
|
||||||
return [$reflection, $this->_constructs[$class] = []];
|
return [$reflection, []];
|
||||||
}
|
}
|
||||||
foreach ($constructs->getParameters() as $key => $param) {
|
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;
|
$this->_constructs[$class] = $dependencies;
|
||||||
return [$reflection, $dependencies];
|
return [$reflection, $dependencies];
|
||||||
@@ -169,68 +190,38 @@ class Container extends BaseObject
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $param
|
* @param $class
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws NotFindClassException
|
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
private function resolveDefaultValue(\ReflectionParameter $param): mixed
|
public function getReflect($class): ReflectionClass
|
||||||
{
|
|
||||||
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
|
|
||||||
{
|
{
|
||||||
if (!isset($this->_reflection[$class])) {
|
if (!isset($this->_reflection[$class])) {
|
||||||
$reflection = new ReflectionClass($class);
|
$this->resolveDependencies($class);
|
||||||
if (!$reflection->isInstantiable()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
$this->_reflection[$class] = $reflection;
|
|
||||||
}
|
}
|
||||||
return $this->_reflection[$class];
|
return $this->_reflection[$class];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param string $class
|
||||||
* @return mixed
|
* @return array
|
||||||
*/
|
|
||||||
public function getDependencies($class): mixed
|
|
||||||
{
|
|
||||||
return $this->_constructs[$class] ?? [];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $class
|
|
||||||
* @return ReflectionClass|null
|
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
|
||||||
*/
|
*/
|
||||||
public function getReflect($class): ?ReflectionClass
|
public function getAttributes(string $class): array
|
||||||
{
|
{
|
||||||
if (!isset($this->_reflection[$class])) {
|
$reflection = $this->getReflect($class);
|
||||||
$this->resolveDependencies($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
|
* @param $class
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user