Просмотр исходного кода

refactor(core): 重构核心模块,去除system扩展依赖

- 重构 AutoloadService,新增 getExtension 方法
- 新增 BeforeRegisterRouteEvent 事件
- 重构 CommandService,使用 AutoloadService 加载扩展命令
-移除 ConfigTrait 和 EventTrait- 重构 HookAttributeService,使用 AutoloadService 加载扩展钩子
- 移除 ExtensionStatusMiddleware 中间件
- 重构 RegisterRouteService,支持事件触发和中间件配置
runphp 7 месяцев назад
Родитель
Сommit
1f4c0bb596

+ 21 - 0
src/Event/BeforeRegisterRouteEvent.php

@@ -0,0 +1,21 @@
+<?php
+declare(strict_types=1);
+namespace SixShop\Core\Event;
+
+class BeforeRegisterRouteEvent
+{
+
+    public function __construct(private array $middlewares = [])
+    {
+    }
+
+    public function getMiddlewares(): array
+    {
+        return $this->middlewares;
+    }
+
+    public function addMiddleware($middleware): void
+    {
+        $this->middlewares[] = $middleware;
+    }
+}

+ 0 - 25
src/Middleware/ExtensionStatusMiddleware.php

@@ -1,25 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace SixShop\Core\Middleware;
-
-use Closure;
-use SixShop\Core\Helper;
-use SixShop\System\Enum\ExtensionStatusEnum;
-use SixShop\System\ExtensionManager;
-
-class ExtensionStatusMiddleware
-{
-    public function __construct(protected ExtensionManager $extensionManager)
-    {
-    }
-
-    public function handle($request, Closure $next, $moduleName)
-    {
-        $extensionModel = $this->extensionManager->getInfo($moduleName);
-        return match ($extensionModel->status) {
-            ExtensionStatusEnum::ENABLED => $next($request),
-            default => Helper::error_response(msg: '模块`' . $moduleName . '`未启用', httpCode: 403)
-        };
-    }
-}

+ 0 - 24
src/Middleware/MacroPageMiddleware.php

@@ -1,24 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace SixShop\Core\Middleware;
-
-use Closure;
-use SixShop\Core\Request;
-use think\Exception;
-use think\Response;
-
-class MacroPageMiddleware
-{
-    public function handle(Request $request, Closure $next): Response
-    {
-        $request->macro('pageAndLimit', function (): array {
-            $page = input('page/d', 1);
-            $limit = input('limit/d', 10);
-            throw_if($page < 1, Exception::class,'页码不能小于1');
-            throw_if($limit < 1 || $limit > 100, Exception::class,'每页数量必须在1-100之间');
-            return ['page' => $page, 'list_rows' => $limit];
-        });
-        return $next($request);
-    }
-}

+ 16 - 6
src/Service/AutoloadService.php

@@ -3,21 +3,24 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Service;
 
-use Composer\InstalledVersions;
-use Composer\Installer;
 use Composer\Json\JsonFile;
+use SixShop\Core\Contracts\ExtensionInterface;
 use SixShop\Core\Helper;
 use think\App;
 
 
 class AutoloadService
 {
-    public function init(App $app): void
+    public function __construct(private App $app)
+    {
+    }
+
+    public function init(): void
     {
         $extensionPath = Helper::extension_path();
-        $classLoader = $app->classLoader;
+        $classLoader = $this->app->classLoader;
         foreach (Helper::extension_name_list() as $moduleName) {
-            $dir = $extensionPath.$moduleName;
+            $dir = $extensionPath . $moduleName;
             if (file_exists($dir . '/composer.json')) {
                 $composerJson = new JsonFile($dir . '/composer.json');
                 $composer = $composerJson->read();
@@ -30,6 +33,7 @@ class AutoloadService
                 foreach ($autoload['files'] as $file) {
                     require_once $dir . '/' . $file;
                 }
+                $extensionClass = $composer['extra']['sixshop']['class'];
             } else {
                 $namespace = "SixShop\\Extension\\$moduleName\\";
                 $path = $dir . '/src';
@@ -40,8 +44,14 @@ class AutoloadService
                         require_once $helperFunctionFile;
                     }
                 }
+                $extensionClass = $namespace . 'Extension';
             }
-            $app->bind('extension.' . $moduleName, $namespace.'Extension');
+            $this->app->bind('extension.' . $moduleName, $extensionClass);
         }
     }
+
+    public function getExtension(string $moduleName): ExtensionInterface
+    {
+        return $this->app->make('extension.' . $moduleName);
+    }
 }

+ 6 - 4
src/Service/CommandService.php

@@ -4,17 +4,19 @@ declare(strict_types=1);
 namespace SixShop\Core\Service;
 
 use SixShop\Core\Helper;
