Cache
8 months ago
DBPermissions.php
9 months ago
DataEncoder.php
11 months ago
DatabaseOptions.php
2 days ago
Env.php
2 days ago
Escape.php
9 months ago
Glob.php
2 years ago
Hooks.php
3 months ago
Math.php
9 months ago
PluginInfo.php
6 months ago
Sanitize.php
3 months ago
ServerVars.php
2 years ago
SlashMode.php
5 years ago
Strings.php
8 months ago
Times.php
6 months ago
Urls.php
1 month ago
Version.php
1 year ago
WpDefaultDirectories.php
2 years ago
Env.php
42 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Utils; |
| 4 | |
| 5 | /** |
| 6 | * Reads environment variables without assuming getenv() is callable. |
| 7 | * |
| 8 | * getenv() needs no extension, but a host can still drop it through the |
| 9 | * disable_functions ini directive. PHP >= 8.0 deletes a disabled function from |
| 10 | * the function table, so calling it directly raises "Call to undefined function" |
| 11 | * and takes the whole request down; PHP 7 leaves a stub that warns and returns |
| 12 | * null. function_exists() reports false in both cases, which is the guard used |
| 13 | * here, with $_SERVER and $_ENV as the fallback source. |
| 14 | */ |
| 15 | class Env |
| 16 | { |
| 17 | /** |
| 18 | * @param string $name |
| 19 | * @return string|false The value, or false when the variable is not set, |
| 20 | * matching getenv()'s own contract. |
| 21 | */ |
| 22 | public static function get(string $name) |
| 23 | { |
| 24 | // Read the live process environment first: it is the only source that |
| 25 | // reflects a putenv() call made during the current request. |
| 26 | if (function_exists('getenv')) { |
| 27 | $value = getenv($name); |
| 28 | if (is_string($value)) { |
| 29 | return $value; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | foreach ([$_SERVER, $_ENV] as $source) { |
| 34 | if (isset($source[$name]) && is_scalar($source[$name])) { |
| 35 | return (string)$source[$name]; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 |