在一个封闭企业环境下这样搞是没有问题的;
然鹅;我们还有一位开放无私的猿领导要把程序开源出去造福其他猿类;
希望有更多的猿类来参与这个项目;共同开发功能;
如果大家都去改动这套程序;把自己的代码扔在 用户注册成功 和 跳转到首页 之间;
这显然是不靠谱的;想想都混乱的一塌糊涂;
那可不可以大家把自己写的代码放到某个目录下;
然后系统自动的根据配置项把这些代码加载到 用户注册成功 和 跳转到首页 之间呢?
好先定义如下目录
├─plugin // 插件目录
│ ├─plugin1 // 插件1
│ │ ├─config.php //
插件1的配置项
│ │ ├─index.php // 插件1的程序处理内容
│ ├─plugin2
│ │
├─config.php
│ │ ├─index.php
│ ├─plugin3
│ │ ├─config.php
│ │
├─index.php
│ ├─...
├─index.php // 业务逻辑
业务逻辑的代码:
$v) {
if ($v=='.' || $v=='..') {
unset($pluginList[$k]);
}
}
echo "简易后台管理";
// 插件管理
foreach ($pluginList as $k => $v) {
// 获取配置项
$config=include './plugin/'.$v.'/config.php';
$word=$config['status']==1 ? '点击关闭' : '点击开启';
echo $config['title'].''.$word.'
';
}
echo '';
// 输出插件内容
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="index();
插件配置项代码:
1, // 定义状态 1表示开启 0表示关闭
'title' => '发送短信', // 插件的名称
);