modify
This commit is contained in:
+120
-115
@@ -23,140 +23,145 @@ use Snowflake\Snowflake;
|
|||||||
class Service extends Component
|
class Service extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_components = [];
|
private array $_components = [];
|
||||||
|
|
||||||
|
|
||||||
private array $_definition = [];
|
private array $_definition = [];
|
||||||
|
|
||||||
|
|
||||||
private array $_ids = [];
|
private array $_ids = [];
|
||||||
|
|
||||||
|
|
||||||
protected array $_alias = [];
|
protected array $_alias = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param bool $try
|
* @param bool $try
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get($id, $try = true): mixed
|
public function get($id, $try = true): mixed
|
||||||
{
|
{
|
||||||
if (isset($this->_components[$id])) {
|
if (isset($this->_components[$id])) {
|
||||||
return $this->_components[$id];
|
return $this->_components[$id];
|
||||||
}
|
}
|
||||||
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
|
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
|
||||||
if ($try === false) {
|
if ($try === false) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
throw new ComponentException("Unknown component ID: $id");
|
throw new ComponentException("Unknown component ID: $id");
|
||||||
}
|
}
|
||||||
if (isset($this->_definition[$id])) {
|
if (isset($this->_definition[$id])) {
|
||||||
$config = $this->_definition[$id];
|
$config = $this->_definition[$id];
|
||||||
if (is_object($config)) {
|
if (is_object($config)) {
|
||||||
return $this->_components[$id] = $config;
|
return $this->_components[$id] = $config;
|
||||||
}
|
}
|
||||||
$object = Snowflake::createObject($config);
|
$object = Snowflake::createObject($config);
|
||||||
} else {
|
} else {
|
||||||
$config = $this->_alias[$id];
|
$config = $this->_alias[$id];
|
||||||
|
|
||||||
$object = Snowflake::createObject($config);
|
$object = Snowflake::createObject($config);
|
||||||
}
|
}
|
||||||
return $this->_components[$id] = $object;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
unset($config['class']);
|
||||||
* @param string $className
|
|
||||||
* @param string $alias
|
|
||||||
*/
|
|
||||||
public function setAlias(string $className, string $alias)
|
|
||||||
{
|
|
||||||
$this->_alias[$className] = $alias;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
Snowflake::configure($object, $config);
|
||||||
* @param $id
|
|
||||||
* @param $definition
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function set($id, $definition): mixed
|
|
||||||
{
|
|
||||||
if ($definition === NULL) {
|
|
||||||
return $this->remove($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->_ids[] = $id;
|
return $this->_components[$id] = $object;
|
||||||
|
}
|
||||||
|
|
||||||
unset($this->_components[$id]);
|
/**
|
||||||
if (is_object($definition) || is_callable($definition, TRUE)) {
|
* @param string $className
|
||||||
return $this->_definition[$id] = $definition;
|
* @param string $alias
|
||||||
} else if (!is_array($definition)) {
|
*/
|
||||||
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
public function setAlias(string $className, string $alias)
|
||||||
}
|
{
|
||||||
if (!isset($definition['class'])) {
|
$this->_alias[$className] = $alias;
|
||||||
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
|
}
|
||||||
} else {
|
|
||||||
$this->_definition[$id] = $definition;
|
|
||||||
}
|
|
||||||
return $this->get($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return bool
|
* @param $definition
|
||||||
*/
|
*
|
||||||
#[Pure] public function has($id): bool
|
* @return mixed
|
||||||
{
|
* @throws Exception
|
||||||
return in_array($id, $this->_ids);
|
*/
|
||||||
}
|
public function set($id, $definition): mixed
|
||||||
|
{
|
||||||
|
if ($definition === NULL) {
|
||||||
|
return $this->remove($id);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$this->_ids[] = $id;
|
||||||
* @param array $data
|
|
||||||
* @throws Exception
|
unset($this->_components[$id]);
|
||||||
*/
|
if (is_object($definition) || is_callable($definition, TRUE)) {
|
||||||
public function setComponents(array $data)
|
return $this->_definition[$id] = $definition;
|
||||||
{
|
} else if (!is_array($definition)) {
|
||||||
foreach ($data as $key => $val) {
|
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
||||||
$this->set($key, $val);
|
}
|
||||||
}
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
#[Pure] public function has($id): bool
|
||||||
|
{
|
||||||
|
return in_array($id, $this->_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function setComponents(array $data)
|
||||||
|
{
|
||||||
|
foreach ($data as $key => $val) {
|
||||||
|
$this->set($key, $val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __get($name): mixed
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
if ($this->has($name)) {
|
if ($this->has($name)) {
|
||||||
return $this->get($name);
|
return $this->get($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
return parent::__get($name);
|
return parent::__get($name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function remove($id): bool
|
public function remove($id): bool
|
||||||
{
|
{
|
||||||
$component = $this->_components[$id];
|
$component = $this->_components[$id];
|
||||||
$className = $component::class;
|
$className = $component::class;
|
||||||
|
|
||||||
unset($component, $this->_components[$id]);
|
unset($component, $this->_components[$id]);
|
||||||
unset($this->_definition[$id]);
|
unset($this->_definition[$id]);
|
||||||
if (isset($this->_alias[$id])) {
|
if (isset($this->_alias[$id])) {
|
||||||
unset($this->_components[$this->_alias[$id]]);
|
unset($this->_components[$this->_alias[$id]]);
|
||||||
unset($this->_definition[$this->_alias[$id]]);
|
unset($this->_definition[$this->_alias[$id]]);
|
||||||
unset($this->_alias[$id]);
|
unset($this->_alias[$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Snowflake::getDi()->unset($className);
|
Snowflake::getDi()->unset($className);
|
||||||
|
|
||||||
return $this->has($id);
|
return $this->has($id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user