Skip to content

Commit

Permalink
重构后台扩展安装方法
Browse files Browse the repository at this point in the history
  • Loading branch information
735579768 committed Dec 15, 2016
1 parent 17ee209 commit 9a92e58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
48 changes: 15 additions & 33 deletions apps/admin/controller/sys/Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function lis() {
// 'order' => 'sort asc',
]);
$this->assign([
'meta_title' => '已装插件',
'meta_title' => '已装扩展',
// '_list' => [],
// '_page' => '',
]);
Expand All @@ -23,13 +23,13 @@ public function install() {
$name = input('param.name', '');
$info = \think\Db::name('Addon')->where(['name' => strtolower($name)])->find();
if ($info) {
$this->error('插件已经安装');
$this->error('扩展已经安装');
}
$obj = $this->getAddon($name);
$obj = \app\common\controller\Addon::getAddonInstance("/addons/{$name}/{$name}Admin");
$result = $obj->install();
if ($result === true) {
$config = $obj->getConfig();
// //添加一个插件的设置菜单
// //添加一个扩展的设置菜单
$data = [
'pid' => 2,
'title' => $config['title'],
Expand All @@ -42,7 +42,7 @@ public function install() {
$menu_id = \think\Db::name('Menu')->insertGetId($data);
\think\Cache::clear('menu');
// $menu_id = 22;
//保存插件信息到数据库
//保存扩展信息到数据库
$data = [
'name' => $name,
'menu_id' => $menu_id,
Expand All @@ -57,10 +57,11 @@ public function install() {
];
$id = \think\Db::name('Addon')->insertGetId($data);
\think\Cache::clear('mainchildmenu');
add_user_log('安装插件', input('param.'));
\think\Cache::clear('menu');
add_user_log('安装扩展', input('param.'));
$this->success('安装成功');
} else {
$this->error('插件安装失败');
$this->error('扩展安装失败');
}
} else {
$dirlist = get_dir_list(SITE_PATH . '/addons');
Expand All @@ -70,7 +71,7 @@ public function install() {
foreach ($dirlist as $key => $value) {
$info = \think\Db::name('Addon')->field('addon_id')->where(['name' => strtolower($value)])->find();
if (!$info) {
$obj = $this->getAddon($value);
$obj = \app\common\controller\Addon::getAddonInstance("/addons/{$value}/{$value}Admin");
if ($obj) {
$conf = $obj->getConfig();
$conf['name'] = $value;
Expand All @@ -82,56 +83,37 @@ public function install() {
}
// dump($addoninfo);
$this->assign([
'meta_title' => '安装插件',
'meta_title' => '安装扩展',
'_list' => $addonlist,
'_page' => '',
]);
return $this->fetch();
}
}

//卸载插件方法
//卸载扩展方法
public function unInstall() {
$name = input('param.name', '');
$info = \think\Db::name('Addon')->field('addon_id,menu_id')->where(['name' => strtolower($name)])->find();
if ($info) {
$obj = $this->getAddon($name);
$obj = \app\common\controller\Addon::getAddonInstance("/addons/{$name}/{$name}Admin");
if ($obj) {
$result = $obj->unInstall();
if ($result === true) {
\think\Db::name('Addon')->where(['addon_id' => $info['addon_id']])->delete();
\think\Db::name('Menu')->where(['menu_id' => $info['menu_id']])->delete();
\think\Cache::clear('mainchildmenu');
add_user_log('卸载插件', input('param.'));
\think\Cache::clear('menu');
add_user_log('卸载扩展', input('param.'));
$this->success('卸载成功', url('lis'));
} else {
$this->error('卸载失败');
}
} else {
$this->error('卸载失败,插件文件可能已丢失');
$this->error('卸载失败,扩展文件可能已丢失');
}
}

}

/**
* 返回一个插件的实例
* @return [type] [description]
*/
private function getAddon($name = '') {
if (empty($name)) {
return null;
}
$name = strtolower($name);
$addon_path = SITE_PATH . '/addons' . '/' . $name . '/' . ucfirst($name) . 'Admin.php';
if (file_exists($addon_path)) {
include $addon_path;
$name = "\\addons\\{$name}\\" . ucfirst($name) . 'Admin';
return new $name();
} else {
return null;
}

}

}
7 changes: 4 additions & 3 deletions apps/common/controller/Addon.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ public function __construct() {
]);
}
/**
* 取对扩展的一个实例
* @param string $addonname [description]
* 取对应扩展的一个实例
* @param string $addonname 扩展类名,包涵命名空间
* @return [type] [description]
*/
public static function getAddonInstance($addonName = '') {
$addonName = str_replace('/', '\\', $addonName);
if (!isset(self::$addonInstances[$addonName])) {
$addon_path = str_replace('\\', '/', SITE_PATH . $addonName . '.php');
if (file_exists($addon_path)) {
Expand All @@ -65,7 +66,7 @@ public static function errorMsg($msg = '') {
}
}
/**
* 执扩展的方法
* 执行已经安装过的扩展的方法
* @param string $name 扩展名字
* @param array $args 扩展参数
* @return [type] [description]
Expand Down

0 comments on commit 9a92e58

Please sign in to comment.