Skip to content

Commit

Permalink
'动态模块配置'
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Feb 17, 2017
1 parent 9878431 commit 6cf6d98
Show file tree
Hide file tree
Showing 32 changed files with 977 additions and 230 deletions.
3 changes: 2 additions & 1 deletion Yii.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function checkInstalled()
/**
* Class BaseApplication
* Used for properties that are identical for both WebApplication and ConsoleApplication
* @property \config\components\Config $config
* @property \common\modules\config\components\Config $config
* @property \common\components\Storage $storage
* @property \common\components\notify\Handler $notify
*/
Expand All @@ -53,6 +53,7 @@ abstract class BaseApplication extends yii\base\Application
* Include only Web application related components here
* @property \frontend\components\Search $search
* @property \common\components\PluginManager $pluginManager
* @property \common\components\ModuleManager $moduleManager
* @property \common\components\ThemeManager $themeManager
*/
class WebApplication extends yii\web\Application
Expand Down
124 changes: 124 additions & 0 deletions backend/controllers/ArticleModuleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace backend\controllers;

use Yii;
use common\models\ArticleModule;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* ArticleModuleController implements the CRUD actions for ArticleModule model.
*/
class ArticleModuleController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}

/**
* Lists all ArticleModule models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => ArticleModule::find(),
]);

return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}

/**
* Displays a single ArticleModule model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Creates a new ArticleModule model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new ArticleModule();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

/**
* Updates an existing ArticleModule model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}

/**
* Deletes an existing ArticleModule model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Finds the ArticleModule model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return ArticleModule the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = ArticleModule::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
118 changes: 112 additions & 6 deletions backend/controllers/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@

namespace backend\controllers;

use backend\models\ModuleConfig;
use backend\widgets\grid\SwitcherAction;
use common\models\Module;
use Yii;
use yii\data\ActiveDataProvider;
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 ModuleController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'open' => ['post'],
'close' => ['post'],
'install' => ['post'],
'uninstall' => ['post']
],
],
];
}

public function actions()
{
return [
Expand All @@ -27,15 +44,104 @@ public function actions()
*/
public function actionIndex()
{
$query = Module::find()->where(['type' => Module::TYPE_CORE]);
$dataProvider = new ActiveDataProvider(['query' => $query]);
$modules = Yii::$app->moduleManager->findAll();
$dataProvider = new ArrayDataProvider([
'allModels' => $modules
]);

return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
// 安装
public function actionInstall()
{
$id = Yii::$app->request->post('id');
$module = Yii::$app->moduleManager->findOne($id);
if(!Yii::$app->moduleManager->install($module)){
Yii::$app->session->setFlash('error', '安装失败');
} else {
Yii::$app->session->setFlash('success', '安装成功');
}
return $this->redirect(['index']);
}
//卸载
public function actionUninstall()
{
$id = Yii::$app->request->post('id');
$module = Yii::$app->moduleManager->findOne($id);
if(!Yii::$app->moduleManager->uninstall($module)){
Yii::$app->session->setFlash('error', '卸载失败');
} else {
Yii::$app->session->setFlash('success', '卸载成功');
}
return $this->redirect(['index']);
}

public function findModel($id)
// 开启
public function actionOpen()
{
$id = Yii::$app->request->post('id');
$module = Yii::$app->moduleManager->findOne($id);
if(!$module->install){
Yii::$app->session->setFlash('error', '没安装');
}
if(!Yii::$app->moduleManager->open($module)){
Yii::$app->session->setFlash('error', '打开失败');
} else {
Yii::$app->session->setFlash('success', '打开成功');
}
return $this->redirect(['index']);
}
// 关闭
public function actionClose()
{
return Module::findOne($id);
$id = Yii::$app->request->post('id');
$module = Yii::$app->moduleManager->findOne($id);
if(!$module->install){
Yii::$app->session->setFlash('error', '没安装');
}
if(!Yii::$app->moduleManager->close($module)){
Yii::$app->session->setFlash('error', '关闭失败');
} else {
Yii::$app->session->setFlash('success', '关闭成功');
}
return $this->redirect(['index']);
}

// 配置
public function actionConfig($id)
{
$module = Yii::$app->moduleManager->findOne($id);
if(!$module->install){
Yii::$app->session->setFlash('error', '插件没安装');
return $this->redirect(['index']);
}
$configs = $module->getConfig();
$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 = $module->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
]);
}
}
28 changes: 13 additions & 15 deletions backend/controllers/PluginsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace backend\controllers;

