|
| 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 | +} |
0 commit comments