Quellcode durchsuchen

feat(config): 为配置获取方法添加默认值支持

- 修改 getConfig 方法签名,新增 $default 参数用于指定默认返回值
- 当配置键不存在时,返回传入的默认值而非 null
- 保持原有调用兼容性,$default 参数默认为 null
runphp vor 4 Monaten
Ursprung
Commit
c26f1d03c3
1 geänderte Dateien mit 4 neuen und 2 gelöschten Zeilen
  1. 4 2
      src/Trait/ConfigTrait.php

+ 4 - 2
src/Trait/ConfigTrait.php

@@ -10,7 +10,8 @@ trait ConfigTrait
     public function __construct(private readonly ExtensionManager $extensionManager, private array $options = [])
     {
     }
-    public function getConfig(string $key = null): mixed
+
+    public function getConfig(string $key = null, mixed $default = null): mixed
     {
         if (empty($this->options)) {
             if (!method_exists($this, 'getExtensionID')) {
@@ -21,8 +22,9 @@ trait ConfigTrait
                 $this->options = array_merge($this->options, $this->getOptions());
             }
         }
-        return $key ? ($this->options[$key] ?? null) : $this->options;
+        return $key ? ($this->options[$key] ?? $default) : $this->options;
     }
+
     public function __get(string $name): mixed
     {
         return $this->getConfig($name);