38929-vm/includes/dotenv.php
2026-03-03 17:37:36 +00:00

39 lines
1.2 KiB
PHP

<?php
/**
* Global .env loader for the Flatlogic LAMP VM.
* Loads environment variables from the parent directory's .env file.
*/
function load_dotenv(): void {
static $loaded = false;
if ($loaded) return;
$envPath = realpath(__DIR__ . '/../../.env'); // executor/.env
if ($envPath && is_readable($envPath)) {
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines !== false) {
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') continue;
if (!str_contains($line, '=')) continue;
[$k, $v] = array_map('trim', explode('=', $line, 2));
if ($k === '') continue;
// Strip potential surrounding quotes (single or double)
$v = trim($v, "' ");
// Set env if not already set or if empty
if (getenv($k) === false || getenv($k) === '') {
putenv("{$k}={$v}");
$_ENV[$k] = $v;
$_SERVER[$k] = $v;
}
}
}
$loaded = true;
}
}
// Auto-load on include
load_dotenv();