Created
January 2, 2016 19:14
-
-
Save fer-ri/0243e7b0e95112fdc20f to your computer and use it in GitHub Desktop.
Laravel env() Helper Without Framework
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
| <?php | |
| /** | |
| * 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 (starts_with($value, '"') && ends_with($value, '"')) { | |
| return substr($value, 1, -1); | |
| } | |
| return $value; | |
| } | |
| /** | |
| * 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 != '' && strpos($haystack, $needle) === 0) return true; | |
| } | |
| return false; | |
| } | |
| /** | |
| * 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 ((string) $needle === substr($haystack, -strlen($needle))) return true; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment