Forráskód Böngészése

feat(core): 添加扩展订阅功能并优化接口实现

- 在 ExtensionInterface 中新增 getSubscribes 方法定义
- 在 HookAttributeService 中实现扩展订阅的注册逻辑
- 为 ExtensionAbstract 中的方法添加 Override 注解
- 实现默认的 getSubscribes 方法返回空数组
- 完善扩展生命周期方法的接口实现
runphp 3 hónapja
szülő
commit
eec5a5a2f6

+ 5 - 0
src/Contracts/ExtensionInterface.php

@@ -42,6 +42,11 @@ interface ExtensionInterface
      */
     public function getHooks(): array;
 
+    /**
+     * 获取扩展订阅
+     */
+    public function getSubscribes(): array;
+
     /**
      * 获取扩展路由
      */

+ 15 - 0
src/ExtensionAbstract.php

@@ -22,6 +22,7 @@ abstract class ExtensionAbstract implements ExtensionInterface
     /**
      * @throws Exception
      */
+    #[\Override]
     public function getInfo(): array
     {
         if (empty($this->info)) {
@@ -44,6 +45,7 @@ abstract class ExtensionAbstract implements ExtensionInterface
 
     abstract protected function getBaseDir(): string;
 
+    #[\Override]
     public function getConfig(): array
     {
         if (!file_exists($this->getBaseDir() . '/config.php')) {
@@ -52,14 +54,17 @@ abstract class ExtensionAbstract implements ExtensionInterface
         return require $this->getBaseDir() . '/config.php';
     }
 
+    #[\Override]
     public function install(): void
     {
     }
 
+    #[\Override]
     public function uninstall(): void
     {
     }
 
+    #[\Override]
     public function getCommands(): array
     {
         if (!file_exists($this->getBaseDir() . '/command.php')) {
@@ -68,15 +73,23 @@ abstract class ExtensionAbstract implements ExtensionInterface
         return require $this->getBaseDir() . '/command.php';
     }
 
+    #[\Override]
     public function getHooks(): array
     {
         return [];
     }
 
+    #[\Override]
+    public function getSubscribes(): array
+    {
+        return [];
+    }
+
     /**
      * 获取路由
      * @return array<string, string>
      */
+    #[\Override]
     public function getRoutes(): array
     {
         $adminRoute = $this->getBaseDir() . '/route/admin.php';
@@ -91,11 +104,13 @@ abstract class ExtensionAbstract implements ExtensionInterface
         return $routes;
     }
 
+    #[\Override]
     public function getCronJobs(): array
     {
         return [];
     }
 
+    #[\Override]
     public function boot(): void
     {
         $this->isBooted = true;

+ 4 - 0
src/Service/HookAttributeService.php

@@ -31,6 +31,10 @@ readonly class HookAttributeService
             if (!$extension->available()) {
                 continue;
             }
+            $subscribeList = $extension->getSubscribes();
+            foreach ($subscribeList as $subscribe) {
+                $this->event->subscribe($subscribe);
+            }
             $hookClassList = $extension->getHooks();
             foreach ($hookClassList as $hookClass) {
                 $ref = new ReflectionClass($hookClass);