Drupal中的模块载入

什么是模块载入?首先说载入,这里的载入是指require_once。模块载入就是指require_once模块目录中的某个PHP文件。

每个Drupal模块都应该有自己的主文件。模块主文件以模块名开始,以.module为后缀。例如blog模块,其主文件就是blog.module。drupal_load()函数用来完成载入模块主文件:

function drupal_load($type, $name) {
static $files = array(); if (isset($files[$type][$name])) {
return TRUE;
} $filename = drupal_get_filename($type, $name); if ($filename) {
include_once DRUPAL_ROOT . '/' . $filename;
$files[$type][$name] = TRUE;
return TRUE;
} return FALSE;
}

drupal_load()不止限于载入模块主文件,其它的Drupal资源主文件也可以载入。更多信息可以参考《Drupal所能够理解的资源》。

模块目录中不仅有主文件,还可以有其它的PHP文件。例如blog模块中有一个test文件。按照Drupal的命名原则,这个文件必须命名为blog.test,以模块名开始,以文件类型为后缀。使用module_load_include()函数可以实现模块目录中非主文件的载入:

function module_load_include($type, $module, $name = NULL) {
if (!isset($name)) {
$name = $module;
} if (function_exists('drupal_get_path')) {
$file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
if (is_file($file)) {
require_once $file;
return $file;
}
} return FALSE;
}

Drupal为install文件的载入实现了module_load_install()函数,但实质还是对module_load_include()的简单封装:

function module_load_install($module) {
// Make sure the installation API is available
include_once DRUPAL_ROOT . '/includes/install.inc';
return module_load_include('install', $module);
}

drupal_load()和module_load_include()都只是载入单个模块中的某种类型文件,Drupal还为这两个函数实现了对应的批量载入函数module_load_all()和module_load_all_includes(),用于Drupal启动过程中,方便地一次载入所有激活的模块:

function module_load_all($bootstrap = FALSE) {
static $has_run = FALSE; if (isset($bootstrap)) {
foreach (module_list(TRUE, $bootstrap) as $module) {
drupal_load('module', $module);
}
// $has_run will be TRUE if $bootstrap is FALSE.
$has_run = !$bootstrap;
}
return $has_run;
} function module_load_all_includes($type, $name = NULL) {
$modules = module_list();
foreach ($modules as $module) {
module_load_include($type, $module, $name);
}
}

这两个函数的关键是module_list(),该函数返回当前激活的模块名称列表:

function module_list($refresh = FALSE, $bootstrap_refresh = FALSE, $sort = FALSE, $fixed_list = NULL) {
static $list = array(), $sorted_list; if (empty($list) || $refresh || $fixed_list) {
$list = array();
$sorted_list = NULL;
if ($fixed_list) {
foreach ($fixed_list as $name => $module) {
drupal_get_filename('module', $name, $module['filename']);
$list[$name] = $name;
}
}
else {
if ($refresh) {
// For the $refresh case, make sure that system_list() returns fresh
// data.
drupal_static_reset('system_list');
}
if ($bootstrap_refresh) {
$list = system_list('bootstrap');
}
else {
// Not using drupal_map_assoc() here as that requires common.inc.
$list = array_keys(system_list('module_enabled'));
$list = (!empty($list) ? array_combine($list, $list) : array());
}
}
}
if ($sort) {
if (!isset($sorted_list)) {
$sorted_list = $list;
ksort($sorted_list);
}
return $sorted_list;
}
return $list;
}

module_list()也只是对system_list()的封装,关于system_list()可以参看《Drupal的system_list()函数解析》。

上一篇:使用linux backtrace打印出错函数堆栈信息


下一篇:Win下,通过Jstack截取Java进程中的堆栈信息