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

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

- 重构 getConfig 方法,增加对 getExtensionID 方法存在性的判断
- 添加 __get 魔术方法,方便通过对象属性方式获取配置
runphp 6 месяцев назад
Родитель
Сommit
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);
+    }
 }