a3a3b54a85
CI / PHP Syntax Check (push) Has been cancelled
CI / JavaScript Lint (push) Has been cancelled
CI / Docker Build Test (push) Has been cancelled
CI / Validate Translation Files (push) Has been cancelled
CI / Auto-merge develop → main (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
Security Scan (Trivy) / Trivy — Docker image scan (push) Has been cancelled
Security Scan (Trivy) / Trivy — Filesystem scan (push) Has been cancelled
40 lines
1.2 KiB
PHP
40 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* EverShelf — environment variable loader (.env).
|
|
*/
|
|
function loadEnv(): array {
|
|
static $cache = null;
|
|
if ($cache !== null) {
|
|
return $cache;
|
|
}
|
|
$envFile = dirname(__DIR__, 2) . '/.env';
|
|
$cache = [];
|
|
if (file_exists($envFile)) {
|
|
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
foreach ($lines as $line) {
|
|
if (strpos($line, '#') === 0 || strpos($line, '=') === false) {
|
|
continue;
|
|
}
|
|
[$key, $val] = explode('=', $line, 2);
|
|
$cache[trim($key)] = trim($val);
|
|
}
|
|
}
|
|
return $cache;
|
|
}
|
|
function env(string $key, string $default = ''): string {
|
|
$vars = loadEnv();
|
|
if (isset($vars[$key]) && $vars[$key] !== '') {
|
|
return $vars[$key];
|
|
}
|
|
// Fallback to system/Docker environment variables (e.g. set via Portainer)
|
|
$sysVal = getenv($key);
|
|
if ($sysVal !== false && $sysVal !== '') {
|
|
return $sysVal;
|
|
}
|
|
return $default;
|
|
}
|
|
/** Push a single key into the in-memory env cache (after .env write). */
|
|
function envCacheSet(string $key, string $value): void {
|
|
loadEnv();
|
|
// Force reload on next call — callers should use loadEnv() return for batch updates
|
|
} |