Przeglądaj źródła

refactor(core): 移除了未使用的 Composer\Json\JsonFile 引用
- 在 CommandService 和 HookAttributeService 中增加了对 ClassNotFoundException 的异常处理- 将 HookAttributeService 类标记为 readonly,提高代码安全性

runphp 7 miesięcy temu
rodzic
commit
c630764f88

+ 0 - 1
src/Service/AutoloadService.php

@@ -3,7 +3,6 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Service;
 
-use Composer\Json\JsonFile;
 use SixShop\Core\Contracts\ExtensionInterface;
 use SixShop\Core\Helper;
 use think\App;

+ 7 - 1
src/Service/CommandService.php

@@ -5,6 +5,7 @@ namespace SixShop\Core\Service;
 
 use SixShop\Core\Helper;
 use think\App;
+use think\exception\ClassNotFoundException;
 
 class CommandService
 {
@@ -16,7 +17,12 @@ class CommandService
     {
         $commands = [];
         foreach (Helper::extension_name_list() as $extensionName) {
-            $commands += $this->autoloadService->getExtension($extensionName)->getCommands();
+            try {
+                $extension = $this->autoloadService->getExtension($extensionName);
+            } catch (ClassNotFoundException $_) {
+                continue;
+            }
+            $commands += $extension->getCommands();
         }
         $closure($commands);
     }

+ 7 - 3
src/Service/HookAttributeService.php

@@ -7,10 +7,10 @@ use ReflectionClass;
 use ReflectionMethod;
 use SixShop\Core\Attribute\Hook;
 use SixShop\Core\Helper;
-use think\App;
 use think\Event;
+use think\exception\ClassNotFoundException;
 
-class HookAttributeService
+readonly class HookAttributeService
 {
     public function __construct(private AutoloadService $autoloadService, private Event $event)
     {
@@ -19,7 +19,11 @@ class HookAttributeService
     public function init(): void
     {
         foreach (Helper::extension_name_list() as $extensionName) {
-            $extension = $this->autoloadService->getExtension($extensionName);
+            try {
+                $extension = $this->autoloadService->getExtension($extensionName);
+            } catch (ClassNotFoundException $_) {
+                continue;
+            }
             $hookClassList = $extension->getHooks();
             foreach ($hookClassList as $hookClass) {
                 $ref = new ReflectionClass($hookClass);