-use SixShop\System\ExtensionManager;
 use think\App;
 
 class CommandService
 {
-    public function init(App $app, \Closure $closure): void
+    public function __construct(private AutoloadService $autoloadService)
+    {
+    }
+
+    public function init(\Closure $closure): void
     {
-        $extensionManager = $app->make(ExtensionManager::class);
         $commands = [];
         foreach (Helper::extension_name_list() as $extensionName) {
-            $commands += $extensionManager->getExtension($extensionName)->getCommands();
+            $commands += $this->autoloadService->getExtension($extensionName)->getCommands();
         }
         $closure($commands);
     }

+ 4 - 3
src/Service/CoreService.php

@@ -35,13 +35,14 @@ class CoreService extends Service
 
     public function boot(): void
     {
-        $this->app->make(AutoloadService::class)->init($this->app);
-        $this->app->make(HookAttributeService::class)->init($this->app);
+        $this->app->make(AutoloadService::class)->init();
+        $this->app->make(HookAttributeService::class)->init();
+        $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));
         });
 
-        $this->app->make(CommandService::class)->init($this->app, function ($commands) {
+        $this->app->make(CommandService::class)->init(function ($commands) {
             $this->commands($commands);
         });
     }

+ 8 - 11
src/Service/HookAttributeService.php

@@ -7,21 +7,19 @@ use ReflectionClass;
 use ReflectionMethod;
 use SixShop\Core\Attribute\Hook;
 use SixShop\Core\Helper;
-use SixShop\System\Enum\ExtensionStatusEnum;
-use SixShop\System\ExtensionManager;
 use think\App;
-use think\facade\Event;
+use think\Event;
 
 class HookAttributeService
 {
-    public function init(App $app): void
+    public function __construct(private AutoloadService $autoloadService, private Event $event)
+    {
+    }
+
+    public function init(): void
     {
-        $extensionManager = $app->make(ExtensionManager::class);
         foreach (Helper::extension_name_list() as $extensionName) {
-            if ($extensionManager->getInfo($extensionName)->status !== ExtensionStatusEnum::ENABLED) {
-                continue;
-            }
-            $extension = $extensionManager->getExtension($extensionName);
+            $extension = $this->autoloadService->getExtension($extensionName);
             $hookClassList = $extension->getHooks();
             foreach ($hookClassList as $hookClass) {
                 $ref = new ReflectionClass($hookClass);
@@ -30,12 +28,11 @@ class HookAttributeService
                     foreach ($attributes as $attr) {
                         $hookNameList = (array)$attr->newInstance()->hook;
                         foreach ($hookNameList as $hookName) {
-                            Event::listen($hookName, [$hookClass, $method->getName()]);
+                            $this->event->listen($hookName, [$hookClass, $method->getName()]);
                         }
                     }
                 }
             }
         }
-        $app->event->trigger('hook_init', $app);
     }
 }

+ 14 - 12
src/Service/RegisterRouteService.php

@@ -3,30 +3,32 @@ declare(strict_types=1);
 
 namespace SixShop\Core\Service;
 
+use SixShop\Core\Event\BeforeRegisterRouteEvent;
 use SixShop\Core\Helper;
-use SixShop\Core\Middleware\ExtensionStatusMiddleware;
-use SixShop\System\ExtensionManager;
-use think\App;
-use think\event\RouteLoaded;
+use think\facade\Event;
 use think\facade\Route;
+use think\Http;
 
 class RegisterRouteService
 {
-    public function init(App $app)
+    public function __construct(private AutoloadService $autoloadService, private Http $http)
     {
-        $extensionManager = $app->make(ExtensionManager::class);
-        return function () use ($extensionManager, $app) {
-            $appName = $app->http->getName();
+    }
+
+    public function init(): \Closure
+    {
+        return function ()  {
+            $appName = $this->http->getName();
             foreach (Helper::extension_name_list() as $extensionName) {
-                $extension = $extensionManager->getExtension($extensionName);
+                $extension = $this->autoloadService->getExtension($extensionName);
                 $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(
-                        ExtensionStatusMiddleware::class, $extensionName
-                    );
+                    })->middleware($event->getMiddlewares(), $extensionName);
                 }
             }
         };

+ 0 - 22
src/Trait/ConfigTrait.php

@@ -1,22 +0,0 @@
-<?php
-declare(strict_types=1);
-
-namespace SixShop\Core\Trait;
-
-use SixShop\System\ExtensionManager;
-
-trait ConfigTrait
-{
-    public function __construct(private readonly ExtensionManager $extensionManager, private array $options = [])
-    {
-    }
-
-    public function getConfig(string $key = null): mixed
-    {
-        if (empty($this->options)) {
-            $extensionID = explode('\\', static::class)[2];
-            $this->options = $this->extensionManager->getExtensionConfig($extensionID);
-        }
-        return $key ? ($this->options[$key] ?? null) : $this->options;
-    }
-}

+ 0 - 16
src/Trait/EventTrait.php

@@ -1,16 +0,0 @@
-<?php
-declare(strict_types=1);
-namespace SixShop\Core\Trait;
-
-
-use think\facade\Event;
-
-trait EventTrait
-{
-    public static function trigger(mixed $content): static
-    {
-        $event = new static($content);
-        Event::trigger($event);
-        return $event;
-    }
-}