Skip to content

Commit 02d0aba

Browse files
committed
implement bootstrap() function
1 parent f59a038 commit 02d0aba

File tree

11 files changed

+107
-0
lines changed

11 files changed

+107
-0
lines changed

src/Nuxed/Environment/bootstrap.hack

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
namespace Nuxed\Environment;
2+
3+
use namespace Nuxed\Filesystem;
4+
5+
/**
6+
* Bootstrap your environment.
7+
*
8+
* Loads a .env file and the corresponding .env.local, .env.$mode and .env.$mode.local files if they exist.
9+
*
10+
* .env.local is always ignored in test env because tests should produce the same results for everyone.
11+
* .env.dist is loaded when it exists and .env is not found.
12+
*
13+
* @param string $path A file to load
14+
* @param Mode $defaultMode The app mode to use when none is defined
15+
*/
16+
async function bootstrap(
17+
string $path,
18+
Mode $defaultMode = Mode::Development,
19+
): Awaitable<void> {
20+
$path = Filesystem\Path::create($path);
21+
$dist = Filesystem\Path::create($path->toString().'.dist');
22+
if ($path->exists() || !$dist->exists()) {
23+
await load($path->toString());
24+
} else {
25+
await load($dist->toString());
26+
}
27+
28+
$mode = mode();
29+
if (null === $mode) {
30+
$mode = $defaultMode;
31+
put('APP_MODE', $mode);
32+
}
33+
34+
$local = Filesystem\Path::create($path->toString().'.local');
35+
if ($mode !== Mode::Test && $local->exists()) {
36+
await load($local->toString());
37+
}
38+
39+
if (Mode::Local === $mode) {
40+
return;
41+
}
42+
43+
$modeSpecific = Filesystem\Path::create($path->toString().'.'.$mode);
44+
if ($modeSpecific->exists()) {
45+
await load($modeSpecific->toString());
46+
}
47+
48+
if (Mode::Test === $mode) {
49+
return;
50+
}
51+
52+
$localModeSpecifc = Filesystem\Path::create(
53+
$modeSpecific->toString().'.local',
54+
);
55+
if ($localModeSpecifc->exists()) {
56+
await load($localModeSpecifc->toString());
57+
}
58+
}

tests/Nuxed/Environment/EnvironmentTest.hack

+37
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,41 @@ class EnvironmentTest extends HackTest\HackTest {
167167
expect(Environment\mode())
168168
->toBeSame(null);
169169
}
170+
171+
public async function testBootstrap(): Awaitable<void> {
172+
await Environment\bootstrap(__DIR__.'/env/dist/.env');
173+
expect(Environment\contains('NE_DIST_LOADED'))
174+
->toBeTrue();
175+
176+
Environment\forget('APP_MODE');
177+
await Environment\bootstrap(__DIR__.'/env/dev/.env');
178+
expect(Environment\contains('NE_DEV_LOADED'))
179+
->toBeTrue();
180+
expect(Environment\contains('NE_DEV_MODE_LOADED'))
181+
->toBeTrue();
182+
expect(Environment\mode())
183+
->toBeSame(Environment\Mode::Development);
184+
185+
Environment\forget('APP_MODE');
186+
await Environment\bootstrap(__DIR__.'/env/prod/.env');
187+
expect(Environment\contains('NE_PROD_LOADED'))
188+
->toBeTrue();
189+
expect(Environment\contains('NE_PROD_MODE_LOADED'))
190+
->toBeTrue();
191+
expect(Environment\contains('NE_PROD_MODE_LOCAL_LOADED'))
192+
->toBeTrue();
193+
expect(Environment\mode())
194+
->toBeSame(Environment\Mode::Production);
195+
196+
Environment\forget('APP_MODE');
197+
await Environment\bootstrap(__DIR__.'/env/test/.env');
198+
expect(Environment\contains('NE_TEST_LOADED'))
199+
->toBeTrue();
200+
expect(Environment\contains('NE_TEST_MODE_LOADED'))
201+
->toBeTrue();
202+
expect(Environment\contains('NE_TEST_MODE_LOCAL_LOADED'))
203+
->toBeFalse();
204+
expect(Environment\mode())
205+
->toBeSame(Environment\Mode::Test);
206+
}
170207
}

tests/Nuxed/Environment/env/dev/.env

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NE_DEV_LOADED=1
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NE_DEV_MODE_LOADED=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NE_DIST_LOADED=1

tests/Nuxed/Environment/env/prod/.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_MODE=prod
2+
NE_PROD_LOADED=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NE_PROD_MODE_LOADED=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NE_PROD_MODE_LOCAL_LOADED=1

tests/Nuxed/Environment/env/test/.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
APP_MODE=test
2+
NE_TEST_LOADED=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
NE_TEST_MODE_LOADED=1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NE_TEST_MODE_LOCAL_LOADED=0

0 commit comments

Comments
 (0)