Yii2 服务定位器
2019-08-25 22:23:08 3 举报
AI智能生成
Yii2 服务定位器
作者其他创作
大纲/内容
private $_components = [];
private $_definitions = [];
__get($name)
if ($this->has($name))
return $this->get($name);
else
return parent::__get($name);
__isset($name)
if ($this->has($name))
return true;
else
return parent::__isset($name);
has($id, $checkInstance = false)
return $checkInstance ? isset($this->_components[$id]) : isset($this->_definitions[$id]);
setComponents($components)
foreach ($components as $id => $component)
$this->set($id, $component);
set($id, $definition)
if ($definition === null)
unset($this->_components[$id], $this->_definitions[$id]);
return;
unset($this->_components[$id]);
if (is_object($definition) || is_callable($definition, true))
$this->_definitions[$id] = $definition;
elseif (is_array($definition))
if (isset($definition['class']))
$this->_definitions[$id] = $definition;
get($id, $throwException = true)
if (isset($this->_components[$id]))
return $this->_components[$id];
if (isset($this->_definitions[$id]))
$definition = $this->_definitions[$id];
if (is_object($definition) && !$definition instanceof Closure)
return $this->_components[$id] = $definition;
else
return $this->_components[$id] = Yii::createObject($definition);
elseif ($throwException)
throw new InvalidConfigException("Unknown component ID: $id");
else
return null;
clear($id)
unset($this->_definitions[$id], $this->_components[$id]);
getComponents($returnDefinitions = true)
return $returnDefinitions ? $this->_definitions : $this->_components;
0 条评论
下一页