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
This commit is contained in:
2025-11-02 21:18:48 +01:00
parent d2ee59bd65
commit fce990f3b5

View File

@@ -150,7 +150,61 @@ final readonly class Environment
*/ */
public function all(): array 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<string, mixed>
*/
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;
} }
/** /**