update:优化模块信息获取

This commit is contained in:
JaguarJack 2021-02-08 11:12:36 +08:00
parent 84c85c3d12
commit 61ecd0629f
3 changed files with 56 additions and 124 deletions

View File

@ -22,7 +22,7 @@ class Module extends CatchController
$modules = []; $modules = [];
foreach(CatchAdmin::getModulesDirectory() as $d) { 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')); $hasModules = array_unique(Permissions::whereIn('id', request()->user()->getPermissionsBy())->column('module'));

View File

@ -3,13 +3,13 @@ declare(strict_types=1);
namespace catcher; namespace catcher;
use think\helper\Arr; use catcher\facade\FileSystem;
class CatchAdmin class CatchAdmin
{ {
public static $root = 'catch'; 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日 * @time 2020年06月23日
* @return array * @return array
*/ */
public static function getEnabledService() public static function getEnabledService(): array
{ {
$services = []; $services = [];
@ -211,96 +211,70 @@ class CatchAdmin
} }
/** /**
* 开启模块 * 获取模块 Json
* *
* @time 2020年06月23 * @time 2021年02月08
* @param $module * @param $module
* @return bool * @return string
*/ */
public static function enableModule($module) public static function getModuleJson($module): string
{ {
$moduleJson = self::moduleDirectory($module) . 'module.json'; if (is_dir($module)) {
return $module . DIRECTORY_SEPARATOR . 'module.json';
if (!file_exists($moduleJson)) {
return true;
} }
$info = \json_decode(file_get_contents($moduleJson), true); return self::moduleDirectory($module) . 'module.json';
$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;
} }
/** /**
* 获取模块信息 * 获取模块信息
* *
* @time 2019年11月30 * @time 2021年02月08日
* @param $module * @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')) { $moduleJson = self::getModuleJson($module);
return \json_decode(file_get_contents($module. DIRECTORY_SEPARATOR . 'module.json'), true);
} 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日 * @time 2019年11月30日
* @return mixed * @return mixed
*/ */
public static function getRoutes() public static function getRoutes(): array
{ {
if (file_exists(self::getCacheRoutesFile())) { if (file_exists(self::getCacheRoutesFile())) {
return [self::getCacheRoutesFile()]; return [self::getCacheRoutesFile()];
@ -330,19 +304,6 @@ class CatchAdmin
return self::getModuleRoutes(); 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) . ';'); . var_export(self::getEnabledService(), true) . ';');
} }
/**
*
* @time 2019年11月30日
* @return false|int
*/
public static function cacheViews()
{
return file_put_contents(self::getCacheViewsFile(), "<?php\r\n return "
. var_export(self::getModuleViews(), true) . ';');
}
/**
*
* @time 2019年11月30日
* @return mixed
*/
protected static function getCacheViews()
{
return include self::getCacheViewsFile();
}
/** /**
* *
* @time 2019年11月30日 * @time 2019年11月30日
@ -420,22 +360,13 @@ class CatchAdmin
{ {
return include self::getCacheServicesFile(); return include self::getCacheServicesFile();
} }
/**
*
* @time 2019年11月30日
* @return mixed
*/
protected static function getCacheViewsFile()
{
return self::cacheDirectory() . 'views.php';
}
/** /**
* *
* @time 2019年11月30日 * @time 2019年11月30日
* @return mixed * @return mixed
*/ */
public static function getCacheServicesFile() public static function getCacheServicesFile(): string
{ {
return self::cacheDirectory() . 'services.php'; return self::cacheDirectory() . 'services.php';
} }

View File

@ -125,7 +125,8 @@ class InstallLocalModule
*/ */
public function enableModule() public function enableModule()
{ {
CatchAdmin::enableModule($this->module); CatchAdmin::updateModuleInfo($this->module, ['enable' => true]);
app(Permissions::class)->restore(['module' => trim($this->module)]); app(Permissions::class)->restore(['module' => trim($this->module)]);
} }
@ -137,7 +138,7 @@ class InstallLocalModule
*/ */
public function disableModule() public function disableModule()
{ {
CatchAdmin::disableModule($this->module); CatchAdmin::updateModuleInfo($this->module, ['enable' => false]);
Permissions::destroy(function ($query) { Permissions::destroy(function ($query) {
$query->where('module', trim($this->module)); $query->where('module', trim($this->module));