From 61ecd0629f157691e0bb63b91fd9f17efd6aebf4 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 8 Feb 2021 11:12:36 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/system/controller/Module.php | 2 +- extend/catcher/CatchAdmin.php | 173 ++++++------------ extend/catcher/library/InstallLocalModule.php | 5 +- 3 files changed, 56 insertions(+), 124 deletions(-) diff --git a/catch/system/controller/Module.php b/catch/system/controller/Module.php index 3644b8d..d518499 100644 --- a/catch/system/controller/Module.php +++ b/catch/system/controller/Module.php @@ -22,7 +22,7 @@ class Module extends CatchController $modules = []; foreach(CatchAdmin::getModulesDirectory() as $d) { - $modules[] = json_decode(file_get_contents($d . 'module.json'), true); + $modules[] = CatchAdmin::getModuleInfo($d); } $hasModules = array_unique(Permissions::whereIn('id', request()->user()->getPermissionsBy())->column('module')); diff --git a/extend/catcher/CatchAdmin.php b/extend/catcher/CatchAdmin.php index 5e07f85..a6ba36b 100644 --- a/extend/catcher/CatchAdmin.php +++ b/extend/catcher/CatchAdmin.php @@ -3,13 +3,13 @@ declare(strict_types=1); namespace catcher; -use think\helper\Arr; +use catcher\facade\FileSystem; class CatchAdmin { public static $root = 'catch'; - public const VERSION = '2.1.0'; + public const VERSION = '2.5.0'; /** @@ -192,7 +192,7 @@ class CatchAdmin * @time 2020年06月23日 * @return array */ - public static function getEnabledService() + public static function getEnabledService(): array { $services = []; @@ -211,94 +211,68 @@ class CatchAdmin } /** - * 开启模块 + * 获取模块 Json * - * @time 2020年06月23日 + * @time 2021年02月08日 * @param $module - * @return bool + * @return string */ - public static function enableModule($module) + public static function getModuleJson($module): string { - $moduleJson = self::moduleDirectory($module) . 'module.json'; - - if (!file_exists($moduleJson)) { - return true; + if (is_dir($module)) { + return $module . DIRECTORY_SEPARATOR . 'module.json'; } - $info = \json_decode(file_get_contents($moduleJson), true); - - $info['enable'] = true; - - if (!is_writeable($moduleJson)) { - chmod($moduleJson, 666); - } - - file_put_contents($moduleJson, \json_encode($info, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); - - return true; - } - - /** - * 关闭模块 - * - * @time 2020年06月23日 - * @param $module - * @return bool - */ - public static function disableModule($module) - { - $moduleJson = self::moduleDirectory($module) . 'module.json'; - - if (!file_exists($moduleJson)) { - return true; - } - - $info = \json_decode(file_get_contents($moduleJson), true); - - $info['enable'] = false; - - if (!is_writeable($moduleJson)) { - chmod($moduleJson, 666); - } - - file_put_contents($moduleJson, \json_encode($info, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)); - - return true; - } - - /** - * - * @time 2019年11月30日 - * @return array - */ - protected static function getModuleViews(): array - { - $views = []; - - foreach (self::getModulesDirectory() as $module) { - if (is_dir($module . 'view')) { - $moduleInfo = self::getModuleInfo($module); - $moduleName = $moduleInfo['alias'] ?? Arr::last(explode('/', $module)); - $views[$moduleName] = $module . 'view' . DIRECTORY_SEPARATOR; - } - } - - return $views; + return self::moduleDirectory($module) . 'module.json'; } /** * 获取模块信息 * - * @time 2019年11月30日 + * @time 2021年02月08日 * @param $module - * @return mixed + * @return array */ - public static function getModuleInfo($module) + public static function getModuleInfo($module): array { - if (file_exists($module . DIRECTORY_SEPARATOR . 'module.json')) { - return \json_decode(file_get_contents($module. DIRECTORY_SEPARATOR . 'module.json'), true); + $moduleJson = self::getModuleJson($module); + + if (!file_exists($moduleJson)) { + return []; } - return []; + + return \json_decode(FileSystem::sharedGet($moduleJson), true); + } + + /** + * 更新模块信息 + * + * @time 2021年02月08日 + * @param $module + * @param $info + * @return bool + */ + public static function updateModuleInfo($module, $info): bool + { + $moduleInfo = self::getModuleInfo($module); + + if (!count($moduleInfo)) { + return false; + } + + foreach ($moduleInfo as $k => $v) { + if (isset($info[$k])) { + $moduleInfo[$k] = $info[$k]; + } + } + + if (! is_writeable(self::getModuleJson($module))) { + chmod(self::getModuleJson($module), 666); + } + + FileSystem::put(self::getModuleJson($module), \json_encode($moduleInfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)); + + return true; } /** @@ -321,7 +295,7 @@ class CatchAdmin * @time 2019年11月30日 * @return mixed */ - public static function getRoutes() + public static function getRoutes(): array { if (file_exists(self::getCacheRoutesFile())) { return [self::getCacheRoutesFile()]; @@ -330,19 +304,6 @@ class CatchAdmin return self::getModuleRoutes(); } - /** - * - * @time 2019年11月30日 - * @return array|mixed - */ - public static function getViews() - { - if (file_exists(self::getCacheViewsFile())) { - return self::getCacheViews(); - } - - return self::getModuleViews(); - } /** * @@ -390,27 +351,6 @@ class CatchAdmin . var_export(self::getEnabledService(), true) . ';'); } - /** - * - * @time 2019年11月30日 - * @return false|int - */ - public static function cacheViews() - { - return file_put_contents(self::getCacheViewsFile(), "module); + CatchAdmin::updateModuleInfo($this->module, ['enable' => true]); + app(Permissions::class)->restore(['module' => trim($this->module)]); } @@ -137,7 +138,7 @@ class InstallLocalModule */ public function disableModule() { - CatchAdmin::disableModule($this->module); + CatchAdmin::updateModuleInfo($this->module, ['enable' => false]); Permissions::destroy(function ($query) { $query->where('module', trim($this->module));