This commit is contained in:
2021-02-22 19:15:49 +08:00
parent 04f031a5e8
commit 122392c583
+12 -23
View File
@@ -127,11 +127,7 @@ class Container extends BaseObject
*/ */
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;
} }
@@ -174,7 +170,7 @@ class Container extends BaseObject
return [$reflection, []]; return [$reflection, []];
} }
foreach ($constructs->getParameters() as $key => $param) { foreach ($constructs->getParameters() as $key => $param) {
$dependencies[] = $this->resolveDefaultValue($param, $class); $dependencies[] = $this->resolveDefaultValue($param);
} }
$this->_constructs[$class] = $dependencies; $this->_constructs[$class] = $dependencies;
return [$reflection, $dependencies]; return [$reflection, $dependencies];
@@ -187,27 +183,20 @@ class Container extends BaseObject
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
private function resolveDefaultValue(\ReflectionParameter $param, $class): mixed private function resolveDefaultValue(\ReflectionParameter $param): mixed
{ {
if ($param->isOptional()) { if ($param->isOptional()) {
return $param->getDefaultValue(); return $param->getDefaultValue();
} }
try { return match ($type = $param->getType()) {
$type = $param->getType(); 'mixed' => $param->getDefaultValue(),
var_dump($type); 'string' => '',
return match ($type) { 'array' => [],
'mixed' => $param->getDefaultValue(), 'int', 'float' => 0,
'string' => '', 'bool' => false,
'int', 'float' => 0, '', null, 'object' => NULL,
'bool' => false, default => Snowflake::createObject($type)
'', null, 'object' => NULL, };
default => Snowflake::createObject($type)
};
} catch (\Throwable $throwable) {
var_dump($class, $throwable->getMessage(), $throwable->getFile(), $throwable->getLine());
echo PHP_EOL;
return null;
}
} }