This commit is contained in:
2021-04-25 15:45:40 +08:00
parent 3b0e0a504f
commit ebf8faa903
+10 -19
View File
@@ -71,7 +71,7 @@ class Container extends BaseObject
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 clone $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);
} }
@@ -230,26 +230,17 @@ class Container extends BaseObject
*/ */
private function resolveDependencies($class): ?array private function resolveDependencies($class): ?array
{ {
if (!isset($this->_constructs[$class])) { if (!isset($this->_reflection[$class])) {
$this->_constructs[$class] = ['class' => $class]; $this->_reflection[$class] = new ReflectionClass($class);
if (!$this->_reflection[$class]->isInstantiable()) {
return null;
}
$this->scanProperty($this->_reflection[$class]);
} }
if (isset($this->_reflection[$class])) { if (!is_null($constructs = $this->_reflection[$class]->getConstructor())) {
return [$this->_reflection[$class], $this->_constructs[$class] ?? []]; $constructs = $this->resolveMethodParam($constructs);
} }
return [$this->_reflection[$class], $constructs];
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable()) {
return null;
}
$this->scanProperty($reflection);
$this->_reflection[$class] = $reflection;
if (!is_null($construct = $reflection->getConstructor())) {
$this->_constructs[$class] = array_merge($this->_constructs[$class],
$this->resolveMethodParam($construct)
);
}
return [$reflection, $this->_constructs[$class] ?? []];
} }