Skip to content

Commit

Permalink
Added 'env-tag-map' to replace parameter %tags% with environment vari…
Browse files Browse the repository at this point in the history
…ables
  • Loading branch information
jaysphoto committed May 9, 2015
1 parent 8f9608d commit a895f37
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 4 deletions.
37 changes: 33 additions & 4 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,18 @@ private function processParams(array $config, array $expectedParams, array $actu
// Add the params coming from the environment values
$actualParams = array_replace($actualParams, $this->getEnvValues($envMap));

return $this->getParams($expectedParams, $actualParams);
$envValueMap = empty($config['env-tag-map']) ? array() : (array) $config['env-tag-map'];

return $this->getParams($expectedParams, $actualParams, $this->getEnvValues($envValueMap, '%'));
}

private function getEnvValues(array $envMap)
private function getEnvValues(array $envMap, $encloseChar = '')
{
$params = array();
foreach ($envMap as $param => $env) {
$value = getenv($env);
if ($value) {
$params[$param] = Inline::parse($value);
$params[$encloseChar . $param . $encloseChar] = Inline::parse($value);
}
}

Expand All @@ -137,8 +139,11 @@ private function processRenamedValues(array $renameMap, array $actualParams)
return $actualParams;
}

private function getParams(array $expectedParams, array $actualParams)
private function getParams(array $expectedParams, array $actualParams, array $envValueMap)
{
// Recursively fill in defaults with values from the environment value map
$this->replaceValues($expectedParams, null, $envValueMap);

// Simply use the expectedParams value as default for the missing params.
if (!$this->io->isInteractive()) {
return array_replace($expectedParams, $actualParams);
Expand All @@ -164,4 +169,28 @@ private function getParams(array $expectedParams, array $actualParams)

return $actualParams;
}

/**
* Translate subject with the string values from the replace values map
* Iterates over all values is when subject is an Array
*
* @param mixed $subject
* @param String $key dummy param for array_walk callback compatibility
* @param Array $replaceValuesMap
* @return mixed $subject
*/
private function replaceValues(&$subject, $key, $replaceValuesMap) {
switch (gettype($subject)) {
case 'object':
case 'array':
// Continue recursing through arrays/objects
array_walk($subject, array($this, __FUNCTION__), $replaceValuesMap);
break;

case 'string':
// Replace values in subject
$subject = strtr($subject, $replaceValuesMap);
break;
}
}
}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ As environment variables can only be strings, they are also parsed as inline
Yaml values to allows specifying ``null``, ``false``, ``true`` or numbers
easily.

### Using environment variables to replace tags

Environment variables can also be used to replace %tags% in your parameter
files. By providing a map between environment variables and the tags, the parameters
will be searched for occurrences of these tags.

```json
{
"extra": {
"incenteev-parameters": {
"env-tag-map": {
"foo2": "FOO2",
"foo3": "FOO3"
}
}
}
}
```

If an environment variable is set, its value will always replace any %foo2% and %foo3%
tags in the parameters file. The parameters are scanned recursively and tags can be
part of a string and/or combined with other tags.

### Renaming parameters

If you are renaming a parameter, the new key will be set according to the usual
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ parameters:
boolean: false
another: test
nested: nested
tags:
foo1: bar1
foo2: %foo2%
foo3:
foo4: %foo2%
foo5: '%foo2% - %foo4%'
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ parameters:
- test
- null
another: null
tags:
foo1: bar1
foo2: bar2
foo3:
foo4: bar2
foo5: 'bar2 - bar3'
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ config:
env-map:
boolean: IC_TEST_BOOL
nested: IC_TEST_NESTED
env-tag-map:
foo2: IC_TEST_FOO2
foo3: IC_TEST_FOO3
foo4: IC_TEST_FOO4_NOT_SET

environment:
IC_TEST_BOOL: 'true'
IC_TEST_NESTED: '{foo: env_foo, bar: [env, test, null]}'
IC_TEST_FOO2: bar2
IC_TEST_FOO3: bar3

interactive: true

requested_params:
another:
default: test
input: 'null'
tags:
default: "{ foo1: bar1, foo2: bar2, foo3: { foo4: bar2, foo5: 'bar2 - %foo4%' } }"
input: "{ foo1: bar1, foo2: bar2, foo3: { foo4: bar2, foo5: 'bar2 - bar3' } }"

0 comments on commit a895f37

Please sign in to comment.