Skip to content

Commit f95536b

Browse files
Support multiple file names and disabling short circuit file loading
1 parent 55dc901 commit f95536b

File tree

5 files changed

+77
-32
lines changed

5 files changed

+77
-32
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"extra": {
3232
"branch-alias": {
33-
"dev-master": "4.0-dev"
33+
"dev-master": "4.1-dev"
3434
}
3535
}
3636
}

src/Dotenv.php

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,64 +32,78 @@ class Dotenv
3232
*/
3333
protected $filePaths;
3434

35+
/**
36+
* Should file loading short circuit?
37+
*
38+
* @var bool
39+
*/
40+
protected $shortCircuit;
41+
3542
/**
3643
* Create a new dotenv instance.
3744
*
3845
* @param \Dotenv\Loader\LoaderInterface $loader
3946
* @param \Dotenv\Repository\RepositoryInterface $repository
4047
* @param string[] $filePaths
48+
* @param bool $shortCircuit
4149
*
4250
* @return void
4351
*/
44-
public function __construct(LoaderInterface $loader, RepositoryInterface $repository, array $filePaths)
52+
public function __construct(LoaderInterface $loader, RepositoryInterface $repository, array $filePaths, $shortCircuit = true)
4553
{
4654
$this->loader = $loader;
4755
$this->repository = $repository;
4856
$this->filePaths = $filePaths;
57+
$this->shortCircuit = $shortCircuit;
4958
}
5059

5160
/**
5261
* Create a new dotenv instance.
5362
*
5463
* @param \Dotenv\Repository\RepositoryInterface $repository
5564
* @param string|string[] $paths
56-
* @param string|null $file
65+
* @param string|string[]|null $names
66+
* @param bool $shortCircuit
5767
*
5868
* @return \Dotenv\Dotenv
5969
*/
60-
public static function create(RepositoryInterface $repository, $paths, $file = null)
70+
public static function create(RepositoryInterface $repository, $paths, $names = null, $shortCircuit = true)
6171
{
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);
6375
}
6476

6577
/**
6678
* Create a new mutable dotenv instance with default repository.
6779
*
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
7083
*
7184
* @return \Dotenv\Dotenv
7285
*/
73-
public static function createMutable($paths, $file = null)
86+
public static function createMutable($paths, $names = null, $shortCircuit = true)
7487
{
7588
$repository = RepositoryBuilder::create()->make();
7689

77-
return self::create($repository, $paths, $file);
90+
return self::create($repository, $paths, $names, $shortCircuit);
7891
}
7992

8093
/**
8194
* Create a new immutable dotenv instance with default repository.
8295
*
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
8599
*
86100
* @return \Dotenv\Dotenv
87101
*/
88-
public static function createImmutable($paths, $file = null)
102+
public static function createImmutable($paths, $names = null, $shortCircuit = true)
89103
{
90104
$repository = RepositoryBuilder::create()->immutable()->make();
91105

92-
return self::create($repository, $paths, $file);
106+
return self::create($repository, $paths, $names, $shortCircuit);
93107
}
94108

95109
/**
@@ -101,7 +115,7 @@ public static function createImmutable($paths, $file = null)
101115
*/
102116
public function load()
103117
{
104-
return $this->loader->load($this->repository, self::findAndRead($this->filePaths));
118+
return $this->loader->load($this->repository, self::findAndRead($this->filePaths, $this->shortCircuit));
105119
}
106120

107121
/**
@@ -153,38 +167,54 @@ public function ifPresent($variables)
153167
*
154168
* @return string[]
155169
*/
156-
private static function getFilePaths(array $paths, $file)
170+
private static function getFilePaths(array $paths, $names)
157171
{
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;
161181
}
162182

163183
/**
164184
* Attempt to read the files in order.
165185
*
166186
* @param string[] $filePaths
187+
* @param bool $shortCircuit
167188
*
168189
* @throws \Dotenv\Exception\InvalidPathException
169190
*
170-
* @return string[]
191+
* @return string
171192
*/
172-
private static function findAndRead(array $filePaths)
193+
private static function findAndRead(array $filePaths, $shortCircuit)
173194
{
174195
if ($filePaths === []) {
175196
throw new InvalidPathException('At least one environment file path must be provided.');
176197
}
177198

199+
$output = '';
200+
178201
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+
}
182208
}
183209
}
184210

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;
188218
}
189219

190220
/**

tests/Dotenv/DotenvTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ public function testDotenvLoadsEnvironmentVars()
7070
$this->assertEmpty(getenv('NULL'));
7171
}
7272

73+
public function testDotenvLoadsEnvironmentVarsMultipleNotShortCircuitMode()
74+
{
75+
$dotenv = Dotenv::createImmutable($this->folder, ['.env', 'example.env']);
76+
77+
$this->assertSame(
78+
['FOO' => 'bar', 'BAR' => 'baz', 'SPACED' => 'with spaces', 'NULL' => ''],
79+
$dotenv->load()
80+
);
81+
}
82+
83+
public function testDotenvLoadsEnvironmentVarsMultipleWithShortCircuitMode()
84+
{
85+
$dotenv = Dotenv::createImmutable($this->folder, ['.env', 'example.env'], false);
86+
87+
$this->assertSame(
88+
['FOO' => 'bar', 'BAR' => 'baz', 'SPACED' => 'with spaces', 'NULL' => '', 'EG' => 'example'],
89+
$dotenv->load()
90+
);
91+
}
92+
7393
public function testCommentedDotenvLoadsEnvironmentVars()
7494
{
7595
$dotenv = Dotenv::createImmutable($this->folder, 'commented.env');

tests/fixtures/.env

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/fixtures/env/example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
EG="example"

0 commit comments

Comments
 (0)