use backend\models\ModuleConfig;
use backend\models\PluginsConfig;
use common\models\Module;
use Yii;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function behaviors()
*/
public function actionIndex()
{
$plugins = Yii::$app->get("pluginManager")->findAll();
$plugins = Yii::$app->pluginManager->findAll();
$dataProvider = new ArrayDataProvider([
'allModels' => $plugins
]);
Expand All @@ -50,8 +51,7 @@ public function actionIndex()
public function actionInstall()
{
$id = Yii::$app->request->post('id');
/* @var $pluginManager \common\components\PluginManager */
$pluginManager = Yii::$app->get('pluginManager');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$pluginManager->install($plugin)){
Yii::$app->session->setFlash('error', '插件安装失败');
Expand All @@ -64,8 +64,7 @@ public function actionInstall()
public function actionUninstall()
{
$id = Yii::$app->request->post('id');
/* @var $pluginManager \common\components\PluginManager */
$pluginManager = Yii::$app->get('pluginManager');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$pluginManager->uninstall($plugin)){
Yii::$app->session->setFlash('error', '插件卸载失败');
Expand All @@ -78,8 +77,7 @@ public function actionUninstall()
public function actionOpen()
{
$id = Yii::$app->request->post('id');
/* @var $pluginManager \common\components\PluginManager */
$pluginManager = Yii::$app->get('pluginManager');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$plugin->install){
Yii::$app->session->setFlash('error', '插件没安装');
Expand All @@ -95,8 +93,7 @@ public function actionOpen()
public function actionClose()
{
$id = Yii::$app->request->post('id');
/* @var $pluginManager \common\components\PluginManager */
$pluginManager = Yii::$app->get('pluginManager');
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$plugin->install){
Yii::$app->session->setFlash('error', '插件没安装');
Expand All @@ -111,22 +108,23 @@ public function actionClose()

/**
* 插件配置
* @param $name
* @param $id
* @return string|\yii\web\Response
* @throws \yii\base\InvalidConfigException
*/
public function actionConfig($id)
{
$model = Module::find()->where(['id' => $id])->one();
if (empty($model) || $model->status == Module::STATUS_UNINSTALL) {
$pluginManager = Yii::$app->pluginManager;
$plugin = $pluginManager->findOne($id);
if(!$plugin->install){
Yii::$app->session->setFlash('error', '插件没安装');
return $this->redirect(['index']);
}
$configs = Json::decode($model->config);
$configs = $plugin->getConfig();
$configModels = [];
if (!empty($configs)) {
foreach ($configs as $k => $config) {
$configModel = new PluginsConfig();
$configModel = new ModuleConfig();
$configModel->scenario = 'init';
$configModel->attributes = $config;
$configModels[$k] = $configModel;
Expand All @@ -136,11 +134,11 @@ public function actionConfig($id)
'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();
Yii::$app->cache->delete('pluginConfig-' . $model->name);
return $this->redirect(['index']);
}

Expand Down
2 changes: 1 addition & 1 deletion backend/models/PluginsConfig.php → backend/models/ModuleConfig.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use yii\base\Model;

class PluginsConfig extends Model
class ModuleConfig extends Model
{
public $name;
public $value;
Expand Down
Loading

0 comments on commit 6cf6d98

Please sign in to comment.