41 lines
1.1 KiB
PHP

<?php
class Env {
private static $variables = [];
public static function load() {
$envFile = dirname(__DIR__) . '/.env';
if (!file_exists($envFile)) {
throw new Exception('.env file not found. Please create one based on .env.example');
}
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
// Skip comments
if (strpos(trim($line), '#') === 0) {
continue;
}
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = trim($value);
// Remove quotes if present
if (strpos($value, '"') === 0 || strpos($value, "'") === 0) {
$value = substr($value, 1, -1);
}
self::$variables[$name] = $value;
putenv("$name=$value");
}
}
public static function get($key, $default = null) {
return self::$variables[$key] ?? $default;
}
public static function all() {
return self::$variables;
}
}