轉載請註明出處:http://blog.csdn.net/jh_zzzphp
從 index.php 開始看,數據庫
/** 初始化組件 */數組
Typecho_Widget:: widget('Widget_Init' );架構
看 Typecho_Widget:: widget 函數,查找 Widget/Init.php , Widget 下的文件都是從 Typecho_Widget 派生的,這裏建立該對象實例,並將相關的 Request , Response 對象做爲參數傳遞過去,而後調用該對象的 execute 方法。函數
看一下 Init 中的 execute ,首先會初始化一些參數,重點看看 MVC 架構的路由:this
Typecho_Router:: setPathInfo($this -> request-> getPathInfo());url
Typecho_Router:: setRoutes($options -> routingTable);spa
首先設置路徑,而後初始化路由, $options -> routingTable 默認值是在安裝時寫在數據庫中的,運行時再讀出來,Typecho_Router 的 setRoutes 函數調用了 Typecho_Router_Parser 的 parse 函數, parse 函數遍歷整個routingTable 數組,將處理後的路由數組返回給 Typecho_Router ,保存在 $_routingTable 中。一個典型的路由條目以下:.net
(插件
[url] => /
[widget] => Widget_Archive
[action] => render
[regx] => |^[/]?$|
[format] => /
[params] => Array
(
)
)
/** 註冊一個初始化插件 */
Typecho_Plugin:: factory('index.php' )-> begin();
/** 開始路由分發 */
Typecho_Router:: dispatch();
dispatch 首先取到當前的 Url 路徑信息,遍歷 $_routingTable 找到匹配的路由條目,根據路由條目中的 widget ,action 設置加載相應 Widget 並執行其相應的方法,以 / 目錄爲例,最終執行的就是 Archive 的 render 方法。
看一下 Widget_Archived 的 render 方法,這塊比較複雜:
首先獲得設置的主題的目錄:
$themeDir = __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_THEME_DIR__ . '/' . $this -> options-> theme. '/' ;
再往下看:
if (! $validated && ! empty ($this -> _archiveSlug)) {
$themeFile = $this -> _archiveType . '/' . $this -> _archiveSlug . '.php' ;
if (file_exists($themeDir . $themeFile )) {
$this -> _themeFile = $themeFile ;
$validated = true ;
}
}
這裏須要看一下 _archiveType 和 _archiveSlug 是怎麼來的:
在 Widget_Archived 的 execute 函數中會根據 $this -> parameter-> type 執行相應的 handler 。
if (isset ($handles [$this -> parameter-> type])) {
$handle = $handles [$this -> parameter-> type];
$this -> {$handle }($select , $hasPushed );
} else {
$hasPushed = $this -> pluginHandle()-> handle($this -> parameter-> type, $this , $select );
}
$this -> parameter-> type 這個變量是在構造函數中賦值的 :
$this -> parameter-> type = Typecho_Router:: $current ;
Typecho_Router:: $current 根據路由表能夠查到對應於 index.php 就是 index 。
因此對於上面標黃的代碼對應於 index.php 最終執行的是的是 $handles [‘index’] 對應的 handle ,就是 indexHandle。能夠看到對於其餘的 Handle 通常都會設置 _archiveType 及 _archiveSlug 變量, indexHandle 沒有,由於_archiveType 默認就是 index ,因此在 index.php 中 _archiveType 等於 index , _archiveSlug 爲空。
因此根據前面 render 函數中的代碼,最終是找到對應於 $themeDir 下的 $this -> _archiveType . '/' . $this ->_archiveSlug . '.php' 文件,而後直接包含進來,咱們看到的就是這個文件的輸出了。
/** 註冊一個結束插件 */
Typecho_Plugin:: factory('index.php' )-> end();