mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-23 19:37:35 +08:00
refactor: core plugins to plugins-core
This commit is contained in:
@@ -14,6 +14,7 @@ use Illuminate\Support\Str;
|
||||
class PluginManager
|
||||
{
|
||||
protected string $pluginPath;
|
||||
protected string $corePluginPath;
|
||||
protected array $loadedPlugins = [];
|
||||
protected bool $pluginsInitialized = false;
|
||||
protected array $configTypesCache = [];
|
||||
@@ -21,6 +22,7 @@ class PluginManager
|
||||
public function __construct()
|
||||
{
|
||||
$this->pluginPath = base_path('plugins');
|
||||
$this->corePluginPath = base_path('plugins-core');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,14 +33,42 @@ class PluginManager
|
||||
return 'Plugin\\' . Str::studly($pluginCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取插件的基础路径
|
||||
*/
|
||||
public function resolvePluginPath(string $pluginCode): ?string
|
||||
{
|
||||
$dirName = Str::studly($pluginCode);
|
||||
$corePath = $this->corePluginPath . '/' . $dirName;
|
||||
if (File::isDirectory($corePath)) {
|
||||
return $corePath;
|
||||
}
|
||||
$userPath = $this->pluginPath . '/' . $dirName;
|
||||
if (File::isDirectory($userPath)) {
|
||||
return $userPath;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getPluginPath(string $pluginCode): string
|
||||
{
|
||||
return $this->resolvePluginPath($pluginCode)
|
||||
?? $this->pluginPath . '/' . Str::studly($pluginCode);
|
||||
}
|
||||
|
||||
public function getUserPluginPath(string $pluginCode): string
|
||||
{
|
||||
return $this->pluginPath . '/' . Str::studly($pluginCode);
|
||||
}
|
||||
|
||||
public function isCorePlugin(string $pluginCode): bool
|
||||
{
|
||||
$dirName = Str::studly($pluginCode);
|
||||
return File::isDirectory($this->corePluginPath . '/' . $dirName);
|
||||
}
|
||||
|
||||
public function getPluginPaths(): array
|
||||
{
|
||||
return [$this->corePluginPath, $this->pluginPath];
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载插件类
|
||||
*/
|
||||
@@ -399,17 +429,19 @@ class PluginManager
|
||||
*/
|
||||
public function delete(string $pluginCode): bool
|
||||
{
|
||||
// 先卸载插件
|
||||
if (Plugin::where('code', $pluginCode)->exists()) {
|
||||
$this->uninstall($pluginCode);
|
||||
}
|
||||
|
||||
$pluginPath = $this->getPluginPath($pluginCode);
|
||||
if ($this->isCorePlugin($pluginCode)) {
|
||||
throw new \Exception('核心插件不允许删除');
|
||||
}
|
||||
|
||||
$pluginPath = $this->getUserPluginPath($pluginCode);
|
||||
if (!File::exists($pluginPath)) {
|
||||
throw new \Exception('插件不存在');
|
||||
}
|
||||
|
||||
// 删除插件目录
|
||||
File::deleteDirectory($pluginPath);
|
||||
|
||||
return true;
|
||||
@@ -527,7 +559,7 @@ class PluginManager
|
||||
throw new \Exception('插件配置文件格式错误');
|
||||
}
|
||||
|
||||
$targetPath = $this->pluginPath . '/' . Str::studly($config['code']);
|
||||
$targetPath = $this->getUserPluginPath($config['code']);
|
||||
if (File::exists($targetPath)) {
|
||||
$installedConfigPath = $targetPath . '/config.json';
|
||||
if (!File::exists($installedConfigPath)) {
|
||||
@@ -678,12 +710,27 @@ class PluginManager
|
||||
*/
|
||||
public static function installDefaultPlugins(): void
|
||||
{
|
||||
foreach (Plugin::PROTECTED_PLUGINS as $pluginCode) {
|
||||
if (!Plugin::where('code', $pluginCode)->exists()) {
|
||||
$pluginManager = app(self::class);
|
||||
$pluginManager->install($pluginCode);
|
||||
$pluginManager->enable($pluginCode);
|
||||
Log::info("Installed and enabled default plugin: {$pluginCode}");
|
||||
$pluginManager = app(self::class);
|
||||
$coreDir = base_path('plugins-core');
|
||||
|
||||
if (!File::isDirectory($coreDir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (File::directories($coreDir) as $directory) {
|
||||
$configFile = $directory . '/config.json';
|
||||
if (!File::exists($configFile)) {
|
||||
continue;
|
||||
}
|
||||
$config = json_decode(File::get($configFile), true);
|
||||
$code = $config['code'] ?? null;
|
||||
if (!$code) {
|
||||
continue;
|
||||
}
|
||||
if (!Plugin::where('code', $code)->exists()) {
|
||||
$pluginManager->install($code);
|
||||
$pluginManager->enable($code);
|
||||
Log::info("Installed and enabled core plugin: {$code}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user