@@ -32,64 +32,78 @@ class Dotenv
32
32
*/
33
33
protected $ filePaths ;
34
34
35
+ /**
36
+ * Should file loading short circuit?
37
+ *
38
+ * @var bool
39
+ */
40
+ protected $ shortCircuit ;
41
+
35
42
/**
36
43
* Create a new dotenv instance.
37
44
*
38
45
* @param \Dotenv\Loader\LoaderInterface $loader
39
46
* @param \Dotenv\Repository\RepositoryInterface $repository
40
47
* @param string[] $filePaths
48
+ * @param bool $shortCircuit
41
49
*
42
50
* @return void
43
51
*/
44
- public function __construct (LoaderInterface $ loader , RepositoryInterface $ repository , array $ filePaths )
52
+ public function __construct (LoaderInterface $ loader , RepositoryInterface $ repository , array $ filePaths, $ shortCircuit = true )
45
53
{
46
54
$ this ->loader = $ loader ;
47
55
$ this ->repository = $ repository ;
48
56
$ this ->filePaths = $ filePaths ;
57
+ $ this ->shortCircuit = $ shortCircuit ;
49
58
}
50
59
51
60
/**
52
61
* Create a new dotenv instance.
53
62
*
54
63
* @param \Dotenv\Repository\RepositoryInterface $repository
55
64
* @param string|string[] $paths
56
- * @param string|null $file
65
+ * @param string|string[]|null $names
66
+ * @param bool $shortCircuit
57
67
*
58
68
* @return \Dotenv\Dotenv
59
69
*/
60
- public static function create (RepositoryInterface $ repository , $ paths , $ file = null )
70
+ public static function create (RepositoryInterface $ repository , $ paths , $ names = null , $ shortCircuit = true )
61
71
{
62
- return new self (new Loader (), $ repository , self ::getFilePaths ((array ) $ paths , $ file ?: '.env ' ));
72
+ $ files = self ::getFilePaths ((array ) $ paths , (array ) ($ names ?: '.env ' ));
73
+
74
+ return new self (new Loader (), $ repository , $ files , $ shortCircuit );
63
75
}
64
76
65
77
/**
66
78
* Create a new mutable dotenv instance with default repository.
67
79
*
68
- * @param string|string[] $paths
69
- * @param string|null $file
80
+ * @param string|string[] $paths
81
+ * @param string|string[]|null $names
82
+ * @param bool $shortCircuit
70
83
*
71
84
* @return \Dotenv\Dotenv
72
85
*/
73
- public static function createMutable ($ paths , $ file = null )
86
+ public static function createMutable ($ paths , $ names = null , $ shortCircuit = true )
74
87
{
75
88
$ repository = RepositoryBuilder::create ()->make ();
76
89
77
- return self ::create ($ repository , $ paths , $ file );
90
+ return self ::create ($ repository , $ paths , $ names , $ shortCircuit );
78
91
}
79
92
80
93
/**
81
94
* Create a new immutable dotenv instance with default repository.
82
95
*
83
- * @param string|string[] $paths
84
- * @param string|null $file
96
+ * @param string|string[] $paths
97
+ * @param string|string[]|null $names
98
+ * @param bool $shortCircuit
85
99
*
86
100
* @return \Dotenv\Dotenv
87
101
*/
88
- public static function createImmutable ($ paths , $ file = null )
102
+ public static function createImmutable ($ paths , $ names = null , $ shortCircuit = true )
89
103
{
90
104
$ repository = RepositoryBuilder::create ()->immutable ()->make ();
91
105
92
- return self ::create ($ repository , $ paths , $ file );
106
+ return self ::create ($ repository , $ paths , $ names , $ shortCircuit );
93
107
}
94
108
95
109
/**
@@ -101,7 +115,7 @@ public static function createImmutable($paths, $file = null)
101
115
*/
102
116
public function load ()
103
117
{
104
- return $ this ->loader ->load ($ this ->repository , self ::findAndRead ($ this ->filePaths ));
118
+ return $ this ->loader ->load ($ this ->repository , self ::findAndRead ($ this ->filePaths , $ this -> shortCircuit ));
105
119
}
106
120
107
121
/**
@@ -153,38 +167,54 @@ public function ifPresent($variables)
153
167
*
154
168
* @return string[]
155
169
*/
156
- private static function getFilePaths (array $ paths , $ file )
170
+ private static function getFilePaths (array $ paths , $ names )
157
171
{
158
- return array_map (function ($ path ) use ($ file ) {
159
- return rtrim ($ path , DIRECTORY_SEPARATOR ).DIRECTORY_SEPARATOR .$ file ;
160
- }, $ paths );
172
+ $ files = [];
173
+
174
+ foreach ($ paths as $ path ) {
175
+ foreach ($ names as $ name ) {
176
+ $ files [] = rtrim ($ path , DIRECTORY_SEPARATOR ).DIRECTORY_SEPARATOR .$ name ;
177
+ }
178
+ }
179
+
180
+ return $ files ;
161
181
}
162
182
163
183
/**
164
184
* Attempt to read the files in order.
165
185
*
166
186
* @param string[] $filePaths
187
+ * @param bool $shortCircuit
167
188
*
168
189
* @throws \Dotenv\Exception\InvalidPathException
169
190
*
170
- * @return string[]
191
+ * @return string
171
192
*/
172
- private static function findAndRead (array $ filePaths )
193
+ private static function findAndRead (array $ filePaths, $ shortCircuit )
173
194
{
174
195
if ($ filePaths === []) {
175
196
throw new InvalidPathException ('At least one environment file path must be provided. ' );
176
197
}
177
198
199
+ $ output = '' ;
200
+
178
201
foreach ($ filePaths as $ filePath ) {
179
- $ lines = self ::readFromFile ($ filePath );
180
- if ($ lines ->isDefined ()) {
181
- return $ lines ->get ();
202
+ $ content = self ::readFromFile ($ filePath );
203
+ if ($ content ->isDefined ()) {
204
+ $ output .= $ content ->get ()."\n" ;
205
+ if ($ shortCircuit ) {
206
+ break ;
207
+ }
182
208
}
183
209
}
184
210
185
- throw new InvalidPathException (
186
- sprintf ('Unable to read any of the environment file(s) at [%s]. ' , implode (', ' , $ filePaths ))
187
- );
211
+ if (!$ output ) {
212
+ throw new InvalidPathException (
213
+ sprintf ('Unable to read any of the environment file(s) at [%s]. ' , implode (', ' , $ filePaths ))
214
+ );
215
+ }
216
+
217
+ return $ output ;
188
218
}
189
219
190
220
/**
0 commit comments