以dz根目錄門戶請求入口文件 portal.php為例,淺析其執行流程。
我們以portal.php為例,用戶請求 http://xxx.xxx.xx/portal.php
接收到用戶請求,腳本執行以下內容:
//定義當前應用ID
define('APPTYPEID', 4);
//定義當前應用腳本名稱
define('CURSCRIPT', 'portal');
//引入dz核心庫文件-每個入口文件都會載入此類
require './source/class/class_core.php';
在引入dz 核心庫文件的時候,做了以下操作(以下代碼位於:source/class/class_core.php)
error_reporting(E_ALL);
define('IN_DISCUZ', true);
//定義dz根目錄
define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -12));
//define('DISCUZ_CORE_DEBUG', false);
//調式模式是否開啟
define('DISCUZ_CORE_DEBUG', true);
//異常處理
set_exception_handler(array('core', 'handleException'));
if(DISCUZ_CORE_DEBUG) {
set_error_handler(array('core', 'handleError'));
register_shutdown_function(array('core', 'handleShutdown'));
}
//類自動加載設置
if(function_exists('spl_autoload_register')) {
spl_autoload_register(array('core', 'autoload'));
} else {
function __autoload($class) {
return core::autoload($class);
}
}
接下來的一句:
C::creatapp();
創建dz的類discuz_application的一個實例,此處代碼位於文件:source/class/discuz/discuz_application.php中
public static function creatapp() {
if(!is_object(self::$_app)) {
self::$_app = discuz_application::instance();
}
return self::$_app;
}
discuz_application::instance() 到底做了什么?
static function &instance() {
static $object;
if(empty($object)) {
$object = new self();
}
return $object;
}
public function __construct() {
//運行環境初始化,在x2.5版本中很重要的$_G就在此處聲明定義
$this->_init_env();
//應用配置初始化 載入source/config/config_global.php中的配置項,定義cookie前綴,靜態文件目錄等
$this->_init_config();
//輸入初始化, 在這里將$_GET和$_POST合並,統一使用$_GET
$this->_init_input();
//輸出初始化 header頭 跨域攻擊xss檢查等
$this->_init_output();
}
具體以上四個初始化方法都做了哪些工作,不再贅述,詳情可參看 : source/class/discuz/discuz_application.php 文件
以上代碼執行完畢,回到portal.php,
$cachelist = array('userapp', 'portalcategory', 'diytemplatenameportal');
$discuz->cachelist = $cachelist;
//應用初始化
$discuz->init();
此句代碼我們再次進入類 discuz_application中
public function init() {
if(!$this->initated) {
$this->_init_db();
$this->_init_setting();
$this->_init_user();
$this->_init_session();
$this->_init_mobile();
$this->_init_cron();
$this->_init_misc();
}
$this->initated = true;
}
上述代碼對數據庫連接,css樣式緩存,用戶信息,session等做了初始化,具體內容可查看源代碼
針對門戶包含的兩個函數文件,其他入口根據情況包含文件有所變化
require DISCUZ_ROOT.'./source/function/function_home.php'; require DISCUZ_ROOT.'./source/function/function_portal.php';
當前操作,定義了門戶中可進行的請求內容
if(empty($_GET['mod']) || !in_array($_GET['mod'], array('list', 'view', 'comment', 'portalcp', 'topic', 'attachment', 'rss', 'block'))) $_GET['mod'] = 'index';
define('CURMODULE', $_GET['mod']);
//運行插件
runhooks();
//此句載入source/module/portal目錄下相關文件,假設$_GET['mod']是 index,則載入該目錄的portal_index.php
require_once libfile('portal/'.$_GET['mod'], 'module');
接下來執行的代碼就位於source/module/portal/protal_xxx.php中了,xxx==$_GET['mod'];
在這個文件中,include_once template('diy:portal/index'); 載入模板,此句執行后,返回給用戶瀏覽器可觀看的頁面
template 模板中所需要的數據,就應該在protal_xxx.php文件中提前准備好
template 具體怎么解析模板, 后面如果有時間,我會繼續分析。
