Pārlūkot izejas kodu

feat(core): 支持从 composer.json 读取扩展信息

- 添加对 composer.json 文件的支持以获取扩展元数据
- 当 info.php 不存在时,自动读取 composer.json 中的 name 和 description 字段
- 保留原有 info.php 文件的优先级加载逻辑
- 使用 Composer 的 JsonFile 类安全解析 JSON 配置文件
- 提供默认的扩展信息结构,确保必要的字段存在
- 增强扩展系统的兼容性和灵活性
runphp 3 mēneši atpakaļ
vecāks
revīzija
b5b53b04c0
1 mainītis faili ar 13 papildinājumiem un 3 dzēšanām
  1. 13 3
      src/ExtensionAbstract.php

+ 13 - 3
src/ExtensionAbstract.php

@@ -3,6 +3,7 @@ declare(strict_types=1);
 
 namespace SixShop\Core;
 
+use Composer\Json\JsonFile;
 use Exception;
 use SixShop\Core\Contracts\ExtensionInterface;
 use think\helper\Macroable;
@@ -24,10 +25,19 @@ abstract class ExtensionAbstract implements ExtensionInterface
     public function getInfo(): array
     {
         if (empty($this->info)) {
-            if (!file_exists($this->getBaseDir() . '/info.php')) {
-                throw new Exception('Extension info file not found, please check the extension directory and info.php file.');
+            if (file_exists($this->getBaseDir() . '/info.php')) {
+                $this->info = require $this->getBaseDir() . '/info.php';
+            } else {
+                $localConfig = $this->getBaseDir() . '/composer.json';
+                $file = new JsonFile($localConfig);
+                $localConfig = $file->read();
+                $this->info = [
+                    'id' => static::EXTENSION_ID,
+                    'name' => $localConfig['name'],
+                    'description' => $localConfig['description'],
+                ];
             }
-            $this->info = require $this->getBaseDir() . '/info.php';
+
         }
         return $this->info;
     }