23
23
*/
24
24
class Env
25
25
{
26
- /**
27
- * Represents the initialization status of the code.
28
- *
29
- * This variable is used to keep track of whether the code has been initialized
30
- * or not.
31
- *
32
- * @var bool
33
- */
34
- public static bool $ initialized = false ;
35
-
36
26
/**
37
27
* Represents the instance of the Dotenv class.
38
28
*
@@ -60,19 +50,19 @@ class Env
60
50
* This variable represents the value of the paths and is initially set to null.
61
51
* It can be assigned a different value during the runtime of the program.
62
52
*
63
- * @var string[]|null
53
+ * @var string[]|string| null
64
54
*/
65
- public static ? array $ paths = null ;
55
+ public static string | array | null $ paths = null ;
66
56
67
57
/**
68
58
* Represents the array of filenames.
69
59
*
70
60
* The $names variable is an array that holds the filenames of the ".env" file(s) to be loaded.
71
61
* This variable is used as an argument in the Dotenv class to specify the filenames to load.
72
62
*
73
- * @var string[]
63
+ * @var string[]|string|null
74
64
*/
75
- public static array $ names = [ ' .env ' ] ;
65
+ public static string | array | null $ names = null ;
76
66
77
67
/**
78
68
* Represents the type of data being declared.
@@ -107,105 +97,97 @@ class Env
107
97
*/
108
98
public static ?string $ fileEncoding = null ;
109
99
110
- /**
111
- * Constructs a new instance of the class.
112
- *
113
- * Initializes the class by invoking the initialize method.
114
- *
115
- * @return void
116
- */
117
- public function __construct ()
118
- {
119
- self ::initialize ();
120
- }
121
-
122
100
/**
123
101
* Initializes the Dotenv instance with the specified configurations
124
- * and returns the initialized instance.
102
+ * and returns the loaded instance.
125
103
*
126
- * @param array|null $paths The paths to search for dotenv files.
127
- * @param array|null $names The names of the dotenv files to load.
104
+ * @param string| array|null $paths The paths to search for dotenv files.
105
+ * @param string| array|null $names The names of the dotenv files to load.
128
106
* @param bool|null $shortCircuit Whether to stop loading dotenv files after finding the first one.
129
107
* @param string|null $fileEncoding The encoding of the dotenv files.
130
108
* @param string|null $type The type of dotenv files to load.
131
- * @return Dotenv The initialized Dotenv instance.
109
+ * @return Dotenv The loaded Dotenv instance.
132
110
*/
133
- public static function initialize (? array $ paths = null , ? array $ names = null , ?bool $ shortCircuit = null , ?string $ fileEncoding = null , ?string $ type = null ): Dotenv
111
+ public static function load ( string | array | null $ paths = null , string | array | null $ names = null , ?bool $ shortCircuit = true , ?string $ fileEncoding = null , ?string $ type = null ): Dotenv
134
112
{
135
- if (!self ::$ initialized ) {
136
- $ type ??= self ::getType ();
137
- $ paths ??= self ::getPaths ();
138
- $ names ??= self ::getNames ();
139
- $ shortCircuit ??= self ::getShortCircuit ();
140
- $ fileEncoding ??= self ::getFileEncoding ();
141
- self ::$ dotenv = Dotenv::{'create ' . $ type }($ paths , $ names , $ shortCircuit , $ fileEncoding );
142
- self ::$ vars = self ::$ dotenv ->load ();
143
- self ::$ initialized = true ;
144
- }
113
+ self ::setPaths ($ paths );
114
+ self ::setNames ($ names );
115
+ self ::setShortCircuit ($ shortCircuit );
116
+ self ::setFileEncoding ($ fileEncoding );
117
+ self ::setType ($ type );
118
+
119
+ $ type ??= self ::getType ();
120
+ $ paths ??= self ::getPaths ();
121
+ $ names ??= self ::getNames ();
122
+ $ shortCircuit ??= self ::getShortCircuit ();
123
+ $ fileEncoding ??= self ::getFileEncoding ();
124
+
125
+ self ::$ dotenv = Dotenv::{'create ' . $ type }($ paths , $ names , $ shortCircuit , $ fileEncoding );
126
+ self ::$ vars = self ::$ dotenv ->safeLoad ();
145
127
146
128
return self ::$ dotenv ;
147
129
}
148
130
149
131
/**
150
132
* Retrieves an array of paths.
151
- * If the paths array is not yet created, it will be initialized and returned.
133
+ * If the paths array is not yet created, it will be loaded and returned.
152
134
*
153
- * @return array The array of paths.
135
+ * @return string|string[]|null The array of paths.
154
136
*/
155
- public static function getPaths (): array
137
+ public static function getPaths (): string | array | null
156
138
{
157
- if (is_null (self ::$ paths )) {
158
- self ::$ paths = [];
159
- foreach (['ENV_PATH ' , 'ROOT_PATH ' , 'APP_PATH ' ] as $ constant ) {
160
- if (defined ($ constant )) {
161
- $ path = constant ($ constant );
162
- if (!is_null ($ path )) {
163
- self ::$ paths [] = $ constant === 'APP_PATH ' ? dirname ($ path ) : $ path ;
164
- break ;
165
- }
166
- }
167
- }
168
- if (empty (self ::$ paths )) {
169
- self ::$ paths [] = getcwd ();
170
- }
171
- }
172
139
return self ::$ paths ;
173
140
}
174
141
175
142
/**
176
143
* Sets the paths for the application. If no paths are provided,
177
144
* the paths will be set to null.
178
145
*
179
- * @param array |null $paths The paths to be set for the application.
180
- * If null is provided, the paths will be set to null.
181
- * Default is null.
146
+ * @param string|string[] |null $paths The paths to be set for the application.
147
+ * If null is provided, the paths will be set to null.
148
+ * Default is null.
182
149
*
183
150
* @return void
184
151
*/
185
- public static function setPaths (array $ paths = null ): void
152
+ public static function setPaths (string | array | null $ paths = null ): void
186
153
{
154
+ if (!isset ($ paths )) {
155
+ $ paths = [];
156
+ foreach (['ENV_PATH ' , 'ROOT_PATH ' , 'APP_PATH ' ] as $ constant ) {
157
+ if (defined ($ constant )) {
158
+ $ path = constant ($ constant );
159
+ if (!is_null ($ path )) {
160
+ $ paths [] = $ constant === 'APP_PATH ' ? dirname ($ path ) : $ path ;
161
+ break ;
162
+ }
163
+ }
164
+ }
165
+ if (empty ($ paths )) {
166
+ $ paths [] = getcwd ();
167
+ }
168
+ }
187
169
self ::$ paths = $ paths ;
188
170
}
189
171
190
172
/**
191
173
* Get .env file names
192
- * @return string[]
174
+ * @return string|string[]|null
193
175
*/
194
- public static function getNames (): array
176
+ public static function getNames (): string | array | null
195
177
{
196
178
return self ::$ names ;
197
179
}
198
180
199
181
/**
200
182
* Sets the names array. If the specified array is null, the existing names array will be cleared.
201
183
*
202
- * @param string[] $names The array of names. If null, the existing names array will be cleared.
184
+ * @param string|string[]|null $names The array of names. If null, the existing names array will be cleared.
203
185
*
204
186
* @return void
205
187
*/
206
- public static function setNames (array $ names ): void
188
+ public static function setNames (string | array | null $ names ): void
207
189
{
208
- self ::$ names = $ names ;
190
+ self ::$ names = $ names ?? [ ' .env ' ] ;
209
191
}
210
192
211
193
/**
@@ -229,7 +211,7 @@ public static function getType(): string
229
211
public static function setType (?string $ type = null ): void
230
212
{
231
213
$ domain = ['mutable ' , 'immutable ' , 'unsafe-mutable ' , 'unsafe-immutable ' ];
232
- self ::$ type = isset ($ type ) && ! in_array (strtolower ($ type ), $ domain , true ) ? strtolower ($ type ) : 'mutable ' ;
214
+ self ::$ type = isset ($ type ) && in_array (strtolower ($ type ), $ domain , true ) ? strtolower ($ type ) : 'mutable ' ;
233
215
}
234
216
235
217
/**
@@ -249,9 +231,9 @@ public static function getShortCircuit(): bool
249
231
* @param bool $shortCircuit The new value for the shortCircuit property.
250
232
* @return void
251
233
*/
252
- public static function setShortCircuit (bool $ shortCircuit = true ): void
234
+ public static function setShortCircuit (? bool $ shortCircuit = true ): void
253
235
{
254
- self ::$ shortCircuit = $ shortCircuit ;
236
+ self ::$ shortCircuit = $ shortCircuit ?? true ;
255
237
}
256
238
257
239
/**
@@ -278,50 +260,13 @@ public static function setFileEncoding(?string $fileEncoding = null): void
278
260
279
261
/**
280
262
* Retrieves the Dotenv instance. If the instance is not yet created,
281
- * it will be initialized and returned.
263
+ * it will be loaded and returned.
282
264
*
283
265
* @return Dotenv The Dotenv instance.
284
266
*/
285
267
public static function getDotenv (): Dotenv
286
268
{
287
- return self ::$ dotenv ?? self ::initialize ();
288
- }
289
-
290
- /**
291
- * Get or set the environment variable
292
- * Ex. (SET): $this->SET_APPLICATION_ENV('production');
293
- * Ex. (SET): self::SET_APPLICATION_ENV('production');
294
- * Ex. (SET): Env::SET_APPLICATION_ENV('production');
295
- * Ex. (GET): $this->GET_APPLICATION_ENV('development');
296
- * Ex. (GET): self::GET_APPLICATION_ENV('development');
297
- * Ex. (GET): Env::GET_APPLICATION_ENV('development');
298
- * @param string $name key to get/set
299
- * @param mixed $arguments Default fallback value for get, or value to set for set
300
- * @return mixed Return void for set, and return the environment variable value, or default value for get
301
- */
302
- public static function call (string $ name , mixed $ arguments ): mixed
303
- {
304
- $ getSet = 'set ' ;
305
-
306
- if (str_starts_with ($ name , 'SET_ ' )) {
307
- $ name = substr ($ name , 0 , 4 );
308
- }
309
-
310
- elseif (str_starts_with ($ name , 'set ' )) {
311
- $ name = substr ($ name , 0 , 3 );
312
- }
313
-
314
- elseif (str_starts_with ($ name , 'GET_ ' )) {
315
- $ getSet = 'get ' ;
316
- $ name = substr ($ name , 0 , 4 );
317
- }
318
-
319
- elseif (str_starts_with ($ name , 'get ' )) {
320
- $ getSet = 'get ' ;
321
- $ name = substr ($ name , 0 , 3 );
322
- }
323
-
324
- return self ::$ getSet ($ name , array_pop ($ arguments ));
269
+ return self ::$ dotenv ?? self ::load ();
325
270
}
326
271
327
272
/**
@@ -334,30 +279,35 @@ public static function get(string $key, mixed $default = null): mixed
334
279
{
335
280
self ::getDotenv ();
336
281
337
- $ ret = self ::$ vars [$ key ] ?? null ;
338
- $ ret ??= !is_string ($ default ) && is_callable ($ default )? $ default () : $ default ;
282
+ if (!isset (self ::$ vars [$ key ])) {
283
+ return $ default ;
284
+ }
285
+
286
+ $ value = self ::$ vars [$ key ];
287
+
288
+ if (!is_string ($ value )) {
289
+ return $ value ;
290
+ }
291
+
292
+ // Check for boolean values
293
+ if (strtolower ($ value ) === 'true ' ) {
294
+ return true ;
295
+ } elseif (strtolower ($ value ) === 'false ' ) {
296
+ return false ;
297
+ }
339
298
340
- if (is_string ($ ret )) {
341
- switch (strtolower ($ ret )) {
342
- case 'true ' :
343
- $ ret = true ;
344
- break ;
345
-
346
- case 'false ' :
347
- $ ret = false ;
348
- break ;
349
-
350
- case 'empty ' :
351
- $ ret = '' ;
352
- break ;
353
-
354
- case 'null ' :
355
- $ ret = null ;
356
- break ;
299
+ // Check for numeric values
300
+ if (is_numeric ($ value )) {
301
+ // Floats
302
+ if (str_contains ($ value , '. ' )) {
303
+ return floatval ($ value );
357
304
}
305
+
306
+ // Integers
307
+ return intval ($ value );
358
308
}
359
309
360
- return $ ret ;
310
+ return $ value ;
361
311
}
362
312
363
313
/**
@@ -368,54 +318,5 @@ public static function get(string $key, mixed $default = null): mixed
368
318
public static function set (string $ key , mixed $ value ): void
369
319
{
370
320
self ::$ vars [$ key ] = $ value ;
371
- $ _ENV [$ key ] = $ value ;
372
- }
373
-
374
- /**
375
- * Return the environment variable
376
- * @param string $name Env key to fetch
377
- * @return mixed Env value
378
- */
379
- public function __get (string $ name ): mixed
380
- {
381
- return self ::get ($ name );
382
- }
383
-
384
- /**
385
- * Set the environment variable
386
- * @param string $name Env key to set
387
- * @param mixed $value Value to set
388
- */
389
- public function __set (string $ name , mixed $ value ): void
390
- {
391
- self ::set ($ name , $ value );
392
- }
393
-
394
- /**
395
- * Get or set the environment variable
396
- * Ex. (SET): self::SET_APPLICATION_ENV('production');
397
- * Ex. (SET): Env::SET_APPLICATION_ENV('production');
398
- * Ex. (GET): self::GET_APPLICATION_ENV('development');
399
- * Ex. (GET): Env::GET_APPLICATION_ENV('development');
400
- * @param $name string Key to get/set
401
- * @param $arguments array Default fallback value for get, or value to set for set
402
- * @return mixed Return void for set, and return the environment variable value, or default value for get
403
- */
404
- public static function __callStatic (string $ name , array $ arguments ): mixed
405
- {
406
- return self ::call ($ name , $ arguments );
407
- }
408
-
409
- /**
410
- * Get or set the environment variable
411
- * Ex. (SET): $this->SET_APPLICATION_ENV('production');
412
- * Ex. (GET): $this->GET_APPLICATION_ENV('development');
413
- * @param $name string Key to get/set
414
- * @param $arguments array Default fallback value for get, or value to set for set
415
- * @return mixed Return void for set, and return the environment variable value, or default value for get
416
- */
417
- public function __call (string $ name , array $ arguments ): mixed
418
- {
419
- return self ::call ($ name , $ arguments );
420
321
}
421
322
}
0 commit comments