1 | <?php |
||
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) |
||
44 | |||
45 | /** |
||
46 | * Copy all variables to putenv(). |
||
47 | * |
||
48 | * @param string $prefix |
||
49 | */ |
||
50 | public static function copyVarsToPutenv($prefix = 'PHP_') |
||
60 | |||
61 | /** |
||
62 | * Copy all variables to $_ENV. |
||
63 | */ |
||
64 | public static function copyVarsToEnv() |
||
70 | |||
71 | /** |
||
72 | * Copy all variables to $_SERVER. |
||
73 | */ |
||
74 | public static function copyVarsToServer() |
||
80 | |||
81 | /** |
||
82 | * Get env variables. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | public static function all() |
||
90 | |||
91 | /** |
||
92 | * Get env variable. |
||
93 | * |
||
94 | * @param string $key |
||
95 | * @param mixed $default |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public static function get($key, $default = null) |
||
103 | |||
104 | /** |
||
105 | * Set env variable. |
||
106 | * |
||
107 | * @param string|array $keys |
||
108 | * @param mixed $value |
||
109 | * |
||
110 | * @return void |
||
111 | */ |
||
112 | public static function set($keys, $value = null) |
||
120 | |||
121 | /** |
||
122 | * Set required variables. |
||
123 | * |
||
124 | * @param array $variables |
||
125 | */ |
||
126 | public static function setRequired(array $variables) |
||
134 | |||
135 | /** |
||
136 | * Delete all variables. |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public static function flush() |
||
145 | |||
146 | /** |
||
147 | * Throw exception if any of required variables was not loaded. |
||
148 | * |
||
149 | * @throws MissingVariableException |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | protected static function checkRequiredVariables() |
||
161 | } |
||
162 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: