This commit is contained in:
2025-07-11 11:51:05 +08:00
parent 6ae7f5a721
commit 045c9293d5
+22 -3
View File
@@ -91,7 +91,8 @@ class Container implements ContainerInterface
*/ */
public function get(string $id): object public function get(string $id): object
{ {
if (isset($this->_singletons[$id])) return $this->_singletons[$id]; if (isset($this->_singletons[$id]))
return $this->_singletons[$id];
if (isset($this->_interfaces[$id])) { if (isset($this->_interfaces[$id])) {
return $this->_singletons[$id] = $this->make($this->_interfaces[$id]); return $this->_singletons[$id] = $this->make($this->_interfaces[$id]);
} else { } else {
@@ -166,7 +167,7 @@ class Container implements ContainerInterface
} }
if (($handler = $reflect->getConstructor()) !== null) { if (($handler = $reflect->getConstructor()) !== null) {
$construct = $this->getMethodParams($handler); $construct = $this->mergeParams($this->getMethodParams($handler), $construct);
} }
$newInstance = $reflect->newInstanceArgs($construct); $newInstance = $reflect->newInstanceArgs($construct);
@@ -174,6 +175,23 @@ class Container implements ContainerInterface
} }
/**
* @param array $default
* @param array $params
* @return array
*/
protected function mergeParams(array $default, array $params): array
{
if (empty($params)) {
return $default;
}
foreach ($params as $key => $value) {
$default[$key] = $value;
}
return $default;
}
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param array $construct * @param array $construct
@@ -304,7 +322,8 @@ class Container implements ContainerInterface
{ {
$className = $parameters->getDeclaringClass()->getName(); $className = $parameters->getDeclaringClass()->getName();
$methodName = $parameters->getName(); $methodName = $parameters->getName();
if (!isset($this->_parameters[$className])) $this->_parameters[$className] = []; if (!isset($this->_parameters[$className]))
$this->_parameters[$className] = [];
if (!isset($this->_parameters[$className][$methodName])) { if (!isset($this->_parameters[$className][$methodName])) {
return $this->_parameters[$className][$methodName] = $this->resolveMethodParams($parameters); return $this->_parameters[$className][$methodName] = $this->resolveMethodParams($parameters);
} else { } else {