From 3adf8b67be61feda45a52f413b8fde98bf1066bd Mon Sep 17 00:00:00 2001 From: "Reindl David (IT-PTR-CEN2-SL10)" Date: Wed, 7 Oct 2026 19:50:04 +0200 Subject: [PATCH] amend .env intitialization --- public/index.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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(); }