amend .env intitialization
This commit is contained in:
1 parent
42ef989fb7
commit
3adf8b67be
1 file changed
+14
-4
+14
-4
@@ -9,11 +9,21 @@ use Slim\Factory\AppFactory;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
// Load environment variables when a project .env file is present.
|
||||
// Docker can provide these via env_file/environment as well; in that case
|
||||
// we should not crash if the file is not mounted into the container.
|
||||
// Only load the project .env file when the runtime environment has not already
|
||||
// provided the required values. Docker commonly sets these via env_file or
|
||||
// environment entries, so we must not try to read a missing local .env file in that case.
|
||||
$dotenvPath = __DIR__ . '/../';
|
||||
if (is_file($dotenvPath . '.env')) {
|
||||
$requiredEnvKeys = ['APP_ENV', 'APP_DEBUG', 'LOG_LEVEL', 'PORT', 'RATE_LIMIT', 'CORS_ORIGIN'];
|
||||
$hasRuntimeEnvironment = false;
|
||||
|
||||
foreach ($requiredEnvKeys as $key) {
|
||||
if (array_key_exists($key, $_ENV) || getenv($key) !== false) {
|
||||
$hasRuntimeEnvironment = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$hasRuntimeEnvironment && is_file($dotenvPath . '.env')) {
|
||||
$dotenv = Dotenv\Dotenv::createImmutable($dotenvPath);
|
||||
$dotenv->safeLoad();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user