fix: Resolve Docker Secrets in all() method for empty variables

- Update all() to automatically resolve Docker Secrets (*_FILE pattern) for empty variables
- Ensures DB_PASSWORD, REDIS_PASSWORD, etc. are resolved from their *_FILE counterparts when empty
- Variables like DB_PASSWORD_FILE are still included in output, but empty values are replaced with resolved secrets
- Fixes issue where DB_PASSWORD and REDIS_PASSWORD appeared empty in logs even though *_FILE variables existed
This commit is contained in:
2025-11-02 21:36:07 +01:00
parent 0a5d0ecd0c
commit 0912df0537

View File

@@ -161,7 +161,7 @@ final readonly class Environment
/**
* @return array<string, mixed>
*/
public function all(): array
public function all(bool $sorted = false): array
{
// Merge internal variables with system environment variables
// This ensures all available environment variables are returned,
@@ -172,7 +172,29 @@ final readonly class Environment
// 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);
$all = array_merge($systemVariables, $this->variables);
// Resolve Docker Secrets for variables that are empty or not set
// This ensures that variables like DB_PASSWORD are resolved from their *_FILE counterparts
$resolved = [];
foreach ($all as $key => $value) {
// If variable is empty or not set, check for Docker Secret
if (empty($value) || $value === '' || $value === null) {
$secretValue = $this->secretsResolver->resolve($key, $all);
if ($secretValue !== null) {
$resolved[$key] = $secretValue;
continue;
}
}
// Include non-empty values and *_FILE variables
$resolved[$key] = $value;
}
if ($sorted) {
ksort($resolved);
}
return $resolved;
}
/**