Wish there were more inline documentation to ME Classes & Methods. Now digging into your code i wonder why you treat $module as an array in Modules->load function?
Is this done to enable autoload() functionality (load an array of modules)?
UPDATE:
I would like to sudgest a small unobtrusive addition to Modules and Controller Class (or discuss some other ways to implement similar functionality).
So let me explain: It is very convenient that besides running modules with modules::run() you can actually load a module or a module sub-controller as library to access it’s functionality from other modules and controllers.
But there’s an inconvenience - you can not pass any additional data along when loading modules as library.
This functionality is very much needed & missing. Sometimes you just need to pass additional data to __construct() to instantiate a library instance.
So this is just a quick fix after some digging your code. This does not change functionality of modules loading as well as loading arrays of modules.
What do you think?
Controller.php Libarry
/** Load a module controller **/
/* Added $module_vars to get an array of parameters for module constructor*/
public function module($module, $module_vars=array())
{
if (is_array($module))
return self::modules($module);
$ci = modules::$registry[$this->_class];
/* get the controller name */
$controller = strtolower(end(explode('/', $module)));
/* pass $module_vars to module loader */
(isset($ci->$controller)) OR $ci->$controller = modules::load($module, $module_vars);
return $ci->$controller;
}
Modules.php Libarry
/** Load a module controller **/
public static function load($module, $module_vars=array())
{
(is_array($module)) AND list($module, $params) = each($module) OR $params = NULL;
/* get the controller class name */
$class = strtolower(end(explode('/', $module)));
/* is controller in registry? */
/* if $module_vars are passed - we need a separate controller instance - don't look up in module regestry*/
if (isset(self::$registry[$class]) && empty($module_vars))
return self::$registry[$class];
/* get the module name */
list($module) = explode('/', $module);
/* find the module controller */
list($module, $controller) = Router::locate(array($module, $class));
if ($module === FALSE) return;
/* set the module directory */
self::$home = $module;
$path = ($module) ? MODOFFSET.$module.'/controllers/' : NULL;
/* load the module controller class */
self::load_file($controller, APPPATH.'controllers/'.$path);
$class = ucfirst($controller);
/* create the new controller */
/* added some logic to avoid $params/$module_vars collision */
if(empty($module_vars)) $controller = new $class($params);
else $controller = new $class($module_vars);
return $controller;
}
Looking forward to hear any thoughts & ideas!
Cheers! 11
