1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Env |
4
|
|
|
{ |
5
|
|
|
const CONVERT_BOOL = 1; |
6
|
|
|
const CONVERT_NULL = 2; |
7
|
|
|
const CONVERT_INT = 4; |
8
|
|
|
const STRIP_QUOTES = 8; |
9
|
|
|
const USE_ENV_ARRAY = 16; |
10
|
|
|
const LOCAL_FIRST = 32; |
11
|
|
|
|
12
|
|
|
public static $options = 15; //All flags enabled |
13
|
|
|
public static $default = null; //Default value if not exists |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Include the global env() function. |
17
|
|
|
* Returns whether the function has been registered or not. |
18
|
|
|
* |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public static function init() |
22
|
|
|
{ |
23
|
|
|
if (function_exists('env')) { |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
include_once dirname(__FILE__).'/env_function.php'; |
28
|
|
|
|
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns an environment variable. |
34
|
|
|
* |
35
|
|
|
* @param string $name |
36
|
|
|
*/ |
37
|
|
|
public static function get($name) |
38
|
|
|
{ |
39
|
|
|
if (self::$options & self::USE_ENV_ARRAY) { |
40
|
|
|
$value = isset($_ENV[$name]) ? $_ENV[$name] : false; |
41
|
|
|
} elseif (self::$options & self::LOCAL_FIRST) { |
42
|
|
|
$value = getenv($name, true); |
43
|
|
|
|
44
|
|
|
if ($value === false) { |
45
|
|
|
$value = getenv($name); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
$value = getenv($name); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($value === false) { |
52
|
|
|
return self::$default; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return self::convert($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Converts the type of values like "true", "false", "null" or "123". |
60
|
|
|
* |
61
|
|
|
* @param string $value |
62
|
|
|
* @param int|null $options |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public static function convert($value, $options = null) |
67
|
|
|
{ |
68
|
|
|
if ($options === null) { |
69
|
|
|
$options = self::$options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
switch (strtolower($value)) { |
73
|
|
|
case 'true': |
74
|
|
|
return ($options & self::CONVERT_BOOL) ? true : $value; |
75
|
|
|
|
76
|
|
|
case 'false': |
77
|
|
|
return ($options & self::CONVERT_BOOL) ? false : $value; |
78
|
|
|
|
79
|
|
|
case 'null': |
80
|
|
|
return ($options & self::CONVERT_NULL) ? null : $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (($options & self::CONVERT_INT) && ctype_digit($value)) { |
84
|
|
|
return (int) $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (($options & self::STRIP_QUOTES) && !empty($value)) { |
88
|
|
|
return self::stripQuotes($value); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Strip quotes. |
96
|
|
|
* |
97
|
|
|
* @param string $value |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
private static function stripQuotes($value) |
102
|
|
|
{ |
103
|
|
|
if ( |
104
|
|
|
($value[0] === '"' && substr($value, -1) === '"') |
105
|
|
|
|| ($value[0] === "'" && substr($value, -1) === "'") |
106
|
|
|
) { |
107
|
|
|
return substr($value, 1, -1); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $value; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|