Skip to content

Instantly share code, notes, and snippets.

@stovberpv
Created December 9, 2021 21:05
Show Gist options
  • Save stovberpv/fbc9104f9e62ea8a0de0464e4e4d9f88 to your computer and use it in GitHub Desktop.
Save stovberpv/fbc9104f9e62ea8a0de0464e4e4d9f88 to your computer and use it in GitHub Desktop.
Lumen env bootstrapping
<?php
/*
|--------------------------------------------------------------------------
| Detect The Application Environment
|--------------------------------------------------------------------------
|
| Loads environment variables from the first available `env` file in accordance
| with the current `APP_ENV` mode specified in the environment variables before
| the server starts.
|
*/
$appEnv = getenv('APP_ENV') ?: '';
$envFiles = [];
if ($appEnv) {
array_push($envFiles, ".env.{$appEnv}.local");
array_push($envFiles, ".env.{$appEnv}");
array_push($envFiles, ".env");
} else {
array_push($envFiles, ".env.local");
array_push($envFiles, ".env");
}
foreach ($envFiles as $envFile) {
$envPath = dirname(__DIR__) . "/{$envFile}";
if (file_exists($envPath)) {
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__),
$envFile
))->bootstrap();
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment