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()函数解析》。
Drupal中的模块载入的更多相关文章
- 解密javascript模块载入器require.js
require.config require.config设置require.js模板载入选项 // 定义config req.config = function (config) { return ...
- 在nodejs中引进模块要经历的步骤
在nodejs中引入模块需要经历如下3个步骤 1.路径分析 2.文件定位 3.编译执行 在nodejs中模块分为两类,一类是nodejs提供的模块,称为核心模块,另一类的用户编写的模块,称为文件模块. ...
- Lua中的模块与module函数详解
很快就要开始介绍Lua里的“面向对象”了,在此之前,我们先来了解一下Lua的模块. 1.编写一个简单的模块 Lua的模块是什么东西呢?通常我们可以理解为是一个table,这个table里有一些变量.一 ...
- drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法二
drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法一 中介绍了ckeditor的安装和配置方法,其实还有另一种新方法,不用IMCE模块. 不过需要ckfinder的JS库,可以 ...
- python中os模块中文帮助
python中os模块中文帮助 python中os模块中文帮助文档文章分类:Python编程 python中os模块中文帮助文档 翻译者:butalnd 翻译于2010.1.7——2010.1.8 ...
- PCL中outofcore模块---基于核外八叉树的大规模点云的显示
写在前面 最近公众号的活动让更多的人加入交流群,尝试提问更多的我问题,群主也在积极的招募更多的小伙伴与我一起分享,能够相互促进. 这里总结群友经常问,经常提的两个问题,并给出我的回答: (1) ...
- node 模块载入原理【1】
简单介绍 我们会从简单的模块载入原理来开始,尝试阅读下 Node.js 源代码.首先我们知道 Node.js 的源代码主要是由 C++ 和 JavaScript 编写的,JS 部分主要在 lib 目录 ...
- 隐藏进程中的模块绕过IceSword的检测
标 题: [原创] 隐藏进程中的模块绕过IceSword的检测 作 者: xPLK 时 间: 2008-06-19,17:59:11 链 接: http://bbs.pediy.com/showthr ...
- 浅析JS中的模块规范(CommonJS,AMD,CMD)////////////////////////zzzzzz
浅析JS中的模块规范(CommonJS,AMD,CMD) 如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已. ...
随机推荐
- ES5 的 Array
1: Array.isArray判断是否为数组 Array.isArray([1, 2, 3]); // true Array.isArray({foo: 123}); // false // Pol ...
- 【欧拉回路】【欧拉路径】【Fleury算法】CDOJ1634 记得小苹初见,两重心字罗衣
Fleury算法看这里 http://hihocoder.com/problemset/problem/1181 把每个点看成边,每个横纵坐标看成一个点,得到一个无向图. 如果新图中每个点的度都是偶数 ...
- Android Studio自动化快速实现Parcelable接口序列化
1.在线安装 然后打开File -> Settings -> Pugins -> Browse Repositories 如下,输入android parcelable code g ...
- 模式匹配之Kmp算法
Kmp: 算法定义借鉴wikipedia: http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm#KMP_ ...
- uboot如何检测XC2440是从Nand或Nor启动
转:http://blog.chinaunix.net/uid-22030783-id-3347621.html 在XC2440开发板上做uboot从nandflash启动时,需要检测硬件启动方式,启 ...
- Nand Flash与Nor
转:http://www.360doc.com/content/11/1215/15/1299815_172458274.shtml Flash经常在一些地方被提到,一直没认真去理解它们的区别,因此, ...
- Xcode8从相册选图片
使用Xcode8写自己的东西有一段时间了,在使用Xcode8编程时不得不说我特别喜欢改后的默认字体,哈哈,当然默认字体是可以调回去的,只不过默认的字体感觉看起来比以前舒服了,毕竟不会像之前那么”字正腔 ...
- text-align 属性,输入框数字向右靠
1.业务需求:金额输入框数字向右靠 2.HTML文件 <td id="otherPay_Td"> <input id="otherPay" t ...
- html Frame、Iframe、Frameset 的区别 详细出处参考:http://www.jb51.net/web/22785.html
10.4.1 Frameset与Frame的区别首先讲解Frameset与Frame之间的区别. 用来划分框架,每一个框架由标记.必须在之内使用,代码如下: 在上面的例子当中,把页面分为左右两个部分, ...
- JQUERY中的事件处理:RETURN FALSE、阻止默认行为、阻止冒泡以及兼容性问题
return false 在jQuery中,我们常用return false来阻止浏览器的默认行为,那"return false"到底做了什么? 当你每次调用"retur ...