diff --git a/Processor.php b/Processor.php index 1b960bd..3b8f1fc 100644 --- a/Processor.php +++ b/Processor.php @@ -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)); diff --git a/README.md b/README.md index 0f42600..953f728 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Tests/fixtures/testcases/interaction_with_environment_auto/dist.yml b/Tests/fixtures/testcases/interaction_with_environment_auto/dist.yml new file mode 100644 index 0000000..044c607 --- /dev/null +++ b/Tests/fixtures/testcases/interaction_with_environment_auto/dist.yml @@ -0,0 +1,4 @@ +parameters: + ic_test_bool: false + another: test + ic_test_nested: nested diff --git a/Tests/fixtures/testcases/interaction_with_environment_auto/expected.yml b/Tests/fixtures/testcases/interaction_with_environment_auto/expected.yml new file mode 100644 index 0000000..05135aa --- /dev/null +++ b/Tests/fixtures/testcases/interaction_with_environment_auto/expected.yml @@ -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 diff --git a/Tests/fixtures/testcases/interaction_with_environment_auto/setup.yml b/Tests/fixtures/testcases/interaction_with_environment_auto/setup.yml new file mode 100644 index 0000000..1a6eddc --- /dev/null +++ b/Tests/fixtures/testcases/interaction_with_environment_auto/setup.yml @@ -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' diff --git a/Tests/fixtures/testcases/non_existent_with_environment_auto/dist.yml b/Tests/fixtures/testcases/non_existent_with_environment_auto/dist.yml new file mode 100644 index 0000000..417b8c1 --- /dev/null +++ b/Tests/fixtures/testcases/non_existent_with_environment_auto/dist.yml @@ -0,0 +1,7 @@ +parameters: + ic_test_foo: bar + ic_test_bool: false + another: ~ + ic_test_nested: + foo: bar + bar: baz diff --git a/Tests/fixtures/testcases/non_existent_with_environment_auto/expected.yml b/Tests/fixtures/testcases/non_existent_with_environment_auto/expected.yml new file mode 100644 index 0000000..b783112 --- /dev/null +++ b/Tests/fixtures/testcases/non_existent_with_environment_auto/expected.yml @@ -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 diff --git a/Tests/fixtures/testcases/non_existent_with_environment_auto/setup.yml b/Tests/fixtures/testcases/non_existent_with_environment_auto/setup.yml new file mode 100644 index 0000000..9a54c98 --- /dev/null +++ b/Tests/fixtures/testcases/non_existent_with_environment_auto/setup.yml @@ -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]}' diff --git a/Tests/fixtures/testcases/renamed_and_environment_auto/dist.yml b/Tests/fixtures/testcases/renamed_and_environment_auto/dist.yml new file mode 100644 index 0000000..ddb4df6 --- /dev/null +++ b/Tests/fixtures/testcases/renamed_and_environment_auto/dist.yml @@ -0,0 +1,3 @@ +parameters: + ic_test_new: bar + new2: new2 diff --git a/Tests/fixtures/testcases/renamed_and_environment_auto/existing.yml b/Tests/fixtures/testcases/renamed_and_environment_auto/existing.yml new file mode 100644 index 0000000..b27e31b --- /dev/null +++ b/Tests/fixtures/testcases/renamed_and_environment_auto/existing.yml @@ -0,0 +1,4 @@ +# This file is auto-generated during the composer install +parameters: + old: old_value + old2: old_value2 diff --git a/Tests/fixtures/testcases/renamed_and_environment_auto/expected.yml b/Tests/fixtures/testcases/renamed_and_environment_auto/expected.yml new file mode 100644 index 0000000..06586a9 --- /dev/null +++ b/Tests/fixtures/testcases/renamed_and_environment_auto/expected.yml @@ -0,0 +1,4 @@ +# This file is auto-generated during the composer install +parameters: + ic_test_new: new_env_value + new2: old_value2 diff --git a/Tests/fixtures/testcases/renamed_and_environment_auto/setup.yml b/Tests/fixtures/testcases/renamed_and_environment_auto/setup.yml new file mode 100644 index 0000000..151c981 --- /dev/null +++ b/Tests/fixtures/testcases/renamed_and_environment_auto/setup.yml @@ -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'