Typecho 代码阅读笔记(三) - 插件机制
Typecho 代码阅读笔记(二) - 数据库访问
Typecho 代码阅读笔记(一) - 页面渲染及路由机制
转载请注明出处:http://blog.youkuaiyun.com/jh_zzz
从 index.php 开始看,
/** 初始化组件 */
Typecho_Widget:: widget('Widget_Init' );
看 Typecho_Widget:: widget 函数,查找 Widget/Init.php , Widget 下的文件都是从 Typecho_Widget 派生的,这里创建该对象实例,并将相关的 Request , Response 对象作为参数传递过去,然后调用该对象的 execute 方法。
看一下 Init 中的 execute ,首先会初始化一些参数,重点看看 MVC 架构的路由:
Typecho_Router:: setPathInfo($this -> request-> getPathInfo());
Typecho_Router:: setRoutes($options -> routingTable);
首先设置路径,然后初始化路由, $options -> routingTable 默认值是在安装时写在数据库中的,运行时再读出来, Typecho_Router 的 setRoutes 函数调用了 Typecho_Router_Parser 的 parse 函数, parse 函数遍历整个 routingTable 数组,将处理后的路由数组返回给 Typecho_Router ,保存在 $_routingTable 中。一个典型的路由条目如下:
(
[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();