@@ -18,63 +18,51 @@ final class DotEnv
1818
1919 public function __construct (array $ options = [], bool $ canUseKirbyOptions = true )
2020 {
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 ) {
3528 return $ dotenv ;
3629 },
3730 ];
3831 $ this ->options = array_merge ($ defaults , $ options );
3932
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+ }
4639
40+ public function option (string $ key ): mixed
41+ {
42+ return A::get ($ this ->options , $ key );
43+ }
44+
45+ private function tryMultipleReasonableDirs (): void
46+ {
4747 // try multiple reasonable dirs
4848 $ dirs = $ this ->options ['dir ' ];
4949 if (! is_array ($ dirs )) {
5050 $ dirs = [$ dirs ];
5151 }
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+ ];
5259 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- ];
6160 foreach ($ files as $ file ) {
6261 if ($ this ->loadFromDir ($ dir , $ file )) {
63- break ;
62+ break 2 ;
6463 }
6564 }
6665 }
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 );
7866 }
7967
8068 private function loadFromDir (string $ dir , string $ file ): bool
@@ -132,4 +120,14 @@ public static function getenv(string $env, mixed $default = null): mixed
132120
133121 return A::get ($ _ENV , $ env , $ default );
134122 }
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+ }
135133}
0 commit comments