1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrilot\DotEnv; |
4
|
|
|
|
5
|
|
|
use Arrilot\DotEnv\Exceptions\MissingVariableException; |
6
|
|
|
|
7
|
|
|
class DotEnv |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Key-value storage. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected static $variables = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Required variables. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected static $required = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Were variables loaded? |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected static $isLoaded = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Load .env.php file or array. |
32
|
|
|
* |
33
|
|
|
* @param string|array $source |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public static function load($source) |
38
|
|
|
{ |
39
|
|
|
self::$variables = is_array($source) ? $source : require $source; |
40
|
|
|
self::$isLoaded = true; |
41
|
|
|
|
42
|
|
|
self::checkRequiredVariables(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get env variables. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public static function all() |
51
|
|
|
{ |
52
|
|
|
return self::$variables; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get env variable. |
57
|
|
|
* |
58
|
|
|
* @param string $key |
59
|
|
|
* @param mixed $default |
60
|
|
|
* |
61
|
|
|
* @return mixed |
62
|
|
|
*/ |
63
|
|
|
public static function get($key, $default = null) |
64
|
|
|
{ |
65
|
|
|
return isset(self::$variables[$key]) ? self::$variables[$key] : $default; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set env variable. |
70
|
|
|
* |
71
|
|
|
* @param string|array $keys |
72
|
|
|
* @param mixed $value |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public static function set($keys, $value = null) |
77
|
|
|
{ |
78
|
|
|
if (is_array($keys)) { |
79
|
|
|
self::$variables = array_merge(self::$variables, $keys); |
80
|
|
|
} else { |
81
|
|
|
self::$variables[$keys] = $value; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set required variables. |
87
|
|
|
* |
88
|
|
|
* @param array $variables |
89
|
|
|
*/ |
90
|
|
|
public static function setRequired(array $variables) |
91
|
|
|
{ |
92
|
|
|
self::$required = $variables; |
93
|
|
|
|
94
|
|
|
if (self::$isLoaded) { |
95
|
|
|
self::checkRequiredVariables(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Delete all variables. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
public static function flush() |
105
|
|
|
{ |
106
|
|
|
self::$variables = []; |
107
|
|
|
self::$isLoaded = false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Throw exception if any of required variables was not loaded. |
112
|
|
|
* |
113
|
|
|
* @throws MissingVariableException |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
protected static function checkRequiredVariables() |
118
|
|
|
{ |
119
|
|
|
foreach (self::$required as $key) { |
120
|
|
|
if (!isset(self::$variables[$key])) { |
121
|
|
|
throw new MissingVariableException(".env variable '{$key}' is missing"); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|