ferrero-naming-tool-nv/public-v2/env_loader.php
nickviljoen 58a1a8fdb5 Add project source files and documentation
Includes backend API, public-v2 frontend, database migrations,
Composer config, and deployment/implementation docs.
Config files with credentials are excluded via .gitignore.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-30 19:19:12 +02:00

51 lines
1.3 KiB
PHP

<?php
/**
* Simple .env file loader
* Loads environment variables from .env file if it exists
*/
function loadEnvFile($path = null) {
// Default to .env in parent directory
if ($path === null) {
$path = __DIR__ . '/../.env';
}
// Check if file exists
if (!file_exists($path)) {
return false;
}
// Read file line by line
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
// Skip comments
if (strpos(trim($line), '#') === 0) {
continue;
}
// Parse KEY=VALUE format
if (strpos($line, '=') !== false) {
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
// Remove quotes if present
if (preg_match('/^(["\'])(.*)\1$/', $value, $matches)) {
$value = $matches[2];
}
// Set environment variable if not already set
if (!getenv($key)) {
putenv("$key=$value");
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
}
}
return true;
}
// Auto-load .env file
loadEnvFile();