diff --git a/README.md b/README.md index e02c180..d00c3b5 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,29 @@ It is possible to use environment values in order to set default values for para In the example above, the following line used the `USER` environment variable as a default value: +### Multiple defaults for a parameter +It is also possible to have multiple default values (eg `{{QUESTION|DEFAULT1|DEFAULT2}}`. +This can be useful if you want to use environment variables as a default and, in case the environment variable is not set, fall back to a hardcoded default value. + +Hence, you can write in your dis file for instance: +```yaml +myVar: {{"My var []?|=ENV[MY_VAR]|my_default_var}} +``` + +If the MY_VAR environment variable is set to 'foobar', the question will be: +``` +My var [foobar]? +``` + +If the MY_VAR environment variable is not set, the question will be: +``` +My var [my_default_var]? +``` + +### Non-interactive mode +When composer is launched with `--no-interaction` option, the default value is applied. +This can be a good option for deployment process, in case you use environment variables in your dist file. + # LICENSE See `LICENSE.txt` file in this same package. diff --git a/src/Processor/Generic.php b/src/Processor/Generic.php index 2b0c8b3..0f9610e 100644 --- a/src/Processor/Generic.php +++ b/src/Processor/Generic.php @@ -143,15 +143,18 @@ protected function _templateReplace(array $matches) if (count($matches) > 1) { $explode = explode('|', $matches[1]); $question = $explode[0]; - $default = @$explode[1] ?: null; - // if default syntax is =ENV[VARIABLE_NAME] then extract VARIABLE_NAME from the environment as default value - if (strpos($default, '=ENV[') === 0) { - $envMatch = []; - preg_match('/^\=ENV\[(.*)\]$/', $default, $envMatch); - if (isset($envMatch[1])) { - $default = $this->_getEnvValue($envMatch[1]); + $index = 0; + do { + $default = @$explode[++$index] ?: null; + // if default syntax is =ENV[VARIABLE_NAME] then extract VARIABLE_NAME from the environment as default value + if (strpos($default, '=ENV[') === 0) { + $envMatch = []; + preg_match('/^\=ENV\[(.*)\]$/', $default, $envMatch); + if (isset($envMatch[1])) { + $default = $this->_getEnvValue($envMatch[1]); + } } - } + } while( empty($default) && $index < count($explode) ); $question = str_replace('[]', "[$default]", $question); $result = $this->getIO()->ask(rtrim($question) . ' ', $default); } diff --git a/test/Processor/GenericTest.php b/test/Processor/GenericTest.php index 285604a..7fa6243 100644 --- a/test/Processor/GenericTest.php +++ b/test/Processor/GenericTest.php @@ -40,6 +40,11 @@ class GenericTest extends TestCase const TEST_UNMAPPED_ENV_KEY = 'CUBE_TEST_UNMAPPED'; const TEST_UNMAPPED_ENV_VAL = 'UNMAPPED_VALUE'; + // used to test not existing env variables + const TEST_UNKNOWN_ENV_KEY = 'UNKNOWN_ENV_KEY'; + const TEST_UNKNOWN_ENV_KEY2 = 'UNKNOWN_ENV_KEY2'; + + protected $environmentBackup = []; @@ -295,12 +300,19 @@ public function configProvider() { public function templateReplaceProvider() { $envKey = self::TEST_MAPPED_ENV_KEY; + $unknownEnvKey = self::TEST_UNKNOWN_ENV_KEY; + $unknownEnvKey2 = self::TEST_UNKNOWN_ENV_KEY2; + return [ [['test'], null, 'test'], [['{{ }}', ' '], ' ', null], [['{{username}}', 'username'], 'username', null], [['{{username|john.doe}}', 'username|john.doe'], 'username', 'john.doe'], [["{{environment|=ENV[$envKey]}}", "environment|=ENV[$envKey]"], 'environment', self::TEST_ENV_VAL], + [["{{environment|=ENV[$envKey]|localhost}}", "environment|=ENV[$envKey]|localhost"], 'environment', self::TEST_ENV_VAL], + [["{{environment|=ENV[$unknownEnvKey]|localhost}}", "environment|=ENV[$unknownEnvKey]|localhost"], 'environment', 'localhost'], + [["{{environment|=ENV[$unknownEnvKey]|=ENV[$envKey]|localhost}}", "environment|=ENV[$unknownEnvKey]|=ENV[$envKey]|localhost"], 'environment', self::TEST_ENV_VAL], + [["{{environment|=ENV[$unknownEnvKey]|=ENV[$unknownEnvKey2]|localhost}}", "environment|=ENV[$unknownEnvKey]|=ENV[$unknownEnvKey2]|localhost"], 'environment', 'localhost'], ]; }