From fce990f3b582f6d2101b98e3e0d41e25e2f6b809 Mon Sep 17 00:00:00 2001 From: Michael Schiemer Date: Sun, 2 Nov 2025 21:18:48 +0100 Subject: [PATCH] fix: Make all() method dynamically check system environment variables - Update all() to merge internal variables with system environment variables - Ensures variables set after initialization (e.g., by PHP-FPM) are included - Internal variables take precedence over system variables - Fixes issue where RAPIDMAIL_USERNAME and RAPIDMAIL_PASSWORD were missing in all() output --- src/Framework/Config/Environment.php | 56 +++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Framework/Config/Environment.php b/src/Framework/Config/Environment.php index bcdbb543..8bd8acce 100644 --- a/src/Framework/Config/Environment.php +++ b/src/Framework/Config/Environment.php @@ -150,7 +150,61 @@ final readonly class Environment */ public function all(): array { - return $this->variables; + // Merge internal variables with system environment variables + // This ensures all available environment variables are returned, + // including those that became available after Environment initialization + // (e.g., set by PHP-FPM/FastCGI during request processing) + $systemVariables = $this->getSystemEnvironment(); + + // Merge: internal variables take precedence over system variables + // This ensures variables loaded from .env files or set during initialization + // take precedence over system environment variables + return array_merge($systemVariables, $this->variables); + } + + /** + * Get all system environment variables dynamically + * + * @return array + */ + private function getSystemEnvironment(): array + { + $variables = []; + + // Load from $_ENV (contains dynamically set vars in PHP-FPM) + foreach ($_ENV as $key => $value) { + if (is_string($key) && is_string($value) && !str_starts_with($key, 'HTTP_')) { + $variables[$key] = $value; + } + } + + // Load from $_SERVER (may contain additional vars from web server) + foreach ($_SERVER as $key => $value) { + if (!isset($variables[$key]) && + is_string($key) && + is_string($value) && + !str_starts_with($key, 'HTTP_') && + !in_array($key, ['GATEWAY_INTERFACE', 'SERVER_SOFTWARE', 'SERVER_NAME', 'SERVER_ADDR', + 'SERVER_PORT', 'REQUEST_URI', 'REQUEST_METHOD', 'QUERY_STRING', + 'CONTENT_TYPE', 'CONTENT_LENGTH', 'SCRIPT_NAME', 'SCRIPT_FILENAME', + 'PATH_INFO', 'FCGI_ROLE', 'REDIRECT_STATUS', 'REQUEST_TIME', + 'REQUEST_TIME_FLOAT', 'DOCUMENT_ROOT', 'DOCUMENT_URI', + 'REMOTE_ADDR', 'REMOTE_PORT', 'REMOTE_USER'], true)) { + $variables[$key] = $value; + } + } + + // Load from getenv() as fallback + $allEnvVars = getenv(); + if ($allEnvVars !== false) { + foreach ($allEnvVars as $key => $value) { + if (!isset($variables[$key])) { + $variables[$key] = $value; + } + } + } + + return $variables; } /**