catchAdmin/extend/catcher/CatchAdmin.php

401 lines
9.3 KiB
PHP
Raw Normal View History

2019-12-06 09:17:40 +08:00
<?php
2020-11-29 09:29:14 +08:00
declare(strict_types=1);
2019-12-06 09:17:40 +08:00
namespace catcher;
2021-02-08 11:12:36 +08:00
use catcher\facade\FileSystem;
2019-12-06 09:17:40 +08:00
class CatchAdmin
{
2020-07-11 15:52:30 +08:00
public static $root = 'catch';
2019-12-06 09:17:40 +08:00
2021-02-08 11:12:36 +08:00
public const VERSION = '2.5.0';
2020-07-05 16:36:28 +08:00
2020-07-11 15:52:30 +08:00
2019-12-06 09:17:40 +08:00
/**
*
* @time 2019年11月30日
* @return string
*/
public static function directory(): string
{
2020-07-11 15:52:30 +08:00
return app()->getRootPath() . self::$root . DIRECTORY_SEPARATOR;
2019-12-06 09:17:40 +08:00
}
2021-03-29 19:51:41 +08:00
/**
* 设置 root
*
* @time 2021年03月28日
* @param $root
* @return CatchAdmin
*/
public static function setRoot($root): CatchAdmin
{
self::$root = $root;
return new self();
}
2019-12-06 09:17:40 +08:00
/**
2019-12-17 09:02:49 +08:00
* 创建目录
2019-12-06 09:17:40 +08:00
*
2019-12-17 09:02:49 +08:00
* @time 2019年12月16日
* @param string $directory
2019-12-06 09:17:40 +08:00
* @return string
*/
2019-12-17 09:02:49 +08:00
public static function makeDirectory(string $directory): string
2019-12-06 09:17:40 +08:00
{
if (!is_dir($directory) && !mkdir($directory, 0777, true) && !is_dir($directory)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $directory));
}
return $directory;
}
2019-12-17 09:02:49 +08:00
/**
*
* @time 2019年12月04日
* @param $module
* @return string
*/
public static function moduleDirectory($module): string
{
return self::makeDirectory(self::directory() . $module . DIRECTORY_SEPARATOR);
}
2019-12-06 09:17:40 +08:00
/**
*
* @time 2019年11月30日
* @return string
*/
public static function cacheDirectory(): string
{
2020-07-14 17:35:16 +08:00
return self::makeDirectory(app()->getRuntimePath() . self::$root . DIRECTORY_SEPARATOR);
2019-12-06 09:17:40 +08:00
}
2019-12-13 17:26:54 +08:00
/**
* 备份地址
*
* @time 2019年12月13日
* @return string
*/
public static function backupDirectory(): string
{
2019-12-17 09:02:49 +08:00
return self::makeDirectory(self::cacheDirectory() . 'backup' .DIRECTORY_SEPARATOR);
2019-12-13 17:26:54 +08:00
}
2019-12-06 09:17:40 +08:00
/**
*
* @time 2019年12月03日
* @param $module
* @return string
*/
public static function moduleMigrationsDirectory($module): string
{
return self::directory() . $module . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR. 'migrations' . DIRECTORY_SEPARATOR;
}
/**
*
* @time 2019年12月03日
* @param $module
* @return string
*/
public static function moduleSeedsDirectory($module): string
{
2020-06-25 08:35:52 +08:00
$seedPath = self::directory() . $module . DIRECTORY_SEPARATOR . 'database' . DIRECTORY_SEPARATOR. 'seeds' . DIRECTORY_SEPARATOR;
self::makeDirectory($seedPath);
return $seedPath;
2019-12-06 09:17:40 +08:00
}
/**
* 获取模块 view path
*
* @time 2019年12月03日
* @param $module
* @return string
*/
public static function getModuleViewPath($module): string
{
2019-12-17 09:02:49 +08:00
return self::makeDirectory(self::directory() . $module . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR);
2019-12-06 09:17:40 +08:00
}
/**
*
* @time 2019年12月03日
* @param $module
* @return string
*/
public static function getModuleModelDirectory($module): string
{
2019-12-17 09:02:49 +08:00
return self::makeDirectory(self::directory() . $module . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR);
2019-12-06 09:17:40 +08:00
}
/**
*
* @time 2019年11月30日
* @return array
*/
public static function getModulesDirectory(): array
{
$modules = glob(self::directory() . '*');
foreach ($modules as $key => &$module) {
if (!is_dir($module)) {
unset($modules[$key]);
}
$module .= DIRECTORY_SEPARATOR;
}
return $modules;
}
2019-12-12 18:54:07 +08:00
/**
*
* @time 2019年12月12日
2020-07-14 17:35:16 +08:00
* @param bool $select
2019-12-12 18:54:07 +08:00
* @return array
*/
2019-12-12 22:34:27 +08:00
public static function getModulesInfo($select = true): array
2019-12-12 18:54:07 +08:00
{
$modules = [];
2019-12-12 22:34:27 +08:00
if ($select) {
foreach (self::getModulesDirectory() as $module) {
$moduleInfo = self::getModuleInfo($module);
$modules[] = [
'value' => $moduleInfo['alias'],
'title' => $moduleInfo['name'],
];
}
} else {
foreach (self::getModulesDirectory() as $module) {
$moduleInfo = self::getModuleInfo($module);
$modules[$moduleInfo['alias']] = $moduleInfo['name'];
}
2019-12-12 18:54:07 +08:00
}
return $modules;
}
2019-12-06 09:17:40 +08:00
/**
*
* @time 2019年11月30日
* @return array
*/
protected static function getModuleServices(): array
{
$services = [];
foreach (self::getModulesDirectory() as $module) {
if (is_dir($module)) {
$moduleInfo = self::getModuleInfo($module);
if (isset($moduleInfo['services']) && !empty($moduleInfo['services'])) {
$services = array_merge($services, $moduleInfo['services']);
}
}
}
return $services;
}
2020-06-23 21:37:37 +08:00
/**
* 获取可用模块
*
* @time 2020年06月23日
* @return array
*/
2021-02-08 11:12:36 +08:00
public static function getEnabledService(): array
2020-06-23 21:37:37 +08:00
{
$services = [];
foreach (self::getModulesDirectory() as $module) {
if (is_dir($module)) {
$moduleInfo = self::getModuleInfo($module);
2020-06-24 10:07:11 +08:00
// 如果没有设置 module.json 默认加载
$moduleServices = $moduleInfo['services'] ?? [];
if (!empty($moduleServices) && $moduleInfo['enable']) {
$services = array_merge($services, $moduleServices);
2020-06-23 21:37:37 +08:00
}
}
}
return $services;
}
/**
2021-02-08 11:12:36 +08:00
* 获取模块 Json
2020-06-23 21:37:37 +08:00
*
2021-02-08 11:12:36 +08:00
* @time 2021年02月08日
2020-06-23 21:37:37 +08:00
* @param $module
2021-02-08 11:12:36 +08:00
* @return string
2020-06-23 21:37:37 +08:00
*/
2021-02-08 11:12:36 +08:00
public static function getModuleJson($module): string
2020-06-23 21:37:37 +08:00
{
2021-02-08 11:12:36 +08:00
if (is_dir($module)) {
return $module . DIRECTORY_SEPARATOR . 'module.json';
2020-06-23 21:37:37 +08:00
}
2021-02-08 11:12:36 +08:00
return self::moduleDirectory($module) . 'module.json';
2020-06-23 21:37:37 +08:00
}
/**
2021-02-08 11:12:36 +08:00
* 获取模块信息
2020-06-23 21:37:37 +08:00
*
2021-02-08 11:12:36 +08:00
* @time 2021年02月08日
2020-06-23 21:37:37 +08:00
* @param $module
2021-02-08 11:12:36 +08:00
* @return array
2020-06-23 21:37:37 +08:00
*/
2021-02-08 11:12:36 +08:00
public static function getModuleInfo($module): array
2020-06-23 21:37:37 +08:00
{
2021-02-08 11:12:36 +08:00
$moduleJson = self::getModuleJson($module);
2020-06-23 21:37:37 +08:00
if (!file_exists($moduleJson)) {
2021-02-08 11:12:36 +08:00
return [];
2020-09-23 08:05:31 +08:00
}
2021-02-08 11:12:36 +08:00
return \json_decode(FileSystem::sharedGet($moduleJson), true);
2020-06-23 21:37:37 +08:00
}
2019-12-06 09:17:40 +08:00
/**
2021-02-08 11:12:36 +08:00
* 更新模块信息
2019-12-06 09:17:40 +08:00
*
2021-02-08 11:12:36 +08:00
* @time 2021年02月08日
* @param $module
* @param $info
* @return bool
2019-12-06 09:17:40 +08:00
*/
2021-02-08 11:12:36 +08:00
public static function updateModuleInfo($module, $info): bool
2019-12-06 09:17:40 +08:00
{
2021-02-08 11:12:36 +08:00
$moduleInfo = self::getModuleInfo($module);
2019-12-06 09:17:40 +08:00
2021-02-08 11:12:36 +08:00
if (!count($moduleInfo)) {
return false;
2019-12-06 09:17:40 +08:00
}
2021-02-08 11:12:36 +08:00
foreach ($moduleInfo as $k => $v) {
if (isset($info[$k])) {
$moduleInfo[$k] = $info[$k];
}
}
2019-12-06 09:17:40 +08:00
2021-02-08 11:12:36 +08:00
if (! is_writeable(self::getModuleJson($module))) {
chmod(self::getModuleJson($module), 666);
2019-12-06 09:17:40 +08:00
}
2021-02-08 11:12:36 +08:00
FileSystem::put(self::getModuleJson($module), \json_encode($moduleInfo, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
return true;
2019-12-06 09:17:40 +08:00
}
/**
* 获取服务
*
* @time 2019年11月30日
* @return array
*/
public static function getServices(): array
{
if (file_exists(self::getCacheServicesFile())) {
return self::getCacheServices();
}
return self::getModuleServices();
}
/**
*
* @time 2019年11月30日
2019-12-15 15:51:00 +08:00
* @return mixed
2019-12-06 09:17:40 +08:00
*/
2021-02-08 11:12:36 +08:00
public static function getRoutes(): array
2019-12-06 09:17:40 +08:00
{
2019-12-09 16:22:00 +08:00
if (file_exists(self::getCacheRoutesFile())) {
2019-12-15 17:47:30 +08:00
return [self::getCacheRoutesFile()];
2019-12-06 09:17:40 +08:00
}
2019-12-15 15:51:00 +08:00
return self::getModuleRoutes();
2019-12-06 09:17:40 +08:00
}
/**
*
2019-12-15 15:51:00 +08:00
* @time 2019年12月15日
* @return array
2019-12-06 09:17:40 +08:00
*/
2019-12-15 15:51:00 +08:00
public static function getModuleRoutes(): array
2019-12-06 09:17:40 +08:00
{
$routeFiles = [];
foreach (self::getModulesDirectory() as $module) {
2019-12-14 17:37:00 +08:00
$moduleInfo = self::getModuleInfo($module);
2020-03-11 14:14:27 +08:00
$moduleAlias = $moduleInfo['alias'] ?? '';
if (!in_array($moduleAlias, ['login']) && file_exists($module . 'route.php')) {
2019-12-15 15:51:00 +08:00
$routeFiles[] = $module . 'route.php';
2019-12-06 09:17:40 +08:00
}
}
2019-12-15 15:51:00 +08:00
return $routeFiles;
}
2020-06-23 21:37:37 +08:00
2019-12-15 15:51:00 +08:00
/**
*
* @time 2019年11月30日
* @return false|int
*/
public static function cacheRoutes()
{
2019-12-06 09:17:40 +08:00
$routes = '';
2019-12-15 15:51:00 +08:00
foreach (self::getModuleRoutes() as $route) {
2019-12-06 09:17:40 +08:00
$routes .= trim(str_replace('<?php', '', file_get_contents($route))) . PHP_EOL;
}
return file_put_contents(self::getCacheRoutesFile(), "<?php\r\n " . $routes);
}
/**
*
* @time 2019年11月30日
* @return false|int
*/
public static function cacheServices()
{
return file_put_contents(self::getCacheServicesFile(), "<?php\r\n return "
2020-09-22 11:14:51 +08:00
. var_export(self::getEnabledService(), true) . ';');
2019-12-06 09:17:40 +08:00
}
/**
*
* @time 2019年11月30日
* @return mixed
*/
protected static function getCacheServices()
{
return include self::getCacheServicesFile();
}
/**
*
* @time 2019年11月30日
* @return mixed
*/
2021-02-08 11:12:36 +08:00
public static function getCacheServicesFile(): string
2019-12-06 09:17:40 +08:00
{
return self::cacheDirectory() . 'services.php';
}
/**
*
* @time 2019年11月30日
* @return string
*/
protected static function getCacheRoutesFile(): string
{
return self::cacheDirectory() . 'routes.php';
}
}