mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-20 16:38:22 +08:00
Refactored getModule and hasModule.
This commit is contained in:
@@ -326,55 +326,47 @@ abstract class Module extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks whether the named module exists.
|
* Checks whether the child module of the specified ID exists.
|
||||||
* @param string $id module ID
|
* This method supports checking the existence of both child and grand child modules.
|
||||||
|
* @param string $id module ID. For grand child modules, use ID path relative to this module (e.g. `admin/content`).
|
||||||
* @return boolean whether the named module exists. Both loaded and unloaded modules
|
* @return boolean whether the named module exists. Both loaded and unloaded modules
|
||||||
* are considered.
|
* are considered.
|
||||||
*/
|
*/
|
||||||
public function hasModule($id)
|
public function hasModule($id)
|
||||||
{
|
{
|
||||||
if (strpos($id, '/') === false) {
|
if (($pos = strpos($id, '/')) !== false) {
|
||||||
return isset($this->_modules[$id]);
|
// sub-module
|
||||||
|
$module = $this->getModule(substr($id, 0, $pos));
|
||||||
|
return $module === null ? false : $module->hasModule(substr($id, $pos + 1));
|
||||||
} else {
|
} else {
|
||||||
// it's a sub-module
|
return isset($this->_modules[$id]);
|
||||||
$ids = explode('/', $id);
|
|
||||||
$module = $this;
|
|
||||||
foreach ($ids as $id) {
|
|
||||||
if (!isset($module->_modules[$id])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$module = $module->getModule($id);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the named module.
|
* Retrieves the child module of the specified ID.
|
||||||
* @param string $id module ID (case-sensitive).
|
* This method supports retrieving both child modules and grand child modules.
|
||||||
|
* @param string $id module ID (case-sensitive). To retrieve grand child modules,
|
||||||
|
* use ID path relative to this module (e.g. `admin/content`).
|
||||||
* @param boolean $load whether to load the module if it is not yet loaded.
|
* @param boolean $load whether to load the module if it is not yet loaded.
|
||||||
* @return Module|null the module instance, null if the module does not exist.
|
* @return Module|null the module instance, null if the module does not exist.
|
||||||
* @see hasModule()
|
* @see hasModule()
|
||||||
*/
|
*/
|
||||||
public function getModule($id, $load = true)
|
public function getModule($id, $load = true)
|
||||||
{
|
{
|
||||||
if (strpos($id, '/') === false) {
|
if (($pos = strpos($id, '/')) !== false) {
|
||||||
if (isset($this->_modules[$id])) {
|
// sub-module
|
||||||
if ($this->_modules[$id] instanceof Module) {
|
$module = $this->getModule(substr($id, 0, $pos));
|
||||||
return $this->_modules[$id];
|
return $module === null ? null : $module->getModule(substr($id, $pos + 1), $load);
|
||||||
} elseif ($load) {
|
}
|
||||||
Yii::trace("Loading module: $id", __METHOD__);
|
|
||||||
return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this);
|
if (isset($this->_modules[$id])) {
|
||||||
}
|
if ($this->_modules[$id] instanceof Module) {
|
||||||
|
return $this->_modules[$id];
|
||||||
|
} elseif ($load) {
|
||||||
|
Yii::trace("Loading module: $id", __METHOD__);
|
||||||
|
return $this->_modules[$id] = Yii::createObject($this->_modules[$id], $id, $this);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// it's a sub-module
|
|
||||||
$ids = explode('/', $id);
|
|
||||||
$module = $this;
|
|
||||||
foreach ($ids as $id) {
|
|
||||||
$module = $module->getModule($id);
|
|
||||||
}
|
|
||||||
return $module;
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user