Explorar o código

refactor(system): 重构系统模块,core扩展去除system依赖相关代码迁移

- 更新 Config 类,使用新的 ConfigTrait
- 新增 ConfigTrait 和 EventTrait 特性
- 修改 Extension 类,添加新的 ExtensionStatusHook
- 更新 ExtensionManager 类,使用新的 PaymentExtensionInterface
- 新增 ExtensionStatusHook 和 MacroPageMiddleware 类
runphp hai 7 meses
pai
achega
aaad7b83b1

+ 1 - 1
src/Config.php

@@ -3,7 +3,7 @@ declare(strict_types=1);
 
 namespace SixShop\System;
 
-use SixShop\Core\Trait\ConfigTrait;
+use SixShop\System\Trait\ConfigTrait;
 
 class Config
 {

+ 2 - 0
src/Extension.php

@@ -5,6 +5,7 @@ namespace SixShop\System;
 
 use SixShop\Core\ExtensionAbstract;
 use SixShop\System\Cron\SystemCron;
+use SixShop\System\Hook\ExtensionStatusHook;
 use SixShop\System\Hook\GatheringCrontabEventHook;
 
 class Extension extends ExtensionAbstract
@@ -13,6 +14,7 @@ class Extension extends ExtensionAbstract
     public function getHooks(): array
     {
         return [
+            ExtensionStatusHook::class,
             GatheringCrontabEventHook::class
         ];
     }

+ 1 - 1
src/ExtensionManager.php

@@ -6,7 +6,7 @@ namespace SixShop\System;
 use RuntimeException;
 use SixShop\Core\Contracts\ExtensionInterface;
 use SixShop\Core\Helper;
-use SixShop\Extension\payment\Contracts\PaymentExtensionInterface;
+use SixShop\Payment\Contracts\PaymentExtensionInterface;
 use SixShop\System\Config\ExtensionConfig;
 use SixShop\System\Enum\ExtensionStatusEnum;
 use SixShop\System\Model\ExtensionConfigModel;

+ 36 - 0
src/Hook/ExtensionStatusHook.php

@@ -0,0 +1,36 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\System\Hook;
+
+use Closure;
+use SixShop\Core\Attribute\Hook;
+use SixShop\Core\Event\BeforeRegisterRouteEvent;
+use SixShop\Core\Helper;
+use SixShop\Core\Request;
+use SixShop\System\Enum\ExtensionStatusEnum;
+use SixShop\System\ExtensionManager;
+
+class ExtensionStatusHook
+{
+    public function __construct(protected ExtensionManager $extensionManager)
+    {
+    }
+
+    #[Hook(BeforeRegisterRouteEvent::class)]
+    public function addMiddleware(BeforeRegisterRouteEvent $event)
+    {
+        $event->addMiddleware(Closure::fromCallable([$this, 'handle']));
+
+
+    }
+
+    public function handle(Request $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)
+        };
+    }
+}

+ 24 - 0
src/Middleware/MacroPageMiddleware.php

@@ -0,0 +1,24 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\System\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);
+    }
+}

+ 22 - 0
src/Trait/ConfigTrait.php

@@ -0,0 +1,22 @@
+<?php
+declare(strict_types=1);
+
+namespace SixShop\System\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;
+    }
+}

+ 16 - 0
src/Trait/EventTrait.php

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