Created
November 30, 2019 11:25
-
-
Save felixdorn/7db21ed76229cfd188baa700e2cd8982 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param string $key | |
* @param string|null $default | |
* @return string|boolean | |
*/ | |
function env(string $key, string $default = null) | |
{ | |
$getKey = static function ($key, $default) { | |
if (notEmpty(array_get($_SERVER, $key))) { | |
return $_SERVER[$key]; | |
} | |
if (notEmpty(array_get($_ENV, $key))) { | |
return $_ENV[$key]; | |
} | |
if (notEmpty(getenv($key))) { | |
return getenv($key); | |
} | |
return $default ?? false; | |
}; | |
$key = trim($getKey($key, $default)); | |
if (in_array($key, ['yes', 'true', 'TRUE'])) { | |
return true; | |
} | |
if (in_array($key, ['no', 'false', 'FALSE'])) { | |
return false; | |
} | |
if (is_numeric($key)) { | |
return (int)$key; | |
} | |
return $key; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment