This commit is contained in:
as2252258@163.com
2021-02-23 03:18:50 +08:00
parent 742879e674
commit e3fe05876f
+21 -23
View File
@@ -40,21 +40,24 @@ class Service extends Component
*/ */
public function get($id): mixed public function get($id): mixed
{ {
if (!isset($this->_components[$id])) { if (isset($this->_components[$id])) {
if (!isset($this->_definition[$id])) { return $this->_components[$id];
}
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
throw new ComponentException("Unknown component ID: $id"); throw new ComponentException("Unknown component ID: $id");
} }
if (isset($this->_definition[$id])) {
$config = $this->_definition[$id]; $config = $this->_definition[$id];
if (is_object($config)) { if (is_object($config)) {
return $config; return $this->_components[$id] = $config;
} }
$this->_components[$id] = Snowflake::createObject($config); $object = Snowflake::createObject($config);
} else {
$config = $this->_alias[$id];
$object = Snowflake::createObject($config);
} }
$object = $this->_components[$id]; return $this->_components[$id] = $object;
if (method_exists($object, 'afterInit')) {
$object->afterInit();
}
return $object;
} }
/** /**
@@ -78,25 +81,20 @@ class Service extends Component
public function set($id, $definition): mixed public function set($id, $definition): mixed
{ {
if ($definition === NULL) { if ($definition === NULL) {
$this->remove($id); return $this->remove($id);
return;
} }
unset($this->_components[$id]); unset($this->_components[$id]);
if (is_object($definition) || is_callable($definition, TRUE)) { if (is_object($definition) || is_callable($definition, TRUE)) {
$this->_definition[$id] = $definition; return $this->_definition[$id] = $definition;
return; } else if (!is_array($definition)) {
} 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)); throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
} }
$this->_components[$id] = $object = Snowflake::createObject($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);
} }
/** /**