@@ -17,6 +17,7 @@ class PluginManager
1717 protected string $ pluginPath ;
1818 protected array $ loadedPlugins = [];
1919 protected bool $ pluginsInitialized = false ;
20+ protected array $ configTypesCache = [];
2021
2122 public function __construct ()
2223 {
@@ -319,7 +320,9 @@ public function enable(string $pluginCode): bool
319320 ->first ();
320321
321322 if ($ dbPlugin && !empty ($ dbPlugin ->config )) {
322- $ plugin ->setConfig (json_decode ($ dbPlugin ->config , true ));
323+ $ values = json_decode ($ dbPlugin ->config , true ) ?: [];
324+ $ values = $ this ->castConfigValuesByType ($ pluginCode , $ values );
325+ $ plugin ->setConfig ($ values );
323326 }
324327
325328 // 注册服务提供者
@@ -453,13 +456,15 @@ public function update(string $pluginCode): bool
453456 $ this ->runMigrations ($ pluginCode );
454457
455458 $ plugin = $ this ->loadPlugin ($ pluginCode );
456- if ($ plugin ) {
457- if (!empty ($ dbPlugin ->config )) {
458- $ plugin ->setConfig (json_decode ($ dbPlugin ->config , true ));
459- }
459+ if ($ plugin ) {
460+ if (!empty ($ dbPlugin ->config )) {
461+ $ values = json_decode ($ dbPlugin ->config , true ) ?: [];
462+ $ values = $ this ->castConfigValuesByType ($ pluginCode , $ values );
463+ $ plugin ->setConfig ($ values );
464+ }
460465
461- $ plugin ->update ($ oldVersion , $ newVersion );
462- }
466+ $ plugin ->update ($ oldVersion , $ newVersion );
467+ }
463468
464469 $ dbPlugin ->update ([
465470 'version ' => $ newVersion ,
@@ -567,7 +572,9 @@ public function initializeEnabledPlugins(): void
567572 }
568573
569574 if (!empty ($ dbPlugin ->config )) {
570- $ pluginInstance ->setConfig (json_decode ($ dbPlugin ->config , true ));
575+ $ values = json_decode ($ dbPlugin ->config , true ) ?: [];
576+ $ values = $ this ->castConfigValuesByType ($ pluginCode , $ values );
577+ $ pluginInstance ->setConfig ($ values );
571578 }
572579
573580 $ this ->registerServiceProvider ($ pluginCode );
@@ -603,7 +610,9 @@ public function registerPluginSchedules(Schedule $schedule): void
603610 return ;
604611 }
605612 if (!empty ($ dbPlugin ->config )) {
606- $ pluginInstance ->setConfig (json_decode ($ dbPlugin ->config , true ));
613+ $ values = json_decode ($ dbPlugin ->config , true ) ?: [];
614+ $ values = $ this ->castConfigValuesByType ($ dbPlugin ->code , $ values );
615+ $ pluginInstance ->setConfig ($ values );
607616 }
608617 $ pluginInstance ->schedule ($ schedule );
609618
@@ -669,4 +678,50 @@ public static function installDefaultPlugins(): void
669678 }
670679 }
671680 }
681+
682+ /**
683+ * 根据 config.json 的类型信息对配置值进行类型转换(仅处理 type=json 键)。
684+ */
685+ protected function castConfigValuesByType (string $ pluginCode , array $ values ): array
686+ {
687+ $ types = $ this ->getConfigTypes ($ pluginCode );
688+ foreach ($ values as $ key => $ value ) {
689+ $ type = $ types [$ key ] ?? null ;
690+
691+ if ($ type === 'json ' ) {
692+ if (is_array ($ value )) {
693+ continue ;
694+ }
695+
696+ if (is_string ($ value ) && $ value !== '' ) {
697+ $ decoded = json_decode ($ value , true );
698+ if (json_last_error () === JSON_ERROR_NONE ) {
699+ $ values [$ key ] = $ decoded ;
700+ }
701+ }
702+ }
703+ }
704+ return $ values ;
705+ }
706+
707+ /**
708+ * 读取并缓存插件 config.json 中的键类型映射。
709+ */
710+ protected function getConfigTypes (string $ pluginCode ): array
711+ {
712+ if (isset ($ this ->configTypesCache [$ pluginCode ])) {
713+ return $ this ->configTypesCache [$ pluginCode ];
714+ }
715+ $ types = [];
716+ $ configFile = $ this ->getPluginPath ($ pluginCode ) . '/config.json ' ;
717+ if (File::exists ($ configFile )) {
718+ $ config = json_decode (File::get ($ configFile ), true );
719+ $ fields = $ config ['config ' ] ?? [];
720+ foreach ($ fields as $ key => $ meta ) {
721+ $ types [$ key ] = is_array ($ meta ) ? ($ meta ['type ' ] ?? 'string ' ) : 'string ' ;
722+ }
723+ }
724+ $ this ->configTypesCache [$ pluginCode ] = $ types ;
725+ return $ types ;
726+ }
672727}
0 commit comments