Commit 9945bca5 authored by Dries's avatar Dries

- Patch #9650 by Adrian: this change introduces a module_load function, which maintains a list of modules that have already been loaded in a static array, and will not load another module of the same name, or if the file does not exist.

Modules can be stored anywhere, as there is now a set of functions called module_get_filename, and module_set_filename .. which allow system_listing and module_list to specify the locations of the files.

A new function module_load_all() replaces the hardcoded includes in module_init, and loads all modules which have been enabled, using module_load.

module_listing no longer includes files itself, instead it just keeps the listing (and sets the filenames).

This patch is a requirement for the multisite configuration patch, as overriding modules are currently being loaded due to the only protection of loading them is include_once.
parent 202eee42
......@@ -13,12 +13,7 @@
* system_listing() and module_list().
*/
function module_init() {
require_once 'modules/admin.module';
require_once 'modules/filter.module';
require_once 'modules/system.module';
require_once 'modules/user.module';
require_once 'modules/watchdog.module';
module_list();
module_load_all();
module_invoke_all('init');
}
......@@ -34,9 +29,6 @@ function module_iterate($function, $argument = '') {
/**
* Collect a list of all installed and enabled modules.
*
* If any modules are enabled but have not yet been loaded, this function performs
* the include_once() to load them.
*
* @param $refresh
* Whether to force the module list to be regenerated (such as after the
* administrator has changed the system settings).
......@@ -70,7 +62,7 @@ function module_list($refresh = FALSE, $bootstrap = FALSE) {
$throttle = ($module->throttle && variable_get('throttle_level', 0) > 4);
if (!$throttle) {
$list[$module->name] = $module->name;
include_once $module->filename;
module_set_filename($module->name, $module->filename);
}
}
}
......@@ -79,6 +71,90 @@ function module_list($refresh = FALSE, $bootstrap = FALSE) {
return $list;
}
/**
* Set the filename of a module, for future loading through module_load()
*
* @param $module
* Name of the module which to specify the filename of.
* @param $pa
* Filename of the module named $module.
* @return
* Filename of module, if no $path has been specified.
*/
function module_set_filename($module, $path = null) {
static $list;
if ($path) {
$list[$module] = $path;
}
else {
return $list[$module] ? $list[$module] : "modules/$module.module";
}
}
/**
* Retrieve the filename of a module
*
* @param $module
* Name of the module which to retrieve the filename of.
* @return
* Filename of module.
*/
function module_get_filename($module) {
return module_set_filename($module);
}
/**
* Retrieve the path of a module
*
* @param $module
* Name of the module which to retrieve the path of.
* @return
* Path of module.
*/
function module_get_path($module) {
return dirname(module_get_filename($module));
}
/**
* Load a module into Drupal, but check first wether a module by the same name
* has been loaded, and that the filename being included exists.
* @param $module
* The name of the module to be loaded.
* @return
* TRUE if the load was successfull.
*/
function module_load($module) {
static $loaded = array();
if (!$loaded[$module]) {
$filename = module_get_filename($module);
if (file_exists($filename)) {
include_once($filename);
$loaded[$module] = $filename;
return true;
}
}
return false;
}
/**
* Load all the modules that have been enabled in the system table.
*
* @return
* TRUE if all modules were loaded successfully
*/
function module_load_all() {
$list = module_list();
$status = true;
foreach ($list as $module) {
$status = $status && module_load($module);
}
return $status;
}
/**
* Determine whether a given module exists.
*
......
......@@ -245,7 +245,9 @@ function system_listing($type) {
}
foreach ($files as $filename => $file) {
include_once($filename);
module_set_filename($file->name, $filename);
module_load($file->name);
if ($type == 'module') {
$info->name = module_invoke($file->name, 'help', 'admin/modules#name') ? module_invoke($file->name, 'help', 'admin/modules#name') : module_invoke($file->name, 'system', 'name') ? module_invoke($file->name, 'system', 'name') : $file->name;
$info->description = module_invoke($file->name, 'help', 'admin/modules#description') ? module_invoke($file->name, 'help', 'admin/modules#description') : module_invoke($file->name, 'system', 'description');
......
......@@ -245,7 +245,9 @@ function system_listing($type) {
}
foreach ($files as $filename => $file) {
include_once($filename);
module_set_filename($file->name, $filename);
module_load($file->name);
if ($type == 'module') {
$info->name = module_invoke($file->name, 'help', 'admin/modules#name') ? module_invoke($file->name, 'help', 'admin/modules#name') : module_invoke($file->name, 'system', 'name') ? module_invoke($file->name, 'system', 'name') : $file->name;
$info->description = module_invoke($file->name, 'help', 'admin/modules#description') ? module_invoke($file->name, 'help', 'admin/modules#description') : module_invoke($file->name, 'system', 'description');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment