1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Utils; |
12
|
|
|
|
13
|
|
|
use Dotenv\Dotenv; |
14
|
|
|
use Phalcon\Text; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Env |
18
|
|
|
* Allow to access environment variable easily |
19
|
|
|
* |
20
|
|
|
* Example usage: |
21
|
|
|
* ```php |
22
|
|
|
* $this->SET_APPLICATION_ENV('production'); // SET APPLICATION_ENV to 'production' |
23
|
|
|
* self::SET_APPLICATION_ENV('production'); // SET APPLICATION_ENV to 'production' |
24
|
|
|
* $this->GET_APPLICATION_ENV('default'); // GET APPLICATION_ENV value or 'default' |
25
|
|
|
* self::GET_APPLICATION_ENV('default'); // GET APPLICATION_ENV value or 'default' |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* @todo fix dotenv mandatory file |
29
|
|
|
* @author Julien Turbide <[email protected]> |
30
|
|
|
* @copyright Zemit Team <[email protected]> |
31
|
|
|
* |
32
|
|
|
* @since 1.0 |
33
|
|
|
* @version 1.0 |
34
|
|
|
* |
35
|
|
|
* @package Zemit\Utils |
36
|
|
|
*/ |
37
|
|
|
class Env |
38
|
|
|
{ |
39
|
|
|
public static bool $initialized = false; |
40
|
|
|
|
41
|
|
|
public static Dotenv $dotenv; |
42
|
|
|
public static array $vars = []; |
43
|
|
|
|
44
|
|
|
public static ?array $paths = null; |
45
|
|
|
public static array $names = ['.env']; |
46
|
|
|
public static string $type = 'mutable'; |
47
|
|
|
public static bool $shortCircuit = true; |
48
|
|
|
public static ?string $fileEncoding = null; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get .env directories |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public static function getPaths() { |
55
|
|
|
if (is_null(self::$paths)) { |
56
|
|
|
self::$paths = []; |
57
|
|
|
foreach (['ENV_PATH', 'ROOT_PATH', 'APP_PATH'] as $constant) { |
58
|
|
|
if (defined($constant)) { |
59
|
|
|
$path = constant($constant); |
60
|
|
|
if (!is_null($path)) { |
61
|
|
|
self::$paths []= $constant === 'APP_PATH'? dirname($path) : $path; |
62
|
|
|
break; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
if (empty(self::$paths)) { |
67
|
|
|
self::$paths []= getcwd(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return self::$paths; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set .env directories |
75
|
|
|
* @param $paths |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
|
|
public static function setPaths(array $paths = null): void { |
79
|
|
|
self::$paths = $paths; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get .env file names |
84
|
|
|
* @return string|string[] |
85
|
|
|
*/ |
86
|
|
|
public static function getNames() { |
87
|
|
|
return self::$names; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set .env file names |
92
|
|
|
* @param $names |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
|
public static function setNames($names = null): void { |
96
|
|
|
self::$names = $names; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get Dotenv type |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public static function getType() { |
104
|
|
|
return ucfirst(Text::camelize(strtolower(self::$type ?? 'mutable'))); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set Dotenv type |
109
|
|
|
* @param string|null $type |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
|
|
public static function setType(?string $type = null): void { |
113
|
|
|
$domain = ['mutable', 'immutable', 'unsafe-mutable', 'unsafe-immutable']; |
114
|
|
|
self::$type = (!in_array(strtolower($type), $domain, true)) ? strtolower($type) : 'mutable'; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return boolean |
119
|
|
|
*/ |
120
|
|
|
public static function getShortCircuit(): bool { |
121
|
|
|
return self::$shortCircuit; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param bool $shortCircuit |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
public static function setShortCircuit(bool $shortCircuit = true): void { |
129
|
|
|
self::$shortCircuit = (bool)$shortCircuit; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return ?string |
134
|
|
|
*/ |
135
|
|
|
public static function getFileEncoding(): ?string { |
136
|
|
|
return self::$fileEncoding; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param ?string $fileEncoding |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public static function setFileEncoding(?string $fileEncoding = null): void { |
144
|
|
|
self::$fileEncoding = $fileEncoding; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* |
149
|
|
|
* @param $paths |
150
|
|
|
* @param $names |
151
|
|
|
* @param bool $shortCircuit |
152
|
|
|
* @param string|null $fileEncoding |
153
|
|
|
* @param $type |
154
|
|
|
* @return Dotenv |
155
|
|
|
*/ |
156
|
|
|
public static function initialize($paths = null, $names = null, bool $shortCircuit = null, string $fileEncoding = null, $type = null): Dotenv |
157
|
|
|
{ |
158
|
|
|
if (!self::$initialized) { |
159
|
|
|
$type ??= self::getType(); |
160
|
|
|
$paths ??= self::getPaths(); |
161
|
|
|
$names ??= self::getNames(); |
162
|
|
|
$shortCircuit ??= self::getShortCircuit(); |
163
|
|
|
$fileEncoding ??= self::getFileEncoding(); |
164
|
|
|
|
165
|
|
|
self::$dotenv = Dotenv::{'create'. $type}($paths, $names, $shortCircuit, $fileEncoding); |
166
|
|
|
self::$vars = self::$dotenv->load(); |
167
|
|
|
|
168
|
|
|
self::$initialized = true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return self::$dotenv; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return Dotenv |
176
|
|
|
*/ |
177
|
|
|
public static function getDotenv() { |
178
|
|
|
return self::$dotenv ?? self::initialize(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get or set the environment variable |
183
|
|
|
* Ex. (SET): $this->SET_APPLICATION_ENV('production'); |
184
|
|
|
* Ex. (SET): self::SET_APPLICATION_ENV('production'); |
185
|
|
|
* Ex. (SET): Env::SET_APPLICATION_ENV('production'); |
186
|
|
|
* Ex. (GET): $this->GET_APPLICATION_ENV('development'); |
187
|
|
|
* Ex. (GET): self::GET_APPLICATION_ENV('development'); |
188
|
|
|
* Ex. (GET): Env::GET_APPLICATION_ENV('development'); |
189
|
|
|
* @param string $name key to get/set |
190
|
|
|
* @param mixed $arguments Default fallback value for get, or value to set for set |
191
|
|
|
* @return mixed Return void for set, and return the environment variable value, or default value for get |
192
|
|
|
*/ |
193
|
|
|
public static function call($name, $arguments) |
194
|
|
|
{ |
195
|
|
|
$ret = null; |
|
|
|
|
196
|
|
|
$getSet = 'set'; |
197
|
|
|
if (strpos($name, 'SET_') === 0) { |
198
|
|
|
$name = substr($name, 0, 4); |
199
|
|
|
} elseif (strpos($name, 'set') === 0) { |
200
|
|
|
$name = substr($name, 0, 3); |
201
|
|
|
} elseif (strpos($name, 'GET_') === 0) { |
202
|
|
|
$getSet = 'get'; |
203
|
|
|
$name = substr($name, 0, 4); |
204
|
|
|
} elseif (strpos($name, 'get') === 0) { |
205
|
|
|
$getSet = 'get'; |
206
|
|
|
$name = substr($name, 0, 3); |
207
|
|
|
} |
208
|
|
|
return self::$getSet($name, array_pop($arguments)); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Gets the value of an environment variable. Pass the $default for fallback value. |
213
|
|
|
* @param string $key Key to get |
214
|
|
|
* @param mixed $default Value to fallback if the key can't be found |
215
|
|
|
* @return mixed Return the environment variable value or the default value |
216
|
|
|
*/ |
217
|
|
|
public static function get(string $key, $default = null) |
218
|
|
|
{ |
219
|
|
|
self::getDotenv(); |
220
|
|
|
|
221
|
|
|
$ret = self::$vars[$key] ?? null; |
222
|
|
|
$ret ??= is_callable($default)? $default() : $default; |
223
|
|
|
|
224
|
|
|
if (is_string($ret)) { |
225
|
|
|
switch (strtolower($ret)) { |
226
|
|
|
case 'true': |
227
|
|
|
$ret = true; |
228
|
|
|
break; |
229
|
|
|
case 'false': |
230
|
|
|
$ret = false; |
231
|
|
|
break; |
232
|
|
|
case 'empty': |
233
|
|
|
$ret = ''; |
234
|
|
|
break; |
235
|
|
|
case 'null': |
236
|
|
|
$ret = null; |
237
|
|
|
break; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $ret; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Set an environment variable |
246
|
|
|
* @param string $key Key to set |
247
|
|
|
* @param mixed $value Value to set |
248
|
|
|
*/ |
249
|
|
|
public static function set(string $key, $value) |
250
|
|
|
{ |
251
|
|
|
self::$vars[$key] = $value; |
252
|
|
|
$_ENV[$key] = $value; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Return the environment variable |
257
|
|
|
* @param string $name Env key to fetch |
258
|
|
|
* @return mixed Env value |
259
|
|
|
*/ |
260
|
|
|
public function __get(string $name) |
261
|
|
|
{ |
262
|
|
|
return self::get($name); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set the environment variable |
267
|
|
|
* @param string $name Env key to set |
268
|
|
|
* @param mixed $value Value to set |
269
|
|
|
*/ |
270
|
|
|
public function __set(string $name, $value) |
271
|
|
|
{ |
272
|
|
|
self::set($name, $value); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Get or set the environment variable |
277
|
|
|
* Ex. (SET): self::SET_APPLICATION_ENV('production'); |
278
|
|
|
* Ex. (SET): Env::SET_APPLICATION_ENV('production'); |
279
|
|
|
* Ex. (GET): self::GET_APPLICATION_ENV('development'); |
280
|
|
|
* Ex. (GET): Env::GET_APPLICATION_ENV('development'); |
281
|
|
|
* @param $name string Key to get/set |
282
|
|
|
* @param $arguments array Default fallback value for get, or value to set for set |
283
|
|
|
* @return mixed Return void for set, and return the environment variable value, or default value for get |
284
|
|
|
*/ |
285
|
|
|
public static function __callStatic(string $name, array $arguments) |
286
|
|
|
{ |
287
|
|
|
return self::call($name, $arguments); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Get or set the environment variable |
292
|
|
|
* Ex. (SET): $this->SET_APPLICATION_ENV('production'); |
293
|
|
|
* Ex. (GET): $this->GET_APPLICATION_ENV('development'); |
294
|
|
|
* @param $name string Key to get/set |
295
|
|
|
* @param $arguments array Default fallback value for get, or value to set for set |
296
|
|
|
* @return mixed Return void for set, and return the environment variable value, or default value for get |
297
|
|
|
*/ |
298
|
|
|
public function __call(string $name, array $arguments) |
299
|
|
|
{ |
300
|
|
|
return self::call($name, $arguments); |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|