$k) { if ($i === count($keys) - 1) { $config[$k] = $value; } else { if (!isset($config[$k]) || !is_array($config[$k])) { $config[$k] = []; } $config = &$config[$k]; } } } /** * Load system environment variables */ private static function loadEnvironment() { // System environment variables get precedence foreach ($_ENV as $key => $value) { self::setFromEnv($key, $value); } foreach ($_SERVER as $key => $value) { if (strpos($key, 'APP_') === 0 || strpos($key, 'DB_') === 0 || strpos($key, 'STREAM_') === 0 || strpos($key, 'ADMIN_') === 0) { self::setFromEnv($key, $value); } } } /** * Load .env file */ private static function loadDotEnv() { $envFile = __DIR__ . '/../.env'; if (!file_exists($envFile)) { error_log("Warning: .env file not found. Using default configuration."); return; } $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { $line = trim($line); // Skip comments if (strpos($line, '#') === 0) { continue; } // Parse key=value pairs if (strpos($line, '=') !== false) { list($key, $value) = explode('=', $line, 2); $key = trim($key); $value = trim($value); // Remove quotes if present if ((strpos($value, '"') === 0 && strrpos($value, '"') === strlen($value) - 1) || (strpos($value, "'") === 0 && strrpos($value, "'") === strlen($value) - 1)) { $value = substr($value, 1, -1); } self::setFromEnv($key, $value); } } } /** * Set configuration value from environment variable */ private static function setFromEnv($key, $value) { // Convert environment naming to configuration naming $configKey = strtolower(str_replace('_', '.', $key)); // Type coercion for boolean values if (in_array($value, ['true', 'false'])) { $value = $value === 'true'; } // Type coercion for numeric values if (is_numeric($value) && !is_string($value)) { $value = strpos($value, '.') !== false ? (float)$value : (int)$value; } // Handle arrays (comma-separated) if (strpos($value, ',') !== false) { $value = array_map('trim', explode(',', $value)); } self::set($configKey, $value); } /** * Get all configuration as a flattened array */ public static function all() { return self::load(); } /** * Check if we are in a specific environment */ public static function isEnvironment($env) { return self::get('app.env') === $env; } /** * Check if debug mode is enabled */ public static function isDebug() { return self::get('app.debug', false); } }