This commit is contained in:
as2252258@163.com
2021-02-23 03:17:32 +08:00
parent 74460526a4
commit 742879e674
2 changed files with 101 additions and 123 deletions
+25 -23
View File
@@ -40,24 +40,21 @@ class Service extends Component
*/
public function get($id): mixed
{
if (isset($this->_components[$id])) {
return $this->_components[$id];
}
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
throw new ComponentException("Unknown component ID: $id");
}
if (isset($this->_definition[$id])) {
if (!isset($this->_components[$id])) {
if (!isset($this->_definition[$id])) {
throw new ComponentException("Unknown component ID: $id");
}
$config = $this->_definition[$id];
if (is_object($config)) {
return $this->_components[$id] = $config;
return $config;
}
$object = Snowflake::createObject($config);
} else {
$config = $this->_alias[$id];
$object = Snowflake::createObject($config);
$this->_components[$id] = Snowflake::createObject($config);
}
return $this->_components[$id] = Snowflake::configure($object, $config);
$object = $this->_components[$id];
if (method_exists($object, 'afterInit')) {
$object->afterInit();
}
return $object;
}
/**
@@ -81,20 +78,25 @@ class Service extends Component
public function set($id, $definition): mixed
{
if ($definition === NULL) {
return $this->remove($id);
$this->remove($id);
return;
}
unset($this->_components[$id]);
if (is_object($definition) || is_callable($definition, TRUE)) {
return $this->_definition[$id] = $definition;
} else if (!is_array($definition)) {
$this->_definition[$id] = $definition;
return;
} else if (is_array($definition)) {
if (isset($definition['class'])) {
$this->_definition[$id] = $definition;
} else {
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
}
} else {
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
}
if (!isset($definition['class'])) {
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
} else {
$this->_definition[$id] = $definition;
}
return $this->get($id);
$this->_components[$id] = $object = Snowflake::createObject($definition);
}
/**