amend .env intitialization

This commit is contained in:
Reindl David (IT-PTR-CEN2-SL10) committed 2026-10-07 19:50:04 +02:00
1 parent 42ef989fb7
commit 3adf8b67be
1 file changed
+14 -4
+14 -4
View File
@@ -9,11 +9,21 @@ use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php'; require __DIR__ . '/../vendor/autoload.php';
// Load environment variables when a project .env file is present. // Only load the project .env file when the runtime environment has not already
// Docker can provide these via env_file/environment as well; in that case // provided the required values. Docker commonly sets these via env_file or
// we should not crash if the file is not mounted into the container. // environment entries, so we must not try to read a missing local .env file in that case.
$dotenvPath = __DIR__ . '/../'; $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 = Dotenv\Dotenv::createImmutable($dotenvPath);
$dotenv->safeLoad(); $dotenv->safeLoad();
} }