Skip to content

Commit 77c6a0a

Browse files
committed
feat: add new global func for get env
1 parent 234bbaf commit 77c6a0a

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/OS.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function getUserHomeDir(bool $refresh = false): string
5454
return self::$homeDir;
5555
}
5656

57-
if (!$home = self::getEnvVal('HOME')) {
57+
if (!$home = self::getEnvStrVal('HOME')) {
5858
$isWin = self::isWindows();
5959

6060
// home on windows
@@ -235,15 +235,26 @@ public static function getTempDir(): string
235235
return $tmp;
236236
}
237237

238+
/**
239+
* @param string $key
240+
* @param string|mixed $default
241+
*
242+
* @return mixed
243+
*/
244+
public static function getEnvVal(string $key, string $default = '')
245+
{
246+
return getenv($key) ?: ($_SERVER[$key] ?? $default);
247+
}
248+
238249
/**
239250
* @param string $key
240251
* @param string $default
241252
*
242253
* @return string
243254
*/
244-
public static function getEnvVal(string $key, string $default = ''): string
255+
public static function getEnvStrVal(string $key, string $default = ''): string
245256
{
246-
return getenv($key) ?: (string)($_SERVER[$key] ?? $default);
257+
return (string)self::getEnvVal($key, $default);
247258
}
248259

249260
/**

src/Util/PhpDotEnv.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class PhpDotEnv
5656
*
5757
* @return static
5858
*/
59-
public static function load(string $fileDir, string $fileName = '.env')
59+
public static function load(string $fileDir, string $fileName = '.env'): self
6060
{
6161
return new self($fileDir, $fileName);
6262
}

src/func.php

+13
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,16 @@ function eprints(...$vars): void
113113
echo Toolkit\Stdlib\Php::exportVar(...$vars), PHP_EOL;
114114
}
115115
}
116+
117+
if (!function_exists('env')) {
118+
/**
119+
* @param string $key
120+
* @param string $default
121+
*
122+
* @return string
123+
*/
124+
function env(string $key, string $default = ''): string
125+
{
126+
return Toolkit\Stdlib\OS::getEnvStrVal($key, $default);
127+
}
128+
}

0 commit comments

Comments
 (0)