fix: Resolve Docker Secrets from *_FILE variables even if base variable is missing

- Update all() method to check for *_FILE variables and resolve their base variables
- Ensures APP_KEY, DB_PASSWORD, etc. are resolved even if they don't exist in the array
- Two-pass approach: first processes existing variables, second checks for *_FILE patterns
- Fixes issue where APP_KEY was empty even though APP_KEY_FILE existed
This commit is contained in:
2025-11-02 21:52:04 +01:00
parent 417b451be1
commit c530fbf8f1

View File

@@ -177,7 +177,14 @@ final readonly class Environment
// Resolve Docker Secrets for variables that are empty or not set // Resolve Docker Secrets for variables that are empty or not set
// This ensures that variables like DB_PASSWORD are resolved from their *_FILE counterparts // This ensures that variables like DB_PASSWORD are resolved from their *_FILE counterparts
$resolved = []; $resolved = [];
// First pass: Process existing variables
foreach ($all as $key => $value) { foreach ($all as $key => $value) {
// Skip *_FILE variables (we'll resolve them below)
if (str_ends_with($key, '_FILE')) {
continue;
}
// If variable is empty or not set, check for Docker Secret // If variable is empty or not set, check for Docker Secret
if (empty($value) || $value === '' || $value === null) { if (empty($value) || $value === '' || $value === null) {
$secretValue = $this->secretsResolver->resolve($key, $all); $secretValue = $this->secretsResolver->resolve($key, $all);
@@ -186,10 +193,30 @@ final readonly class Environment
continue; continue;
} }
} }
// Include non-empty values and *_FILE variables // Include non-empty values
$resolved[$key] = $value; $resolved[$key] = $value;
} }
// Second pass: Check for *_FILE variables that don't have corresponding resolved values
// This handles cases where a *_FILE exists but the variable itself is not in the array
foreach ($all as $key => $value) {
if (str_ends_with($key, '_FILE')) {
// Extract the base key name (e.g., APP_KEY from APP_KEY_FILE)
$baseKey = substr($key, 0, -5); // Remove '_FILE' suffix
// If base key is not set or empty, try to resolve it
if (!isset($resolved[$baseKey]) || empty($resolved[$baseKey])) {
$secretValue = $this->secretsResolver->resolve($baseKey, $all);
if ($secretValue !== null) {
$resolved[$baseKey] = $secretValue;
}
}
// Always include *_FILE variables in output
$resolved[$key] = $value;
}
}
if ($sorted) { if ($sorted) {
ksort($resolved); ksort($resolved);
} }
@@ -243,7 +270,7 @@ final readonly class Environment
} }
/** /**
* Factory method für .env file loading * Factory method f?r .env file loading
*/ */
public static function fromFile(FilePath|string $envPath): self public static function fromFile(FilePath|string $envPath): self
{ {
@@ -263,7 +290,7 @@ final readonly class Environment
} }
/** /**
* Für Tests * F?r Tests
*/ */
public function withVariable(string $key, mixed $value): self public function withVariable(string $key, mixed $value): self
{ {