modify
This commit is contained in:
@@ -63,9 +63,9 @@ 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 $this->_singletons[$class];
|
return $this->_singletons[$class];
|
||||||
// }
|
}
|
||||||
if (!isset($this->_constructs[$class])) {
|
if (!isset($this->_constructs[$class])) {
|
||||||
return $this->resolve($class, $constrict, $config);
|
return $this->resolve($class, $constrict, $config);
|
||||||
}
|
}
|
||||||
|
|||||||
+109
-107
@@ -24,125 +24,127 @@ use Snowflake\Snowflake;
|
|||||||
class Service extends Component
|
class Service extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_components = [];
|
private array $_components = [];
|
||||||
|
|
||||||
|
|
||||||
private array $_definition = [];
|
private array $_definition = [];
|
||||||
|
|
||||||
|
|
||||||
protected array $_alias = [];
|
protected array $_alias = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function get($id): mixed
|
public function get($id): mixed
|
||||||
{
|
{
|
||||||
if (isset($this->_components[$id])) {
|
if (isset($this->_components[$id])) {
|
||||||
return $this->_components[$id];
|
return $this->_components[$id];
|
||||||
}
|
}
|
||||||
if (isset($this->_definition[$id])) {
|
if (!isset($this->_definition[$id]) && !isset($this->_alias[$id])) {
|
||||||
$object = $this->_definition[$id];
|
throw new ComponentException("Unknown component ID: $id");
|
||||||
if (!is_object($object)) {
|
}
|
||||||
$object = Snowflake::createObject($object);
|
if (isset($this->_definition[$id])) {
|
||||||
}
|
$config = $this->_definition[$id];
|
||||||
} else if (!isset($this->_alias[$id])) {
|
if (is_object($config)) {
|
||||||
throw new ComponentException("Unknown component ID: $id");
|
return $this->_components[$id] = $config;
|
||||||
} else {
|
}
|
||||||
$id = $this->_alias[$id];
|
$object = Snowflake::createObject($config);
|
||||||
|
} else {
|
||||||
|
$config = $this->_alias[$id];
|
||||||
|
|
||||||
$object = Snowflake::createObject($id);
|
$object = Snowflake::createObject($config);
|
||||||
}
|
}
|
||||||
return $this->_components[$id] = $object;
|
return $this->_components[$id] = Snowflake::configure($object, $config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $className
|
* @param string $className
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
*/
|
*/
|
||||||
public function setAlias(string $className, string $alias)
|
public function setAlias(string $className, string $alias)
|
||||||
{
|
{
|
||||||
$this->_alias[$className] = $alias;
|
$this->_alias[$className] = $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $definition
|
* @param $definition
|
||||||
*
|
*
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
*/
|
*/
|
||||||
public function set($id, $definition): mixed
|
public function set($id, $definition): mixed
|
||||||
{
|
{
|
||||||
if ($definition === NULL) {
|
if ($definition === NULL) {
|
||||||
return $this->remove($id);
|
return $this->remove($id);
|
||||||
}
|
}
|
||||||
unset($this->_components[$id]);
|
unset($this->_components[$id]);
|
||||||
if (is_object($definition) || is_callable($definition, TRUE)) {
|
if (is_object($definition) || is_callable($definition, TRUE)) {
|
||||||
return $this->_definition[$id] = $definition;
|
return $this->_definition[$id] = $definition;
|
||||||
} else if (!is_array($definition)) {
|
} else if (!is_array($definition)) {
|
||||||
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
throw new ComponentException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
|
||||||
}
|
}
|
||||||
if (!isset($definition['class'])) {
|
if (!isset($definition['class'])) {
|
||||||
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
|
throw new ComponentException("The configuration for the \"$id\" component must contain a \"class\" element.");
|
||||||
} else {
|
} else {
|
||||||
$this->_definition[$id] = $definition;
|
$this->_definition[$id] = $definition;
|
||||||
}
|
}
|
||||||
return $this->get($id);
|
return $this->get($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function has($id): bool
|
public function has($id): bool
|
||||||
{
|
{
|
||||||
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]);
|
return isset($this->_definition[$id]) || isset($this->_components[$id]) || isset($this->_alias[$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setComponents(array $data)
|
public function setComponents(array $data)
|
||||||
{
|
{
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
$this->set($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
|
||||||
{
|
{
|
||||||
unset($this->_components[$id]);
|
unset($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]);
|
||||||
}
|
}
|
||||||
return $this->has($id);
|
return $this->has($id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user