什么是模块载入?首先说载入,这里的载入是指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中的模块载入的更多相关文章

  1. 解密javascript模块载入器require.js

    require.config require.config设置require.js模板载入选项 // 定义config req.config = function (config) { return ...

  2. 在nodejs中引进模块要经历的步骤

    在nodejs中引入模块需要经历如下3个步骤 1.路径分析 2.文件定位 3.编译执行 在nodejs中模块分为两类,一类是nodejs提供的模块,称为核心模块,另一类的用户编写的模块,称为文件模块. ...

  3. Lua中的模块与module函数详解

    很快就要开始介绍Lua里的“面向对象”了,在此之前,我们先来了解一下Lua的模块. 1.编写一个简单的模块 Lua的模块是什么东西呢?通常我们可以理解为是一个table,这个table里有一些变量.一 ...

  4. drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法二

    drupal中安装CKEditor文本编辑器,并配置图片上传功能 之 方法一 中介绍了ckeditor的安装和配置方法,其实还有另一种新方法,不用IMCE模块. 不过需要ckfinder的JS库,可以 ...

  5. python中os模块中文帮助

    python中os模块中文帮助   python中os模块中文帮助文档文章分类:Python编程 python中os模块中文帮助文档 翻译者:butalnd 翻译于2010.1.7——2010.1.8 ...

  6. PCL中outofcore模块---基于核外八叉树的大规模点云的显示

    写在前面   最近公众号的活动让更多的人加入交流群,尝试提问更多的我问题,群主也在积极的招募更多的小伙伴与我一起分享,能够相互促进.   这里总结群友经常问,经常提的两个问题,并给出我的回答: (1) ...

  7. node 模块载入原理【1】

    简单介绍 我们会从简单的模块载入原理来开始,尝试阅读下 Node.js 源代码.首先我们知道 Node.js 的源代码主要是由 C++ 和 JavaScript 编写的,JS 部分主要在 lib 目录 ...

  8. 隐藏进程中的模块绕过IceSword的检测

    标 题: [原创] 隐藏进程中的模块绕过IceSword的检测 作 者: xPLK 时 间: 2008-06-19,17:59:11 链 接: http://bbs.pediy.com/showthr ...

  9. 浅析JS中的模块规范(CommonJS,AMD,CMD)////////////////////////zzzzzz

    浅析JS中的模块规范(CommonJS,AMD,CMD)   如果你听过js模块化这个东西,那么你就应该听过或CommonJS或AMD甚至是CMD这些规范咯,我也听过,但之前也真的是听听而已.     ...

随机推荐

  1. EasyUI学习总结(五)——EasyUI组件使用(转载)

    本文转载自:http://www.cnblogs.com/xdp-gacl/p/4084520.html 一.EasyUI组件的简单介绍 easyUI提供了很多组件让我们使用,如下图所示:

  2. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题

    B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...

  3. BZOJ 4031: [HEOI2015]小Z的房间 高斯消元 MartixTree定理 辗转相除法

    4031: [HEOI2015]小Z的房间 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个 ...

  4. css3背景属性 background-size 对背景图进行缩小放大

    background-size需要两个值,它的类型可以是像素(px).百分比(%)或是auto,还可以是cover和contain.第一个值为背景图的width,另外一个值用于指定背景图上的heigh ...

  5. PHP 正则表达式匹配 img ,PHP 正则提取或替换图片 img 标记中的任意属性。

    PHP正则提取或替换img标记属性 PHP 正则表达式匹配 img ,PHP 正则提取或替换图片 img 标记中的任意属性.   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  6. 莫名其妙的float:left; 不能使元素紧贴父级的坑

    这是项目中遇到的一个CSS的坑,做个记录,主要的原因还是浮动后脱离文档流,两个浮动的元素处于同一文档流中会相互影响位置的问题: 先上代码吧: 效果预览地址:浮动不能靠左的情况; 原本红色模块应该处于蓝 ...

  7. 【IntelliJ IDEA】在idea上操作 git分支合并【如何将远程swagger分支 合并到 远程 master分支上】【如何切换 本地分支】

    ============================================ 明确一点: 如果项目交给git管理了[如何将项目交给git管理:https://www.cnblogs.com ...

  8. 突破,Objective-C开发速学手册

    <突破,Objective-C开发速学手册> 基本信息 作者: 傅志辉 出版社:电子工业出版社 ISBN:9787121207426 上架时间:2013-7-12 出版日期:2013 年8 ...

  9. java常用命令行

    1.javac(编译java源文件) javac是用来编译.java文件的. 例子: package com.fjassa.domain;  public class Human.public cla ...

  10. ylbtech-LanguageSamples-Nullable(可以为 null 的类型)

    ylbtech-Microsoft-CSharpSamples:ylbtech-LanguageSamples-Nullable(可以为 null 的类型) 1.A,示例(Sample) 返回顶部 “ ...