PluginModel.php
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <bronet@126.com>
// +----------------------------------------------------------------------
namespace app\admin\model;
use think\Model;
use think\Db;
class PluginModel extends Model
{
/**
* 获取插件列表
*/
public function getList()
{
$dirs = array_map('basename', glob(PLUGINS_PATH . '*', GLOB_ONLYDIR));
if ($dirs === false) {
$this->error = '插件目录不可读';
return false;
}
$plugins = [];
if (empty($dirs)) return $plugins;
$list = $this->select();
foreach ($list as $plugin) {
$plugins[$plugin['name']] = $plugin;
}
foreach ($dirs as $pluginDir) {
$pluginDir = cmf_parse_name($pluginDir, 1);
if (!isset($plugins[$pluginDir])) {
$class = cmf_get_plugin_class($pluginDir);
if (!class_exists($class)) { // 实例化插件失败忽略
//TODO 加入到日志中
continue;
}
$obj = new $class;
$plugins[$pluginDir] = $obj->info;
if (!isset($obj->info['type']) || $obj->info['type'] == 1) {//只获取普通插件
if ($plugins[$pluginDir]) {
$plugins[$pluginDir]['status'] = 3;//未安装
}
} else {
unset($plugins[$pluginDir]);
}
}
}
return $plugins;
}
/**
* @TODO
* 获取所有钩子,包括系统,应用,模板
* @param bool $refresh 是否刷新缓存
* @return array
*/
public function getHooks($refresh = false)
{
if (!$refresh) {
// TODO 加入缓存
}
$returnHooks = [];
$systemHooks = [
//系统钩子
"app_init", "app_begin", "module_init", "action_begin", "view_filter",
"app_end", "log_write", "response_end",
"admin_init",
"home_init",
"send_mobile_verification_code",
//系统钩子结束
//前台登录钩子
"user_login_start",
//模板钩子
"body_start", "before_head_end", "before_footer", "footer_start", "before_footer_end", "before_body_end",
"left_sidebar_start",
"before_left_sidebar_end",
"right_sidebar_start",
"before_right_sidebar_end",
"comment",
"guestbook",
];
$dbHooks = Db::name('hook')->column('hook');
$returnHooks = array_unique(array_merge($systemHooks, $dbHooks));
return $returnHooks;
}
public function uninstall($id)
{
$findPlugin = $this->find($id);
if (empty($findPlugin)) {
return -1; //插件不存在;
}
$class = cmf_get_plugin_class($findPlugin['name']);
Db::startTrans();
try {
$this->where(['name' => $findPlugin['name']])->delete();
Db::name('hook_plugin')->where('plugin', $findPlugin['name'])->delete();
if (class_exists($class)) {
$plugin = new $class;
$uninstallSuccess = $plugin->uninstall();
if (!$uninstallSuccess) {
Db::rollback();
return -2;
}
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
return false;
}
return true;
}
}