Skip to content

Commit

Permalink
Merge pull request #1 from vincentlepot/recursive_processor
Browse files Browse the repository at this point in the history
Add the capacity to recurse among default values
  • Loading branch information
gsomoza committed Sep 18, 2015
2 parents 69672c5 + 67932b8 commit a5e59e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -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.

Expand Down
19 changes: 11 additions & 8 deletions src/Processor/Generic.php
Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions test/Processor/GenericTest.php
Expand Up @@ -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 = [];


Expand Down Expand Up @@ -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'],
];
}

Expand Down

0 comments on commit a5e59e4

Please sign in to comment.