1. <?php
  2. /**
  3. * Binds environment variables with fallback logic for dry-run scenarios.
  4. *
  5. * @param array $expectedVariables An array of environment variable names and their expected values.
  6. * @param array $dryRunValues An array of values to use for dry-run scenarios.
  7. * @return array An array of bound environment variables.
  8. */
  9. function bindEnvironmentVariables(array $expectedVariables, array $dryRunValues = []): array
  10. {
  11. $boundVariables = [];
  12. foreach ($expectedVariables as $variableName => $expectedValue) {
  13. // Check if the environment variable is set.
  14. if (getenv($variableName) !== false) {
  15. $boundVariables[$variableName] = getenv($variableName);
  16. } elseif (isset($dryRunValues[$variableName])) {
  17. // Use dry-run values if available.
  18. $boundVariables[$variableName] = $dryRunValues[$variableName];
  19. } else {
  20. // Fallback: Use a default value (e.g., empty string or null).
  21. $boundVariables[$variableName] = ''; // Or null, or some other suitable default.
  22. }
  23. }
  24. return $boundVariables;
  25. }
  26. // Example usage:
  27. /*
  28. $expectedVars = [
  29. 'DATABASE_URL' => 'mysql://user:pass@host:port/db',
  30. 'DEBUG_MODE' => 'false',
  31. 'API_KEY' => 'your_api_key',
  32. ];
  33. $dryRunVars = [
  34. 'DATABASE_URL' => 'sqlite://test.db',
  35. 'DEBUG_MODE' => 'true',
  36. ];
  37. $boundVars = bindEnvironmentVariables($expectedVars, $dryRunVars);
  38. print_r($boundVars);
  39. */
  40. ?>

Add your comment