From e0112e1587d50c9ae9880a0bbc4d8d7c8af3a00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 22 Feb 2021 18:53:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- System/Di/Container.php | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/System/Di/Container.php b/System/Di/Container.php index 2e93f0b2..bb348a5d 100644 --- a/System/Di/Container.php +++ b/System/Di/Container.php @@ -174,23 +174,35 @@ class Container extends BaseObject return [$reflection, []]; } foreach ($constructs->getParameters() as $key => $param) { - if ($param->isOptional()) { - $dependencies[] = $param->getDefaultValue(); - } else { - match ($param->getType()) { - 'mixed' => $dependencies[] = $param->getDefaultValue(), - 'string' => $dependencies[] = '', - 'int' => $dependencies[] = 0, - 'bool' => $dependencies[] = false, - default => $dependencies[] = Snowflake::createObject($param->getType()) - }; - } + $dependencies[] = $this->resolveDefaultValue($param); } $this->_constructs[$class] = $dependencies; return [$reflection, $dependencies]; } + /** + * @param $param + * @return false|int|string|null + * @throws NotFindClassException + * @throws ReflectionException + */ + private function resolveDefaultValue($param,): bool|int|string|null + { + if ($param->isOptional()) { + return $param->getDefaultValue(); + } + return match ($param->getType()) { + 'mixed' => $param->getDefaultValue(), + 'string' => '', + 'int' => 0, + 'bool' => false, + 'NULL' => NULL, + default => Snowflake::createObject($param->getType()) + }; + } + + /** * @param $class * @return ReflectionClass|null