mirror of
https://github.com/lkddi/Xboard.git
synced 2026-04-30 16:07:24 +08:00
feat(plugin): auto-decode JSON config values by type in PluginManager
This commit is contained in:
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
namespace App\Services\Plugin;
|
namespace App\Services\Plugin;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\View;
|
|
||||||
use Illuminate\Support\Facades\Route;
|
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class PluginManager
|
|||||||
protected string $pluginPath;
|
protected string $pluginPath;
|
||||||
protected array $loadedPlugins = [];
|
protected array $loadedPlugins = [];
|
||||||
protected bool $pluginsInitialized = false;
|
protected bool $pluginsInitialized = false;
|
||||||
|
protected array $configTypesCache = [];
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@@ -319,7 +320,9 @@ class PluginManager
|
|||||||
->first();
|
->first();
|
||||||
|
|
||||||
if ($dbPlugin && !empty($dbPlugin->config)) {
|
if ($dbPlugin && !empty($dbPlugin->config)) {
|
||||||
$plugin->setConfig(json_decode($dbPlugin->config, true));
|
$values = json_decode($dbPlugin->config, true) ?: [];
|
||||||
|
$values = $this->castConfigValuesByType($pluginCode, $values);
|
||||||
|
$plugin->setConfig($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注册服务提供者
|
// 注册服务提供者
|
||||||
@@ -453,13 +456,15 @@ class PluginManager
|
|||||||
$this->runMigrations($pluginCode);
|
$this->runMigrations($pluginCode);
|
||||||
|
|
||||||
$plugin = $this->loadPlugin($pluginCode);
|
$plugin = $this->loadPlugin($pluginCode);
|
||||||
if ($plugin) {
|
if ($plugin) {
|
||||||
if (!empty($dbPlugin->config)) {
|
if (!empty($dbPlugin->config)) {
|
||||||
$plugin->setConfig(json_decode($dbPlugin->config, true));
|
$values = json_decode($dbPlugin->config, true) ?: [];
|
||||||
}
|
$values = $this->castConfigValuesByType($pluginCode, $values);
|
||||||
|
$plugin->setConfig($values);
|
||||||
|
}
|
||||||
|
|
||||||
$plugin->update($oldVersion, $newVersion);
|
$plugin->update($oldVersion, $newVersion);
|
||||||
}
|
}
|
||||||
|
|
||||||
$dbPlugin->update([
|
$dbPlugin->update([
|
||||||
'version' => $newVersion,
|
'version' => $newVersion,
|
||||||
@@ -567,7 +572,9 @@ class PluginManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($dbPlugin->config)) {
|
if (!empty($dbPlugin->config)) {
|
||||||
$pluginInstance->setConfig(json_decode($dbPlugin->config, true));
|
$values = json_decode($dbPlugin->config, true) ?: [];
|
||||||
|
$values = $this->castConfigValuesByType($pluginCode, $values);
|
||||||
|
$pluginInstance->setConfig($values);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->registerServiceProvider($pluginCode);
|
$this->registerServiceProvider($pluginCode);
|
||||||
@@ -603,7 +610,9 @@ class PluginManager
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!empty($dbPlugin->config)) {
|
if (!empty($dbPlugin->config)) {
|
||||||
$pluginInstance->setConfig(json_decode($dbPlugin->config, true));
|
$values = json_decode($dbPlugin->config, true) ?: [];
|
||||||
|
$values = $this->castConfigValuesByType($dbPlugin->code, $values);
|
||||||
|
$pluginInstance->setConfig($values);
|
||||||
}
|
}
|
||||||
$pluginInstance->schedule($schedule);
|
$pluginInstance->schedule($schedule);
|
||||||
|
|
||||||
@@ -669,4 +678,50 @@ class PluginManager
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 config.json 的类型信息对配置值进行类型转换(仅处理 type=json 键)。
|
||||||
|
*/
|
||||||
|
protected function castConfigValuesByType(string $pluginCode, array $values): array
|
||||||
|
{
|
||||||
|
$types = $this->getConfigTypes($pluginCode);
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
$type = $types[$key] ?? null;
|
||||||
|
|
||||||
|
if ($type === 'json') {
|
||||||
|
if (is_array($value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_string($value) && $value !== '') {
|
||||||
|
$decoded = json_decode($value, true);
|
||||||
|
if (json_last_error() === JSON_ERROR_NONE) {
|
||||||
|
$values[$key] = $decoded;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取并缓存插件 config.json 中的键类型映射。
|
||||||
|
*/
|
||||||
|
protected function getConfigTypes(string $pluginCode): array
|
||||||
|
{
|
||||||
|
if (isset($this->configTypesCache[$pluginCode])) {
|
||||||
|
return $this->configTypesCache[$pluginCode];
|
||||||
|
}
|
||||||
|
$types = [];
|
||||||
|
$configFile = $this->getPluginPath($pluginCode) . '/config.json';
|
||||||
|
if (File::exists($configFile)) {
|
||||||
|
$config = json_decode(File::get($configFile), true);
|
||||||
|
$fields = $config['config'] ?? [];
|
||||||
|
foreach ($fields as $key => $meta) {
|
||||||
|
$types[$key] = is_array($meta) ? ($meta['type'] ?? 'string') : 'string';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->configTypesCache[$pluginCode] = $types;
|
||||||
|
return $types;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user