/
PluginModel.class.php
executable file
·191 lines (181 loc) · 4.63 KB
/
PluginModel.class.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
class pluginModel{
var $in;
var $config;
function __construct(){
global $config, $in;
//parent::__construct();
$this -> in = &$in;
$this -> config = &$config;
}
public function loadData(){
if(!isset($this->config['settingSystem']['pluginList'])){
$this->config['settingSystem']['pluginList'] = array();
$this->initDefaultPlugin();//首次,加载并开启默认插件
}
return $this->config['settingSystem']['pluginList'];
}
public function saveData(){
$settingFile = USER_SYSTEM.'system_setting.php';
FileCache::save($settingFile,$this->config['settingSystem']);
}
private function initDefaultPlugin(){
$this->pluginScan();
$list = $this->loadData();
foreach ($list as $app => $val) {
$this->changeStatus($app,1);
}
}
/**
* 加载所有插件hook;
*/
public function init(){
$pluginList = $this->loadData();
foreach ($pluginList as $key=>$item) {
if(!is_array($item) && isset($item['id'])){
continue;
}
$file = PLUGIN_DIR.$item['id'].'/app.php';
if( !$item['status'] || !is_file($file)) {
continue;
}
if(!$this->checkAuth($item['id'])){
continue;
}
foreach ($item['regiest'] as $tag => $action) {
Hook::bind($tag,$action);
}
}
//执行全局插件绑定
Hook::trigger("globalRequest");
Hook::trigger(ST.'.'.ACT);
}
public function checkAuth($app){
$pluginList = $this->loadData();
if( !isset($pluginList[$app]) ||
!$pluginList[$app]['status']){
show_tips("Not exist or disabled!");
}
if( !isset($pluginList[$app]['config']['pluginAuth']) ){
return true;
}
$auth = $pluginList[$app]['config']['pluginAuth'];
if(plugin_check_auth($app,$auth)){
return true;
}else{
return false;
}
}
public function add($app){
if( !file_exists(PLUGIN_DIR.$app.'/package.json') ||
!file_exists(PLUGIN_DIR.$app.'/app.php')){
return;
}
Hook::apply($app.'Plugin.regiest');
$this->saveData();
}
public function remove($app){
$pluginList = &$this->config['settingSystem']['pluginList'];
unset($pluginList[$app]);
if( file_exists(PLUGIN_DIR.$app)){
Hook::apply($app.'Plugin.unInstall');
}
$this->saveData();
return true;
}
/**
* 切换插件启用关闭状态
* @param [type] $app 插件名
* @param [type] $open 开关状态 0-禁用;1-启用
* @return
*/
public function changeStatus($app,$open){
$pluginList = &$this->config['settingSystem']['pluginList'];
if(is_array($pluginList[$app])){
if($open){
$config = $this->getConfig($app);
$default = $this->getConfigDefault($app);
$config = array_merge($default,$config);//保存初始配置;兼容新增默认配置
Hook::apply($app.'Plugin.regiest');
$this->setConfig($app,$config);
}
$pluginList[$app]['status'] = $open;
}
$this->saveData();
}
public function getConfigDefault($app){
$result = array();
$json = $this->getPackageJson($app);
if(!$json && is_array($json['configItem'])){
return $result;
}
foreach($json['configItem'] as $key=>$item) {
if(!isset($item['value']) ||
isset($result[$key]) ){
continue;
}
$result[$key] = $item['value'];
}
return $result;
}
public function getPackageJson($app){
return Hook::apply($app.'Plugin.appPackage');
}
public function getConfig($app){
$result = array();
$pluginList = &$this->config['settingSystem']['pluginList'];
if( isset($pluginList[$app]) &&
is_array($pluginList[$app]['config']) ){
$result = $pluginList[$app]['config'];
}
if(!$result){
$result = $this->getConfigDefault($app);
}
return $result;
}
public function setConfig($app,$value){
$pluginList = &$this->config['settingSystem']['pluginList'];
if(isset($pluginList[$app])){
foreach ($value as $key => $val) {
$pluginList[$app]['config'][$key] = $val;
}
}
$this->saveData();
}
/**
* 遍历查检目录;自动加载插件;
* @return [type] [description]
*/
public function pluginScan(){
$pluginList = &$this->config['settingSystem']['pluginList'];
recursion_dir(PLUGIN_DIR,$dirs,$files,0);
foreach ($dirs as $path) {
$app = get_path_this($path);
if(isset($pluginList[$app])){
continue;
}
if( !file_exists($path.'/package.json') ||
!file_exists($path.'/app.php')){
continue;
}
Hook::apply($app.'Plugin.regiest');
}
$this->saveData();
}
public function viewList(){
$this->pluginScan();
$list = $this->loadData();
$result = array();
foreach ($list as $key => $item) {
if(!plugin_check_allow($key)){
continue;
}
unset($item['regiest']);
$package = Hook::apply($item['id'].'Plugin.appPackage');
if(is_array($package)){
$result[$key] = array_merge($item,$package);
}
}
return $result;
}
}