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