Skip to content

Added "automapping" feature for environment variables #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,18 @@ private function processParams(array $config, array $expectedParams, array $actu
$actualParams = array_intersect_key($actualParams, $expectedParams);
}

$envMap = empty($config['env-map']) ? array() : (array) $config['env-map'];
$envMap = array();
// Add the params coming from the environment values
if (!empty($config['env-map'])) {
// Hydrate env-map from dist file
if ('auto' === $config['env-map']) {
foreach ($expectedParams as $key => $value) {
$envMap[$key] = strtoupper($key);
}
} else {
$envMap = (array) $config['env-map'];
}
}

// Add the params coming from the environment values
$actualParams = array_replace($actualParams, $this->getEnvValues($envMap));
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,35 @@ 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 same names for parameters and environment variables

As an alternative, you can set environment variables with the same uppercased
name of your dist parameters and use ``"env-map": "auto"`` to get an auto mapping.
``my_first_param`` parameter will map ``MY_FIRST_PARAM`` environment variable.

Given this ``parameters.yml.dist`` file:

```yaml
# parameters.yml.dist

parameters:
my_first_param: "some value"
my_second_param: "another value"

```

Following settings will produce the same result as previous example:

```json
{
"extra": {
"incenteev-parameters": {
"env-map": "auto"
}
}
}
```

### 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
@@ -0,0 +1,4 @@
parameters:
ic_test_bool: false
another: test
ic_test_nested: nested
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file is auto-generated during the composer install
parameters:
ic_test_bool: true
ic_test_nested:
foo: env_foo
bar:
- env
- test
- null
another: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
title: Values provided by the environment are not asked interactively

config:
env-map: 'auto'

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

interactive: true

requested_params:
another:
default: test
input: 'null'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ic_test_foo: bar
ic_test_bool: false
another: ~
ic_test_nested:
foo: bar
bar: baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file is auto-generated during the composer install
parameters:
ic_test_foo: foobar
ic_test_bool: true
another: null
ic_test_nested:
foo: env_foo
bar:
- env
- test
- null
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: Environment variables are used over dist file defaults

config:
env-map: 'auto'

environment:
IC_TEST_BOOL: 'true'
IC_TEST_FOO: 'foobar'
IC_TEST_NESTED: '{foo: env_foo, bar: [env, test, null]}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
ic_test_new: bar
new2: new2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
old: old_value
old2: old_value2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This file is auto-generated during the composer install
parameters:
ic_test_new: new_env_value
new2: old_value2
10 changes: 10 additions & 0 deletions Tests/fixtures/testcases/renamed_and_environment_auto/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
title: Environment variables win over renamed keys

config:
rename-map:
ic_test_new: old
new2: old2
env-map: 'auto'

environment:
IC_TEST_NEW: 'new_env_value'