完善扩展插件机制

This commit is contained in:
2020-04-13 10:40:47 +08:00
parent 6560f723cd
commit 53ccc5f123
11 changed files with 167 additions and 142 deletions
+16 -30
View File
@@ -1,36 +1,22 @@
<?php <?php
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ] // | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved. // | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn> // | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
return array( return [
'title'=>array(//配置在表单中的键名 ,这个会是config[title] ['name' => 'title', 'title' => '显示标题', 'type' => 'text', 'value' => 'SentCMS开发团队'],
'title'=>'显示标题:',//表单的文字 ['name' => 'width', 'title' => '显示宽度', 'type' => 'select', 'value' => '6', 'option' => [
'type'=>'text', //表单的类型:text、textarea、checkbox、radio、select等 ['key'=>'1', 'label'=>'1格'],
'value'=>'SentCMS开发团队', //表单的默认值 ['key'=>'2', 'label'=>'2格'],
), ['key'=>'4', 'label'=>'4格'],
'width'=>array( ['key'=>'6', 'label'=>'6格']
'title'=>'显示宽度:', ]],
'type'=>'select', ['name' => 'display', 'title' => '是否显示', 'type' => 'select', 'value' => '1', 'option' => [
'options'=>array( ['key'=>'1', 'label'=>'显示'],
'1'=>'1格', ['key'=>'0', 'label'=>'不显示']
'2'=>'2格', ]],
'4'=>'4格', ];
'6'=>'6格'
),
'value'=>'6'
),
'display'=>array(
'title'=>'是否显示:',
'type'=>'radio',
'options'=>array(
'1'=>'显示',
'0'=>'不显示'
),
'value'=>'1'
)
);
+15 -29
View File
@@ -1,35 +1,21 @@
<?php <?php
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ] // | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved. // | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com> // | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
return array( return [
'title'=>array(//配置在表单中的键名 ,这个会是config[title] ['name' => 'title', 'title' => '显示标题', 'type' => 'text', 'value' => '系统信息'],
'title'=>'显示标题:',//表单的文字 ['name' => 'width', 'title' => '显示宽度', 'type' => 'select', 'value' => '12', 'option' => [
'type'=>'text', //表单的类型:text、textarea、checkbox、radio、select等 ['key'=>'4', 'label'=>'4格'],
'value'=>'系统信息', //表单的默认值 ['key'=>'6', 'label'=>'6格'],
), ['key'=>'12', 'label'=>'12格']
'width'=>array( ]],
'title'=>'显示宽度:', ['name' => 'display', 'title' => '是否显示', 'type' => 'select', 'value' => '1', 'option' => [
'type'=>'select', ['key'=>'1', 'label'=>'显示'],
'options'=>array( ['key'=>'0', 'label'=>'不显示']
'4'=>'4格', ]],
'6'=>'6格', ];
'12'=>'12格',
),
'value'=>'2'
),
'display'=>array(
'title'=>'是否显示:',
'type'=>'radio',
'options'=>array(
'1'=>'显示',
'0'=>'不显示'
),
'value'=>'1'
)
);
+16 -29
View File
@@ -1,35 +1,22 @@
<?php <?php
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ] // | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved. // | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com> // | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
return array( return [
'title'=>array(//配置在表单中的键名 ,这个会是config[title] ['name' => 'title', 'title' => '显示标题', 'type' => 'text', 'value' => '系统信息'],
'title'=>'显示标题:',//表单的文字 ['name' => 'width', 'title' => '显示宽度', 'type' => 'select', 'value' => '6', 'option' => [
'type'=>'text', //表单的类型:text、textarea、checkbox、radio、select等 ['key'=>'1', 'label'=>'1格'],
'value'=>'系统信息', //表单的默认值 ['key'=>'2', 'label'=>'2格'],
), ['key'=>'4', 'label'=>'4格'],
'width'=>array( ['key'=>'6', 'label'=>'6格']
'title'=>'显示宽度:', ]],
'type'=>'select', ['name' => 'display', 'title' => '是否显示', 'type' => 'select', 'value' => '1', 'option' => [
'options'=>array( ['key'=>'1', 'label'=>'显示'],
'3'=>'3格', ['key'=>'0', 'label'=>'不显示']
'4'=>'4格', ]],
'6'=>'6格' ];
),
'value'=>'6'
),
'display'=>array(
'title'=>'是否显示:',
'type'=>'radio',
'options'=>array(
'1'=>'显示',
'0'=>'不显示'
),
'value'=>'1'
)
);
+20 -4
View File
@@ -15,6 +15,7 @@ use think\facade\Cache;
use think\Validate; use think\Validate;
use app\model\Config; use app\model\Config;
use think\facade\Route; use think\facade\Route;
use think\facade\Event;
use app\model\Hooks; use app\model\Hooks;
class Base { class Base {
@@ -72,11 +73,26 @@ class Base {
$config = Config::getConfigList($this->request); $config = Config::getConfigList($this->request);
Cache::set('system_config_data', $config); Cache::set('system_config_data', $config);
} }
$hooks = Hooks::where('status', 1)->column('addons', 'name'); $hooks = Cache::get('sentcms_hooks');
foreach($hooks as $key => $value){ if (!$hooks) {
$hooks[$key] = $value ? explode(",", $value) : []; $hooks = Hooks::where('status', 1)->column('addons', 'name');
foreach($hooks as $key => $values){
if (is_string($values)) {
$values = explode(',', $values);
} else {
$values = (array) $values;
}
$hooks[$key] = array_filter(array_map(function ($v) use ($key) {
return [get_addons_class($v), $key];
}, $values));
// $hooks[$key] = $value ? explode(",", $value) : [];
}
} }
Cache::set('sentcms_hooks', $hooks); if (!empty($hooks)) {
Cache::set('sentcms_hooks', $hooks);
Event::listenEvents($hooks);
}
View::assign('config', $config); View::assign('config', $config);
// 控制器初始化 // 控制器初始化
$this->initialize(); $this->initialize();
+36 -32
View File
@@ -10,6 +10,8 @@ namespace app\controller\admin;
use app\model\Addons as AddonsM; use app\model\Addons as AddonsM;
use app\model\Hooks; use app\model\Hooks;
use think\facade\Cache;
use think\facade\Config;
/** /**
* @title 插件管理 * @title 插件管理
@@ -50,26 +52,22 @@ class Addons extends Base {
* @title 安装插件 * @title 安装插件
*/ */
public function install() { public function install() {
$addon_name = input('addon_name', '', 'trim,ucfirst'); $addon_name = input('addon_name', '', 'trim');
$class = get_addon_class($addon_name); $class = get_addons_class($addon_name);
if (class_exists($class)) { if (class_exists($class)) {
$addons = new $class; $addons = get_addons_instance($addon_name);
$info = $addons->info; $info = $addons->getInfo();
if (!$info || !$addons->checkInfo()) {
//检测信息的正确性
return $this->error('插件信息缺失');
}
session('addons_install_error', null); session('addons_install_error', null);
$install_flag = $addons->install(); $install_flag = $addons->install();
if (!$install_flag) { if (!$install_flag) {
return $this->error('执行插件预安装操作失败' . session('addons_install_error')); return $this->error('执行插件预安装操作失败' . session('addons_install_error'));
} }
$result = $this->addons->install($info); $result = AddonsM::install($info);
if ($result) { if ($result) {
cache('hooks', null); Cache::delete("sentcms_hooks");
return $this->success('安装成功'); return $this->success('安装成功');
} else { } else {
return $this->error($this->addons->getError()); return $this->error("安装失败!");
} }
} else { } else {
return $this->error('插件不存在'); return $this->error('插件不存在');
@@ -80,8 +78,9 @@ class Addons extends Base {
* @title 卸载插件 * @title 卸载插件
*/ */
public function uninstall($id) { public function uninstall($id) {
$result = $this->addons->uninstall($id); $result = AddonsM::uninstall($id);
if ($result === false) { if ($result === false) {
Cache::delete("sentcms_hooks");
return $this->error($this->addons->getError(), ''); return $this->error($this->addons->getError(), '');
} else { } else {
return $this->success('卸载成功!'); return $this->success('卸载成功!');
@@ -91,12 +90,10 @@ class Addons extends Base {
/** /**
* @title 启用插件 * @title 启用插件
*/ */
public function enable() { public function enable($id) {
$id = input('id'); $result = AddonsM::update(['status' => 1], ['id' => $id]);
cache('hooks', null);
$model = model('Addons');
$result = $model::where(array('id' => $id))->update(array('status' => 1));
if ($result) { if ($result) {
Cache::delete('sentcms_hooks');
return $this->success('启用成功'); return $this->success('启用成功');
} else { } else {
return $this->error("启用失败!"); return $this->error("启用失败!");
@@ -106,12 +103,10 @@ class Addons extends Base {
/** /**
* @title 禁用插件 * @title 禁用插件
*/ */
public function disable() { public function disable($id) {
$id = input('id'); $result = AddonsM::update(['status' => 0], ['id' => $id]);
cache('hooks', null);
$model = model('Addons');
$result = $model::where(array('id' => $id))->update(array('status' => 0));
if ($result) { if ($result) {
Cache::delete('sentcms_hooks');
return $this->success('禁用成功'); return $this->success('禁用成功');
} else { } else {
return $this->error("禁用失败!"); return $this->error("禁用失败!");
@@ -123,23 +118,30 @@ class Addons extends Base {
*/ */
public function config() { public function config() {
if ($this->request->isPost()) { if ($this->request->isPost()) {
# code... $config = $this->request->post();
$id = $this->request->param('id');
$result = AddonsM::update(['config' => $config], ['id' => $id]);
if ($result) {
return $this->success('完成设置!');
} else {
return $this->error("无法完成设置!");
}
} else { } else {
$id = input('id', '', 'trim,intval'); $id = $this->request->param('id');
if (!$id) { if (!$id) {
return $this->error("非法操作!"); return $this->error("非法操作!");
} }
$info = $this->addons->find($id); $info = AddonsM::find($id);
if (!empty($info)) { if (!empty($info)) {
$class = get_addon_class($info['name']); $class = get_addons_instance($info['name']);
$keyList = array(); $keyList = $class->getConfig(true);
$data = array( $this->data = array(
'keyList' => $keyList, 'keyList' => $keyList,
'meta_title' => $info['title'] . " - 设置"
); );
$this->assign($data); return $this->fetch('admin/public/edit');
$this->setMeta($info['title'] . " - 设置");
return $this->fetch('public/edit');
} else { } else {
return $this->error("未安装此插件!"); return $this->error("未安装此插件!");
} }
@@ -222,7 +224,9 @@ class Addons extends Base {
*/ */
public function edithook($id) { public function edithook($id) {
if ($this->request->isPost()) { if ($this->request->isPost()) {
$result = $hooks->change(); $data = $this->request->post();
$result = Hooks::update($data, ['id' => $data['id']]);
if ($result !== false) { if ($result !== false) {
return $this->success("修改成功"); return $this->success("修改成功");
} else { } else {
+1 -1
View File
@@ -34,7 +34,7 @@ class Factory {
} }
protected function parseValue(){ protected function parseValue(){
$this->field['value'] = isset($this->data[$this->field['name']]) ? $this->data[$this->field['name']] : ''; $this->field['value'] = isset($this->data[$this->field['name']]) ? $this->data[$this->field['name']] : (isset($this->field['value']) ? $this->field['value'] : '');
} }
public function show(){ public function show(){
+14 -14
View File
@@ -18,7 +18,8 @@ class Addons extends \think\Model {
protected $insert = ['create_time']; protected $insert = ['create_time'];
protected $type = [ protected $type = [
'hooks' => 'json' 'hooks' => 'json',
'config' => 'json'
]; ];
protected function setStatusAttr($value) { protected function setStatusAttr($value) {
@@ -90,16 +91,15 @@ class Addons extends \think\Model {
return $admin; return $admin;
} }
public function install($data) { public static function install($data) {
if ($data) { if ($data) {
$info = $this->where('name', $data['name'])->value('id'); $id = self::where('name', strtolower($data['name']))->value('id');
if ($info) { $result = false;
$result = $this->save(array('isinstall'=>1, 'status'=>1), array('id'=>$info)); if ($id) {
}else{ $result = self::update(['isinstall'=>1, 'status'=>1], ['id'=>$id]);
$result = false;
} }
if (false !== $result) { if (false !== $result) {
return model('Hooks')->addHooks($data['name']); return Hooks::addHooks(strtolower($data['name']));
}else{ }else{
return false; return false;
} }
@@ -108,16 +108,16 @@ class Addons extends \think\Model {
} }
} }
public function uninstall($id) { public static function uninstall($id) {
$info = $this->get($id); $info = self::find($id);
if (!$info) { if (!$info) {
$this->error = "无此插件!"; $this->error = "无此插件!";
return false; return false;
} }
$class = get_addon_class($info['name']); $class = get_addons_class($info['name']);
if (class_exists($class)) { if (class_exists($class)) {
//插件卸载方法 //插件卸载方法
$addons = new $class; $addons = get_addons_instance($info['name']);
if (!method_exists($addons, 'uninstall')) { if (!method_exists($addons, 'uninstall')) {
$this->error = "插件卸载方法!"; $this->error = "插件卸载方法!";
return false; return false;
@@ -125,9 +125,9 @@ class Addons extends \think\Model {
$result = $addons->uninstall(); $result = $addons->uninstall();
if ($result) { if ($result) {
//卸载挂载点中的插件 //卸载挂载点中的插件
$result = model('Hooks')->removeHooks($info['name']); $result = Hooks::removeHooks($info['name']);
//删除插件表中数据 //删除插件表中数据
$this->where(array('id' => $id))->delete(); $info->save(['isinstall' => 0]);
return true; return true;
} else { } else {
$this->error = "无法卸载插件!"; $this->error = "无法卸载插件!";
+47
View File
@@ -19,6 +19,12 @@ class Hooks extends \think\Model {
return isset($type[$data['type']]) ? $type[$data['type']] : ''; return isset($type[$data['type']]) ? $type[$data['type']] : '';
} }
protected function setAddonsAttr($value){
if (!empty($value[0])) {
return implode(",", $value[0]);
}
}
public static function getaddons($addons){ public static function getaddons($addons){
if (isset($addons['addons']) && $addons['addons']) { if (isset($addons['addons']) && $addons['addons']) {
$hook_list = explode(',', $addons['addons']); $hook_list = explode(',', $addons['addons']);
@@ -37,4 +43,45 @@ class Hooks extends \think\Model {
]; ];
return $keylist; return $keylist;
} }
public static function addHooks($name){
// 读取插件目录及钩子列表
$base = get_class_methods("\\sent\\Addons");
// 读取出所有公共方法
$methods = (array)get_class_methods("\\addons\\" . $name . "\\Plugin");
// 跟插件基类方法做比对,得到差异结果
$hooks = array_diff($methods, $base);
$row = self::where('name', 'IN', $hooks)->column("*", "name");
$save = [];
foreach ($hooks as $value) {
if (isset($row[$value]) && !empty($row[$value])) {
$info = $row[$value];
$addons = $info['addons'] ? explode(",", $info['addons']) : [];
empty($addons) ? [$name] : array_push($addons, $name);
$info['addons'] = implode(",", $addons);
$save[] = $info;
}else{
$save[] = ['name' => $value, 'type' => 1, 'create_time' => time(), 'status' => 1, 'addons'=> $name];
}
}
return (new self())->saveAll($save);
}
public static function removeHooks($name){
$row = self::where('addons', 'LIKE', '%'.$name.'%')->select()->toArray();
$save = [];
foreach ($row as $value) {
$addons = explode(",", $value['addons']);
if (in_array($name, $addons)) {
array_splice($addons, array_search($name, $addons), 1);
}
$value['addons'] = !empty($addons) ? implode(",", $addons) : "";
$save[] = $value;
}
if (!empty($save)) {
return (new self())->saveAll($save);
}else{
return true;
}
}
} }
+1 -1
View File
@@ -10,7 +10,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
return [ return [
'autoload' => true, 'autoload' => false,
'hooks' => [], 'hooks' => [],
'route' => [], 'route' => [],
'service' => [], 'service' => [],
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -35,7 +35,7 @@
{if isset($vo['format'])} {if isset($vo['format'])}
<td>{$item[$vo['field']]|$vo['format']}</td> <td>{$item[$vo['field']]|$vo['format']}</td>
{else/} {else/}
<td>{$item[$vo['field']]}</td> <td>{$item[$vo['field']]|default=''}</td>
{/if} {/if}
{/volist} {/volist}
<td> <td>