浏览代码

refactor(sixshop-system):优化配置获取方法并添加魔术方法

- 重构 getConfig 方法,增加对 getExtensionID 方法存在性的判断
- 添加 __get 魔术方法,方便通过对象属性方式获取配置
runphp 6 月之前
父节点
当前提交
dd32c8c88c
共有 1 个文件被更改,包括 8 次插入2 次删除
  1. 8 2
      src/Trait/ConfigTrait.php

+ 8 - 2
src/Trait/ConfigTrait.php

@@ -14,9 +14,15 @@ trait ConfigTrait
     public function getConfig(string $key = null): mixed
     {
         if (empty($this->options)) {
-            $extensionID = explode('\\', static::class)[2];
-            $this->options = $this->extensionManager->getExtensionConfig($extensionID);
+            if (!method_exists($this, 'getExtensionID')) {
+                return [];
+            }
+            $this->options = $this->extensionManager->getExtensionConfig($this->getExtensionID());
         }
         return $key ? ($this->options[$key] ?? null) : $this->options;
     }
+    public function __get(string $name): mixed
+    {
+        return $this->getConfig($name);
+    }
 }