-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginsController.php
executable file
·150 lines (143 loc) · 4.61 KB
/
PluginsController.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
<?php
namespace backend\controllers;
use backend\models\ModuleConfig;
use backend\models\PluginsConfig;
use common\models\Module;
use Yii;
use yii\base\Model;
use yii\data\ArrayDataProvider;
use yii\filters\VerbFilter;
use yii\helpers\Json;
use yii\web\Controller;
/**
* ModuleController implements the CRUD actions for Module model.
*/
class PluginsController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'open' => ['post'],
'close' => ['post'],
'install' => ['post'],
'uninstall' => ['post']
],
],
];
}
/**
* Lists all Module models.
* @return mixed
*/
public function actionIndex()
{
$plugins = Yii::$app->pluginManager->findAll();
$dataProvider = new ArrayDataProvider([
'allModels' => $plugins
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
// 安装
public function actionInstall()
{
$id = Yii::$app->request->post('id');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$pluginManager->install($plugin)){
Yii::$app->session->setFlash('error', '插件安装失败');
} else {
Yii::$app->session->setFlash('success', '插件安装成功');
}
return $this->redirect(['index']);
}
//卸载
public function actionUninstall()
{
$id = Yii::$app->request->post('id');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$pluginManager->uninstall($plugin)){
Yii::$app->session->setFlash('error', '插件卸载失败');
} else {
Yii::$app->session->setFlash('success', '插件卸载成功');
}
return $this->redirect(['index']);
}
// 开启
public function actionOpen()
{
$id = Yii::$app->request->post('id');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$plugin->install){
Yii::$app->session->setFlash('error', '插件没安装');
}
if(!$pluginManager->open($plugin)){
Yii::$app->session->setFlash('error', '插件打开失败');
} else {
Yii::$app->session->setFlash('success', '插件打开成功');
}
return $this->redirect(['index']);
}
// 关闭
public function actionClose()
{
$id = Yii::$app->request->post('id');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$plugin->install){
Yii::$app->session->setFlash('error', '插件没安装');
}
if(!$pluginManager->close($plugin)){
Yii::$app->session->setFlash('error', '插件关闭失败');
} else {
Yii::$app->session->setFlash('success', '插件关闭成功');
}
return $this->redirect(['index']);
}
/**
* 插件配置
* @param $id
* @return string|\yii\web\Response
* @throws \yii\base\InvalidConfigException
*/
public function actionConfig($id)
{
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$plugin->install){
Yii::$app->session->setFlash('error', '插件没安装');
return $this->redirect(['index']);
}
$configs = Json::decode($plugin->getModel()->config);
$configModels = [];
if (!empty($configs)) {
foreach ($configs as $k => $config) {
$configModel = new ModuleConfig();
$configModel->scenario = 'init';
$configModel->attributes = $config;
$configModels[$k] = $configModel;
}
}
$dataProvider = new ArrayDataProvider([
'models' => $configModels,
'pagination' => false
]);
$model = $plugin->getModel();
if (\Yii::$app->request->isPost && Model::loadMultiple($configModels, \Yii::$app->request->post()) && Model::validateMultiple($configModels)) {
$configs = Json::encode($configModels);
$model->config = $configs;
$model->save();
return $this->redirect(['index']);
}
return $this->render('config', [
'model' => $model,
'dataProvider' => $dataProvider
]);
}
}