I’m building a application where I give the admin the option to activate/deactivate modules using the web administration so he doesn’t have to edit the configuration files manually so I need to know what modules are inside the module/ folder. I could parse the folder and see what’s inside but it may be a better solution.
Something like Yii::app ()->getModules () but with a result which includes the loaded/unloaded modules. Is there such a method I can use?
/**
* Find a module searching in application modules and if it's not found there
* looks in modules' modules
* @param String $moduleID The model to find
* @return The module, if it's found else null
*/
public static function findModule($moduleID) {
if(Yii::app()->getModule($moduleID)) {
return Yii::app()->getModule($moduleID);
}
$modules = Yii::app()->getModules();
foreach ($modules as $mod=>$conf) {
if(Yii::app()->getModule($mod)) {
return self::findInModule(Yii::app()->getModule($mod), $moduleID);
}
}
return null;
}
/**
* Search for a child module
* @param String $parent The parent module
* @param String $moduleID The module to find
* @return The module, if it's not found returns null
*/
private static function findInModule($parent, $moduleID) {
if ($parent->getModule($moduleID)) {
return $parent->getModule($moduleID);
} else {
$modules = $parent->getModules();
foreach ($modules as $mod => $conf) {
return $this->findInModule($parent->getModule($mod), $moduleID);
}
}
return null;
}
Thanks to jerry2801, your functions put me in the right way.
I’ve noticed errors when I was trying to find a nested module. I’ll figured it out, I posted my code for those who need it too.
/**
* Find a module searching in application modules and if it's not found there
* looks in modules' modules
* @param String $moduleID The model to find
* @return The module, if it's found else null
*/
public static function findModule($moduleID) {
if(Yii::app()->getModule($moduleID)) {
return Yii::app()->getModule($moduleID);
}
$modules = Yii::app()->getModules();
foreach ($modules as $mod => $conf) {
if(Yii::app()->getModule($mod)) {
$module = self::findInModule(Yii::app()->getModule($mod), $moduleID);
if($module)
return $module;
}
}
return null;
}
/**
* Search for a child module
* @param String $parent The parent module
* @param String $moduleID The module to find
* @return The module, if it's not found returns null
*/
private static function findInModule($parent, $moduleID) {
if ($parent->getModule($moduleID)) {
return $parent->getModule($moduleID);
} else {
$modules = $parent->getModules();
if(!empty($modules)) { // Here was the principal error, when $modules is empty, it returns a CBaseController object.
foreach ($modules as $mod => $conf) {
return $this->findInModule($parent->getModule($mod), $moduleID);
}
}
}
return null;
}