Last active
March 12, 2023 14:49
-
-
Save shengyou/7ee32d24b1f0b61fe8349b41a0c22321 to your computer and use it in GitHub Desktop.
env() helper functions without Laravel framework
This file contains 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
<?php | |
if (! function_exists('env')) { | |
/** | |
* Gets the value of an environment variable. Supports boolean, empty and null. | |
* | |
* @param string $key | |
* @param mixed $default | |
* @return mixed | |
*/ | |
function env($key, $default = null) | |
{ | |
$value = getenv($key); | |
if ($value === false) { | |
return value($default); | |
} | |
switch (strtolower($value)) { | |
case 'true': | |
case '(true)': | |
return true; | |
case 'false': | |
case '(false)': | |
return false; | |
case 'empty': | |
case '(empty)': | |
return ''; | |
case 'null': | |
case '(null)': | |
return; | |
} | |
if (strlen($value) > 1 && starts_with($value, '"') && ends_with($value, '"')) { | |
return substr($value, 1, -1); | |
} | |
return $value; | |
} | |
} | |
if (! function_exists('value')) { | |
/** | |
* Return the default value of the given value. | |
* | |
* @param mixed $value | |
* @return mixed | |
*/ | |
function value($value) | |
{ | |
return $value instanceof Closure ? $value() : $value; | |
} | |
} | |
if (! function_exists('starts_with')) { | |
/** | |
* Determine if a given string starts with a given substring. | |
* | |
* @param string $haystack | |
* @param string|array $needles | |
* @return bool | |
*/ | |
function starts_with($haystack, $needles) | |
{ | |
foreach ((array) $needles as $needle) { | |
if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
if (! function_exists('ends_with')) { | |
/** | |
* Determine if a given string ends with a given substring. | |
* | |
* @param string $haystack | |
* @param string|array $needles | |
* @return bool | |
*/ | |
function ends_with($haystack, $needles) | |
{ | |
foreach ((array) $needles as $needle) { | |
if (substr($haystack, -strlen($needle)) === (string) $needle) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment