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