Skip to content

Commit cff6800

Browse files
committed
feat : ✍🏾 improve get config - support chain
exp : get('key.key1.key2')
1 parent 77aa166 commit cff6800

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/Traits/HasConfig.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,23 @@ static function all(): array
5555
* Get single config
5656
*
5757
* @param string $name
58+
* @param mixed $default
5859
* @return mixed
5960
*/
60-
static function get(string $name): mixed
61+
static function get(string $name, mixed $default = null): mixed
6162
{
62-
return isset(self::$configs[$name]) ? self::$configs[$name] : null;
63+
$keys = explode('.', $name);
64+
$value = self::$configs;
65+
66+
foreach ($keys as $key) {
67+
if (isset($value[$key])) {
68+
$value = $value[$key];
69+
} else {
70+
return ($default) ? $default: null;
71+
}
72+
}
73+
74+
return $value;
6375
}
6476

6577
}

0 commit comments

Comments
 (0)