更新目录结构
This commit is contained in:
148
app/model/addons/Addons.php
Normal file
148
app/model/addons/Addons.php
Normal file
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model\addons;
|
||||
|
||||
use app\facade\Cache;
|
||||
|
||||
/**
|
||||
* 扩展模型
|
||||
*/
|
||||
class Addons extends \think\Model {
|
||||
|
||||
protected $auto = ['status', 'isinstall', 'update_time'];
|
||||
protected $insert = ['create_time'];
|
||||
|
||||
protected $type = [
|
||||
'hooks' => 'json',
|
||||
'config' => 'json'
|
||||
];
|
||||
|
||||
public static function onAfterWrite($addons){
|
||||
Cache::delete('sentcms_hooks');
|
||||
}
|
||||
|
||||
public static function onAfterDelete($addons){
|
||||
Cache::delete('sentcms_hooks');
|
||||
}
|
||||
|
||||
protected function setStatusAttr($value) {
|
||||
return $value ? $value : 0;
|
||||
}
|
||||
|
||||
protected function setNameAttr($value){
|
||||
return $value ? strtolower($value) : '';
|
||||
}
|
||||
|
||||
protected function setIsinstallAttr($value) {
|
||||
return $value ? $value : 0;
|
||||
}
|
||||
|
||||
protected function getStatusTextAttr($value, $data) {
|
||||
return $data['status'] ? "启用" : "禁用";
|
||||
}
|
||||
|
||||
protected function getUninstallAttr($value, $data) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新插件列表
|
||||
* @param string $addon_dir
|
||||
*/
|
||||
public static function refreshAddons($addon_dir = '') {
|
||||
if (!$addon_dir) {
|
||||
$addon_dir = SENT_ADDON_PATH;
|
||||
}
|
||||
$dirs = array_map('basename', glob($addon_dir . '*', GLOB_ONLYDIR));
|
||||
if ($dirs === FALSE || !file_exists($addon_dir)) {
|
||||
return FALSE;
|
||||
}
|
||||
$where[] = ['name', 'in', $dirs];
|
||||
$addons = self::where($where)->column('*', 'name');
|
||||
|
||||
$save = [];
|
||||
foreach ($dirs as $value) {
|
||||
$value = strtolower($value);
|
||||
$class = "\\addons\\" . $value . "\\Plugin";
|
||||
if (!class_exists($class)) {
|
||||
continue;
|
||||
}
|
||||
$item = get_addons_info($value);
|
||||
if (isset($addons[$value])) {
|
||||
$item['id'] = $addons[$value]['id'];
|
||||
unset($item['status']);
|
||||
}
|
||||
$save[] = $item;
|
||||
}
|
||||
$class = new self();
|
||||
return $class->saveAll($save);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件的后台列表
|
||||
*/
|
||||
public function getAdminList() {
|
||||
$admin = [];
|
||||
$map[] = ['status', '=', 1];
|
||||
$map[] = ['has_adminlist', '=', 1];
|
||||
$db_addons = self::where($map)->field('title,name')->select();
|
||||
if ($db_addons) {
|
||||
foreach ($db_addons as $value) {
|
||||
$admin[] = array('title' => $value['title'], 'url' => "Addons/adminList?name={$value['name']}");
|
||||
}
|
||||
}
|
||||
return $admin;
|
||||
}
|
||||
|
||||
public static function install($data) {
|
||||
if ($data) {
|
||||
$id = self::where('name', strtolower($data['name']))->value('id');
|
||||
$result = false;
|
||||
if ($id) {
|
||||
$result = self::update(['isinstall'=>1, 'status'=>1], ['id'=>$id]);
|
||||
}
|
||||
if (false !== $result) {
|
||||
return Hooks::addHooks(strtolower($data['name']));
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function uninstall($id) {
|
||||
$info = self::find($id);
|
||||
if (!$info) {
|
||||
return false;
|
||||
}
|
||||
$class = get_addons_class($info['name']);
|
||||
if (class_exists($class)) {
|
||||
//插件卸载方法
|
||||
$addons = get_addons_instance($info['name']);
|
||||
if (!method_exists($addons, 'uninstall')) {
|
||||
return false;
|
||||
}
|
||||
$result = $addons->uninstall();
|
||||
if ($result) {
|
||||
//卸载挂载点中的插件
|
||||
$result = Hooks::removeHooks($info['name']);
|
||||
//删除插件表中数据
|
||||
$info->save(['isinstall' => 0]);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function build() {
|
||||
|
||||
}
|
||||
}
|
||||
85
app/model/addons/Hooks.php
Normal file
85
app/model/addons/Hooks.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\model\addons;
|
||||
|
||||
/**
|
||||
* 分类模型
|
||||
*/
|
||||
class Hooks extends \think\Model {
|
||||
|
||||
protected function getTypeTextAttr($value, $data){
|
||||
$type = [1 => '视图', 2 => '控制器'];
|
||||
return isset($type[$data['type']]) ? $type[$data['type']] : '';
|
||||
}
|
||||
|
||||
protected function setAddonsAttr($value){
|
||||
if (is_array($value) && !empty($value)) {
|
||||
return implode(",", $value);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getaddons($addons){
|
||||
if (isset($addons['addons']) && $addons['addons']) {
|
||||
$hook_list = explode(',', $addons['addons']);
|
||||
foreach ($hook_list as $key => $value) {
|
||||
$field_list[] = array('id' => $value, 'title' => $value, 'name' => $value, 'is_show' => 1);
|
||||
}
|
||||
$option[] = ['name' => '钩子挂载排序', 'list' => $field_list];
|
||||
} else {
|
||||
$option[] = ['name' => '钩子挂载排序', 'list' => []];
|
||||
}
|
||||
$keylist = [
|
||||
['name' => 'name', 'title' => '钩子名称', 'type' => 'text', 'help' => '需要在程序中先添加钩子,否则无效'],
|
||||
['name' => 'description', 'title' => '钩子描述', 'type' => 'text', 'help' => '钩子的描述信息'],
|
||||
['name' => 'type_text', 'title' => '钩子类型', 'type' => 'select', 'option' => [['key'=>1, 'label'=>'视图'], ['key' => 2, 'label' => '控制器']], 'help' => '钩子的描述信息'],
|
||||
['name' => 'addons', 'title' => '插件排序', 'type' => 'kanban', 'option' => $option],
|
||||
];
|
||||
return $keylist;
|
||||
}
|
||||
|
||||
public static function addHooks($name){
|
||||
// 读取插件目录及钩子列表
|
||||
$base = get_class_methods("\\sent\\Addons");
|
||||
// 读取出所有公共方法
|
||||
$methods = (array)get_class_methods("\\addons\\" . $name . "\\Plugin");
|
||||
// 跟插件基类方法做比对,得到差异结果
|
||||
$hooks = array_diff($methods, $base);
|
||||
$row = self::where('name', 'IN', $hooks)->column("*", "name");
|
||||
$save = [];
|
||||
foreach ($hooks as $value) {
|
||||
if (isset($row[$value]) && !empty($row[$value])) {
|
||||
$info = $row[$value];
|
||||
$addons = $info['addons'] ? explode(",", $info['addons']) : [];
|
||||
$info['addons'] = empty($addons) ? [$name] : array_push($addons, $name);
|
||||
$save[] = $info;
|
||||
}else{
|
||||
$save[] = ['name' => $value, 'type' => 1, 'create_time' => time(), 'status' => 1, 'addons'=> $name];
|
||||
}
|
||||
}
|
||||
return (new self())->saveAll($save);
|
||||
}
|
||||
|
||||
public static function removeHooks($name){
|
||||
$row = self::where('addons', 'LIKE', '%'.$name.'%')->select()->toArray();
|
||||
$save = [];
|
||||
foreach ($row as $value) {
|
||||
$addons = explode(",", $value['addons']);
|
||||
if (in_array($name, $addons)) {
|
||||
array_splice($addons, array_search($name, $addons), 1);
|
||||
}
|
||||
$value['addons'] = !empty($addons) ? implode(",", $addons) : "";
|
||||
$save[] = $value;
|
||||
}
|
||||
if (!empty($save)) {
|
||||
return (new self())->saveAll($save);
|
||||
}else{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user