PHP插件设计钩子(hook)与简单应用插件的思想
发布时间:2017-11-02, 15:59:06 分类:PHP | 编辑 off 网址 | 辅助
正文 2455字数 366,649阅读
定义目录
├─plugin // 插件目录
│ ├─plugin1 // 插件1
│ │ ├─config.php // 插件1的配置项
│ │ ├─index.php // 插件1的程序处理内容
│ ├─plugin2
│ │ ├─config.php
│ │ ├─index.php
│ ├─plugin3
│ │ ├─config.php
│ │ ├─index.php
│ ├─...
├─index.php // 业务逻辑
Run code
Cut to clipboard
业务逻辑
<?php
class Test{
public function index(){
// 用户注册成功
// 获取全部插件
$pluginList=scandir('./plugin/');
// 循环插件 // 排除. ..
foreach ($pluginList as $k => $v) {
if ($v=='.' || $v=='..') {
unset($pluginList[$k]);
}
}
echo "简易后台管理<hr>";
// 插件管理
foreach ($pluginList as $k => $v) {
// 获取配置项
$config=include './plugin/'.$v.'/config.php';
$word=$config['status']==1 ? '点击关闭' : '点击开启';
echo $config['title'].'<a href="./index.php?change='.$v.'">'.$word.'</a><br />';
}
echo '<hr>';
// 输出插件内容
foreach ($pluginList as $k => $v) {
// 获取配置项
$config=include './plugin/'.$v.'/config.php';
if ($config['status']==1) {
include './plugin/'.$v.'/index.php';
// 运行插件
Hook::run($v);
}
}
// 前往网站首页
}
}
// 插件类
class Hook{
// 注册添加插件
public static function add($name,$func){
$GLOBALS['hookList'][$name][]=$func;
}
// 执行插件
public static function run($name,$params=null){
foreach ($GLOBALS['hookList'][$name] as $k => $v) {
call_user_func($v,$params);
}
}
}
// 更改插件状态
if (isset($_GET['change'])) {
// 获取到配置项
$config=include './plugin/plugin'.substr($_GET['change'],-1).'/config.php';
// 如果是开启 那就关闭 如果是关闭 则开启
$config['status']=$config['status']==1 ? 0: 1;
// 将更改后的配置项写入到文件中
$str="<?php \r\n return ".var_export($config,true).';';
file_put_contents('./plugin/'.$_GET['change'].'/config.php', $str);
header('Location:./');
}
$test=new Test();
$test->index();
Run code
Cut to clipboard
插件配置项
<?php
return array (
'status' => 1, // 定义状态 1表示开启 0表示关闭
'title' => '发送短信', // 插件的名称
);
Run code
Cut to clipboard
插件内容
<?php
Hook::add('plugin1',function(){
echo '发送短信的内容<br />';
});
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
<?php /** * * 插件机制的实现核心类 */ class PluginManager { /** * 监听已注册的插件 * * @access private * @var array */ private $_listeners = array(); /** * 构造函数 * * @access public * @return void */ public function __construct() { #这里$plugin数组包含我们获取已经由用户激活的插件信息 #为演示方便,我们假定$plugin中至少包含 #$plugin = array( # 'name' => '插件名称', # 'directory'=>'插件安装目录' #); $plugins = get_active_plugins();#这个函数请自行实现 if($plugins) { foreach($plugins as $plugin) {//假定每个插件文件夹中包含一个actions.php文件,它是插件的具体实现 if (@file_exists(STPATH .'plugins/'.$plugin['directory'].'/actions.php')) { include_once(STPATH .'plugins/'.$plugin['directory'].'/actions.php'); $class = $plugin['name'].'_actions'; if (class_exists($class)) { //初始化所有插件 new $class($this); } } } } #此处做些日志记录方面的东西 } /** * 注册需要监听的插件方法(钩子) * * @param string $hook * @param object $reference * @param string $method */ function register($hook, &$reference, $method) { //获取插件要实现的方法 $key = get_class($reference).'->'.$method; //将插件的引用连同方法push进监听数组中 $this->_listeners[$hook][$key] = array(&$reference, $method); #此处做些日志记录方面的东西 } /** * 触发一个钩子 * * @param string $hook 钩子的名称 * @param mixed $data 钩子的入参 * @return mixed */ function trigger($hook, $data='') { $result = ''; //查看要实现的钩子,是否在监听数组之中 if (isset($this->_listeners[$hook]) && is_array($this->_listeners[$hook]) && count($this->_listeners[$hook]) > 0) { // 循环调用开始 foreach ($this->_listeners[$hook] as $listener) { // 取出插件对象的引用和方法 $class =& $listener[0]; $method = $listener[1]; if(method_exists($class,$method)) { // 动态调用插件的方法 $result .= $class->$method($data); } } } #此处做些日志记录方面的东西 return $result; } }
<?php /** * 这是一个Hello World简单插件的实现 */ /** *需要注意的几个默认规则: * 1. 本插件类的文件名必须是action * 2. 插件类的名称必须是{插件名_actions} */ class DEMO_actions { //解析函数的参数是pluginManager的引用 function __construct(&$pluginManager) { //注册这个插件 //第一个参数是钩子的名称 //第二个参数是pluginManager的引用 //第三个是插件所执行的方法 $pluginManager->register('demo', $this, 'say_hello'); } function say_hello() { echo 'Hello World'; } }
$pluginManager->trigger('demo','');