diff --git a/public/index.php b/public/index.php index ccef715..a318860 100644 --- a/public/index.php +++ b/public/index.php @@ -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(); }