@@ -18,63 +18,51 @@ final class DotEnv
18
18
19
19
public function __construct (array $ options = [], bool $ canUseKirbyOptions = true )
20
20
{
21
- $ defaults = $ canUseKirbyOptions ? [
22
- 'debug ' => option ('debug ' ),
23
- 'dir ' => option ('bnomei.dotenv.dir ' ),
24
- 'file ' => option ('bnomei.dotenv.file ' ),
25
- 'environment ' => option ('bnomei.dotenv.environment ' ),
26
- 'required ' => option ('bnomei.dotenv.required ' ),
27
- 'setup ' => option ('bnomei.dotenv.setup ' ),
28
- ] : [
29
- 'debug ' => false ,
30
- 'dir ' => [],
31
- 'file ' => '.env ' ,
32
- 'environment ' => null ,
33
- 'required ' => [],
34
- 'setup ' => function ($ dotenv ) {
21
+ $ defaults = [
22
+ 'debug ' => $ canUseKirbyOptions ? option ('debug ' ) : false ,
23
+ 'dir ' => $ canUseKirbyOptions ? option ('bnomei.dotenv.dir ' ) : [],
24
+ 'file ' => $ canUseKirbyOptions ? option ('bnomei.dotenv.file ' ) : '.env ' ,
25
+ 'environment ' => $ canUseKirbyOptions ? option ('bnomei.dotenv.environment ' ) : null ,
26
+ 'required ' => $ canUseKirbyOptions ? option ('bnomei.dotenv.required ' ) : [],
27
+ 'setup ' => $ canUseKirbyOptions ? option ('bnomei.dotenv.setup ' ) : function ($ dotenv ) {
35
28
return $ dotenv ;
36
29
},
37
30
];
38
31
$ this ->options = array_merge ($ defaults , $ options );
39
32
40
- // allow callback for a few options
41
- foreach ( $ this ->options as $ key => $ value ) {
42
- if ( $ value instanceof Closure && in_array ( $ key , [ ' dir ' , ' file ' , ' environment ' , ' required ' ])) {
43
- $ this -> options [ $ key ] = $ value ();
44
- }
45
- }
33
+ $ this -> allowCallbackForAFewOptions ();
34
+ $ this ->tryMultipleReasonableDirs ();
35
+ $ this -> addRequired ( $ this -> options [ ' required ' ]);
36
+ // allow additional last step setup
37
+ $ this -> dotenv = $ this -> options [ ' setup ' ]( $ this -> dotenv );
38
+ }
46
39
40
+ public function option (string $ key ): mixed
41
+ {
42
+ return A::get ($ this ->options , $ key );
43
+ }
44
+
45
+ private function tryMultipleReasonableDirs (): void
46
+ {
47
47
// try multiple reasonable dirs
48
48
$ dirs = $ this ->options ['dir ' ];
49
49
if (! is_array ($ dirs )) {
50
50
$ dirs = [$ dirs ];
51
51
}
52
+ $ dirs = array_filter ($ dirs , function ($ dir ) {
53
+ return ! empty ($ dir );
54
+ });
55
+ $ files = [
56
+ $ this ->options ['file ' ].'. ' .$ this ->options ['environment ' ],
57
+ $ this ->options ['file ' ],
58
+ ];
52
59
foreach ($ dirs as $ dir ) {
53
- if (empty ($ dir )) {
54
- continue ;
55
- }
56
- // more specific file first as it will break on first success
57
- $ files = [
58
- $ this ->options ['file ' ].'. ' .$ this ->options ['environment ' ],
59
- $ this ->options ['file ' ],
60
- ];
61
60
foreach ($ files as $ file ) {
62
61
if ($ this ->loadFromDir ($ dir , $ file )) {
63
- break ;
62
+ break 2 ;
64
63
}
65
64
}
66
65
}
67
-
68
- // add rules for required env vars
69
- $ this ->addRequired ($ this ->options ['required ' ]);
70
-
71
- // allow additional last step setup
72
- $ this ->dotenv = $ this ->options ['setup ' ]($ this ->dotenv );
73
- }
74
-
75
- public function option (string $ key ): mixed
76
- {
77
- return A::get ($ this ->options , $ key );
78
66
}
79
67
80
68
private function loadFromDir (string $ dir , string $ file ): bool
@@ -132,4 +120,14 @@ public static function getenv(string $env, mixed $default = null): mixed
132
120
133
121
return A::get ($ _ENV , $ env , $ default );
134
122
}
123
+
124
+ public function allowCallbackForAFewOptions (): void
125
+ {
126
+ // allow callback for a few options
127
+ foreach ($ this ->options as $ key => $ value ) {
128
+ if ($ value instanceof Closure && in_array ($ key , ['dir ' , 'file ' , 'environment ' , 'required ' ])) {
129
+ $ this ->options [$ key ] = $ value ();
130
+ }
131
+ }
132
+ }
135
133
}
0 commit comments