浏览代码

feat: config添加getter方法

runphp 5 月之前
父节点
当前提交
1985a6872d
共有 1 个文件被更改,包括 11 次插入1 次删除
  1. 11 1
      src/Trait/ConfigTrait.php

+ 11 - 1
src/Trait/ConfigTrait.php

@@ -10,7 +10,6 @@ trait ConfigTrait
     public function __construct(private readonly ExtensionManager $extensionManager, private array $options = [])
     {
     }
-
     public function getConfig(string $key = null): mixed
     {
         if (empty($this->options)) {
@@ -33,4 +32,15 @@ trait ConfigTrait
     {
         return $this->getConfig($name) !== null;
     }
+
+    public function __call(string $name, array $arguments): mixed
+    {
+        if (str_starts_with($name, 'get')) {
+            $name = lcfirst(substr($name, 3));
+        } else {
+            throw new \BadMethodCallException("Method {$name} does not exist.");
+        }
+        $name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
+        return $this->getConfig($name);
+    }
 }