Przeglądaj źródła

refactor(core): 优化扩展服务初始化流程

- 移除 CommandService 中对 AutoloadService 的依赖
- 在 CoreService 中引入 bootedExtensionMap 存储已启动扩展
- 修改 HookAttributeService 初始化方式,接收已启动扩展数组
- 调整 RegisterRouteService 构造函数,移除 AutoloadService 依赖
- 更新各服务的 init 方法签名,支持传入已启动扩展实例
- 优化控制台命令和 HTTP 请求的路由注册逻辑
- 统一通过 bootedExtensionMap 管理扩展生命周期
runphp 3 miesięcy temu
rodzic
commit
df1278ce9f

+ 8 - 14
src/Service/CommandService.php

@@ -4,27 +4,21 @@ declare(strict_types=1);
 namespace SixShop\Core\Service;
 
 use Closure;
+use SixShop\Core\Contracts\ExtensionInterface;
 use think\exception\ClassNotFoundException;
 use function SixShop\Core\extension_name_list;
 
 readonly class CommandService
 {
-    public function __construct(private AutoloadService $autoloadService)
-    {
-    }
-
-    public function init(Closure $closure): void
+    /**
+     * @param Closure $closure
+     * @param array<string, ExtensionInterface> $bootedExtensions
+     * @return void
+     */
+    public function init(Closure $closure, array $bootedExtensions): void
     {
         $commands = [];
-        foreach (extension_name_list() as $extensionName) {
-            try {
-                $extension = $this->autoloadService->getExtension($extensionName);
-            } catch (ClassNotFoundException $_) {
-                continue;
-            }
-            if (!$extension->available()) {
-                continue;
-            }
+        foreach ($bootedExtensions as $extension) {
             $commands += $extension->getCommands();
         }
         $closure($commands);

+ 21 - 8
src/Service/CoreService.php

@@ -5,8 +5,10 @@ namespace SixShop\Core\Service;
 
 use Composer\InstalledVersions;
 use Composer\Json\JsonFile;
+use getallheaders\Tests\GetAllHeadersTest;
 use ReflectionException;
 use Seld\JsonLint\ParsingException;
+use SixShop\Core\Contracts\ExtensionInterface;
 use SixShop\Core\Exception\ExceptionHandle;
 use SixShop\Core\Plugin;
 use SixShop\Core\Request;
@@ -28,6 +30,12 @@ class CoreService extends Service
         return self::$extensionComposerMap;
     }
 
+    /**
+     * @param array<string, ExtensionInterface> $bootedExtensionMap
+     * @var array
+     */
+    public private(set) array $bootedExtensionMap = [];
+
     /**
      * @throws ParsingException
      */
@@ -93,15 +101,20 @@ class CoreService extends Service
             }
             $extension->boot();
             $this->app->event->trigger('extension.boot', $extension);
+            $this->bootedExtensionMap[$moduleName] = $extension;
         }
-        $this->app->make(HookAttributeService::class)->init();
+        $this->app->make(HookAttributeService::class)->init($this->bootedExtensionMap);
         $this->app->event->trigger('hook_init', $this->app);
-        $this->app->event->listen(HttpRun::class, function () {
-            $this->registerRoutes($this->app->make(RegisterRouteService::class)->init());
-        });
-
-        $this->app->make(CommandService::class)->init(function ($commands) {
-            $this->commands($commands);
-        });
+        if ($this->app->runningInConsole()) {
+            $this->app->make(CommandService::class)->init(function ($commands) {
+                $this->commands($commands);
+            }, $this->bootedExtensionMap);
+        } else {
+            $this->app->event->listen(HttpRun::class, function ()  {
+                $this->registerRoutes(function () {
+                    $this->app->make(RegisterRouteService::class)->init($this->bootedExtensionMap);
+                });
+            });
+        }
     }
 }

+ 5 - 11
src/Service/HookAttributeService.php

@@ -7,30 +7,24 @@ use ReflectionClass;
 use ReflectionException;
 use ReflectionMethod;
 use SixShop\Core\Attribute\Hook;
+use SixShop\Core\Contracts\ExtensionInterface;
 use think\Event;
 use think\exception\ClassNotFoundException;
 use function SixShop\Core\extension_name_list;
 
 readonly class HookAttributeService
 {
-    public function __construct(private AutoloadService $autoloadService, private Event $event)
+    public function __construct(private Event $event)
     {
     }
 
     /**
+     * @param array<string, ExtensionInterface> $bootedExtensions
      * @throws ReflectionException
      */
-    public function init(): void
+    public function init(array $bootedExtensions): void
     {
-        foreach (extension_name_list() as $extensionName) {
-            try {
-                $extension = $this->autoloadService->getExtension($extensionName);
-            } catch (ClassNotFoundException) {
-                continue;
-            }
-            if (!$extension->available()) {
-                continue;
-            }
+        foreach ($bootedExtensions as $extension) {
             $subscribeList = $extension->getSubscribes();
             foreach ($subscribeList as $subscribe) {
                 $this->event->subscribe($subscribe);

+ 17 - 19
src/Service/RegisterRouteService.php

@@ -4,6 +4,7 @@ declare(strict_types=1);
 namespace SixShop\Core\Service;
 
 use Closure;
+use SixShop\Core\Contracts\ExtensionInterface;
 use SixShop\Core\Event\BeforeRegisterRouteEvent;
 use think\facade\Event;
 use think\facade\Route;
@@ -12,29 +13,26 @@ use function SixShop\Core\extension_name_list;
 
 readonly class RegisterRouteService
 {
-    public function __construct(private AutoloadService $autoloadService, private Http $http)
+    public function __construct(private Http $http)
     {
     }
 
-    public function init(): Closure
+    /**
+     * @param array<string, ExtensionInterface> $bootedExtensions
+     */
+    public function init(array $bootedExtensions): void
     {
-        return function () {
-            $appName = $this->http->getName();
-            foreach (extension_name_list() as $extensionName) {
-                $extension = $this->autoloadService->getExtension($extensionName);
-                if (!$extension->available()) {
-                    continue;
-                }
-                $routes = $extension->getRoutes();
-                if (isset($routes[$appName])) {
-                    $routeFile = $routes[$appName];
-                    $event = new BeforeRegisterRouteEvent();
-                    Event::trigger($event);
-                    Route::group($extensionName, function () use ($routeFile) {
-                        include $routeFile;
-                    })->middleware($event->getMiddlewares(), $extensionName);
-                }
+        $appName = $this->http->getName();
+        foreach ($bootedExtensions as $extensionName => $extension) {
+            $routes = $extension->getRoutes();
+            if (isset($routes[$appName])) {
+                $routeFile = $routes[$appName];
+                $event = new BeforeRegisterRouteEvent();
+                Event::trigger($event);
+                Route::group($extensionName, function () use ($routeFile) {
+                    include $routeFile;
+                })->middleware($event->getMiddlewares(), $extensionName);
             }
-        };
+        }
     }
 }