转载请注明出处:http://blog.csdn.net/jh_zzz

以 index.php 为例:

/** 初始化组件 */

Typecho_Widget:: widget('Widget_Init' );

Init 的 execute 中会初始化 Typecho_Plugin ,这里 $options -> plugins 是从数据库读出来后反序列化的:

Typecho_Plugin:: init($options -> plugins);

init 中分别将 plugins 中的 activated 和 handles 单独保存,打印出来形式是这样的:

[activated] => Array

(

[HelloWorld] => Array

(

[handles] => Array

(

[admin/menu.php:navBar] => Array

(

[0] => Array

(

[0] => HelloWorld_Plugin

[1] => render

)

)

)

)

)

[handles] => Array

(

[admin/menu.php:navBar] => Array

(

[0] => Array

(

[0] => HelloWorld_Plugin

[1] => render

)

)

)

继续看 index.php :

/** 注册一个初始化插件 */

Typecho_Plugin:: factory('index.php' )-> begin();

… 略过 …

/** 注册一个结束插件 */

Typecho_Plugin:: factory('index.php' )-> end();

Typecho_Plugin:: factory 会根据 ’index.php’ 创建一个新的 Typecho_Plugin 。接下来的 begin() , end() 实际上都是不存在的,于是魔鬼方法 __call 被执行,

$component $this -> _handle ':' $component ;

$last count($args );

$args [$last $last $args [0 ] false ;

if (isset (self:: $_plugins ['handles' ][$component ])) {

$args [$last NULL ;

$this -> _signal true ;

foreach (self:: $_plugins ['handles' ][$component ] as $callback ) {

$args [$last call_user_func_array($callback $args );

}

}

__call 查找对应 index.php:begin 的 Typecho_Plugin ,如果找到的话,就会调用相应的方法。例如如果找到的是HelloWorld_Plugin ,则 HelloWorld_Plugin.render() 会被执行。

(

[0] => HelloWorld_Plugin

[1] => render

)

简单说一下 Plugin 是如何加载的,在 config.inc.php 中首先设置了包含路径,插件路径也在其中:

/** 设置包含路径 */

set_include_path(get_include_path() PATH_SEPARATOR .

__TYPECHO_ROOT_DIR__ '/var' PATH_SEPARATOR .

__TYPECHO_ROOT_DIR__ __TYPECHO_PLUGIN_DIR__ );

HelloWorld_Plugin 此时尚未被加载,所以当执行到 HelloWorld_Plugin.render() 时 ,Typecho_Common::__autoLoad 函数被执行,这里会自动加载指定的插件文件:

include_once str_replace('_' , '/' , $className '.php' ;

例如对于 HelloWorld_Plugin ,文件就是 HelloWorld/Plugin.php ,因为 usr/plugin 目录已经在包含的路径中,所以这个文件可以正常加载。

当初我学习 php 的时候还是 php3 ,现在一些新特性我都不知道,这一段我看了半天才搞清楚,这次读这些代码了解了不少 php 的新特性:)

Typecho 代码阅读笔记(三) - 插件机制的更多相关文章

  1. Typecho 代码阅读笔记(一) - 页面渲染及路由机制

    转载请注明出处:http://blog.csdn.net/jh_zzz 从 index.php 开始看, /** 初始化组件 */ Typecho_Widget:: widget('Widget_In ...

  2. Typecho 代码阅读笔记(二) - 数据库访问

    转载请注明出处:http://blog.csdn.net/jh_zzz 这一块比较复杂,我还没有完全理解为什么要把 SQL 语句的组装搞这么复杂. 从一个普通皮肤页面开始 themes/default ...

  3. <<梦断代码>>阅读笔记三

    看完了这最后三分之一的<梦断代码>,意味着这本软件行业的著作已经被我粗略地过了一遍. 在这最后三分之一的内容中,我深入了解了在大型软件项目的运作过程中存在的困难和艰辛.一个大型软件项目的成 ...

  4. Mongodb源代码阅读笔记:Journal机制

    Mongodb源代码阅读笔记:Journal机制 Mongodb源代码阅读笔记:Journal机制 涉及的文件 一些说明 PREPLOGBUFFER WRITETOJOURNAL WRITETODAT ...

  5. Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  6. [置顶] Linux协议栈代码阅读笔记(一)

    Linux协议栈代码阅读笔记(一) (基于linux-2.6.21.7) (一)用户态通过诸如下面的C库函数访问协议栈服务 int socket(int domain, int type, int p ...

  7. [置顶] Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...

  8. Linux-3.0.8 input subsystem代码阅读笔记

    先乱序记录一下阅读Linux input subsystem代码的笔记. 在input device driver的入口代码部分,需要分配并初始化input device结构,内核提供的API是inp ...

  9. 论文阅读笔记三十七:Grid R-CNN(CVPR2018)

    论文源址:https://arxiv.org/abs/1811.12030 开源代码:未公开 摘要 本文提出了目标检测网络Grid R-CNN,其基于网格定位机制实现准确的目标检测.传统方法主要基于回 ...

随机推荐

  1. Codeforces 474D Flowers

    http://codeforces.com/problemset/problem/474/D 思路:F[i]=F[i-1]+(i>=K)F[i-k] #include<cstdio> ...

  2. HttpStatusCode 枚举

    .NET Framework 类库 HttpStatusCode 枚举 包含为 HTTP 定义的状态代码的值. 命名空间:System.Net程序集:System(在 system.dll 中)   ...

  3. PHPDocumentor安装与使用

    phpDocuemtor官网:http://www.phpdoc.org/ 通过pear安装,进入dos的php目录,输入pear install -a PhpDocumentor.如果想使用web接 ...

  4. HDU 4611Balls Rearrangement(思维)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  5. 自己写jstl标签解析long时间

    数据库里存储的是long型的时间,现在想输出到jsp页面,由于使用的是jstl标签,而要显示的是可读的时间类型,找来找去有个fmt:formatDate可以转化,但是只能转date型,long型则不可 ...

  6. 创建UILabel

    UILabelCreate.h #import <UIKit/UIKit.h> @interface UILabelCreate : UILabel /** * 创建UILabel 初始化 ...

  7. 十分钟学会写shell脚本

    大家好!我是handsomecui,下面我为大家讲解一下shell脚本的写法,讲的不好的地方,欢迎大家留言拍砖. 1.在linux下会写shell脚本是非常重要的,下面我参照例子给大家展示几个脚本,顺 ...

  8. java获取指定长度随机数(版本1)

    获取指定长度随机数,含大小写字母和数字 package org.sw; import java.util.Random; /** * 得到指定位数的随机数 * @author mengzw * @si ...

  9. SQL:deferrable initially deferred

    SQL> create table cust(id number,name varchar2(10));Table created SQL> alter table cust add co ...

  10. 虚拟化之docker安装篇

    1,docker pull centos     下载centos镜像 docker search centos  搜索镜像 2,docker images           查看本地镜像 3,do ...