<?php
/**
* Binds environment variables with fallback logic for dry-run scenarios.
*
* @param array $expectedVariables An array of environment variable names and their expected values.
* @param array $dryRunValues An array of values to use for dry-run scenarios.
* @return array An array of bound environment variables.
*/
function bindEnvironmentVariables(array $expectedVariables, array $dryRunValues = []): array
{
$boundVariables = [];
foreach ($expectedVariables as $variableName => $expectedValue) {
// Check if the environment variable is set.
if (getenv($variableName) !== false) {
$boundVariables[$variableName] = getenv($variableName);
} elseif (isset($dryRunValues[$variableName])) {
// Use dry-run values if available.
$boundVariables[$variableName] = $dryRunValues[$variableName];
} else {
// Fallback: Use a default value (e.g., empty string or null).
$boundVariables[$variableName] = ''; // Or null, or some other suitable default.
}
}
return $boundVariables;
}
// Example usage:
/*
$expectedVars = [
'DATABASE_URL' => 'mysql://user:pass@host:port/db',
'DEBUG_MODE' => 'false',
'API_KEY' => 'your_api_key',
];
$dryRunVars = [
'DATABASE_URL' => 'sqlite://test.db',
'DEBUG_MODE' => 'true',
];
$boundVars = bindEnvironmentVariables($expectedVars, $dryRunVars);
print_r($boundVars);
*/
?>
Add your comment