From 6c36c8ad3fa6a589db3d0aabced0d139c5147852 Mon Sep 17 00:00:00 2001 From: Lucas Courot Date: Fri, 2 May 2014 19:07:10 +0200 Subject: [PATCH 1/3] Add possibility to use nested parameters --- Processor.php | 67 ++++++++++++++----- .../testcases/extra_array_keys/dist.yml | 10 +++ .../testcases/extra_array_keys/existing.yml | 6 ++ .../testcases/extra_array_keys/expected.yml | 12 ++++ .../testcases/extra_array_keys/setup.yml | 1 + 5 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 Tests/fixtures/testcases/extra_array_keys/dist.yml create mode 100644 Tests/fixtures/testcases/extra_array_keys/existing.yml create mode 100644 Tests/fixtures/testcases/extra_array_keys/expected.yml create mode 100644 Tests/fixtures/testcases/extra_array_keys/setup.yml diff --git a/Processor.php b/Processor.php index 1b960bd..b03efaf 100644 --- a/Processor.php +++ b/Processor.php @@ -10,17 +10,19 @@ class Processor { private $io; + private $isStarted; public function __construct(IOInterface $io) { - $this->io = $io; + $this->io = $io; + $this->isStarted = false; } public function processFile(array $config) { $config = $this->processConfig($config); - $realFile = $config['file']; + $realFile = $config['file']; $parameterKey = $config['parameter-key']; $exists = is_file($realFile); @@ -70,7 +72,7 @@ private function processConfig(array $config) } if (empty($config['dist-file'])) { - $config['dist-file'] = $config['file'].'.dist'; + $config['dist-file'] = $config['file'] . '.dist'; } if (!is_file($config['dist-file'])) { @@ -87,7 +89,7 @@ private function processConfig(array $config) private function processParams(array $config, array $expectedParams, array $actualParams) { // Grab values for parameters that were renamed - $renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map']; + $renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map']; $actualParams = array_replace($actualParams, $this->processRenamedValues($renameMap, $actualParams)); $keepOutdatedParams = false; @@ -141,27 +143,62 @@ private function getParams(array $expectedParams, array $actualParams) { // Simply use the expectedParams value as default for the missing params. if (!$this->io->isInteractive()) { - return array_replace($expectedParams, $actualParams); + return array_replace_recursive($expectedParams, $actualParams); } - $isStarted = false; + $actualParams = $this->provideParams($expectedParams, $actualParams); - foreach ($expectedParams as $key => $message) { - if (array_key_exists($key, $actualParams)) { + return $actualParams; + } + + /** + * @param array $expectedParams + * @param array $actualParams + * @param string $parentKeys + * + * @return array + */ + private function provideParams(array $expectedParams, array $actualParams, $parentKeys = '') + { + foreach ($expectedParams as $paramKey => $paramValue) { + if (array_key_exists($paramKey, $actualParams) && !is_array($paramValue)) { continue; } - if (!$isStarted) { - $isStarted = true; - $this->io->write('Some parameters are missing. Please provide them.'); + if (is_array($paramValue)) { + if (!array_key_exists($paramKey, $actualParams)) { + $actualParams[$paramKey] = array(); + } + + $actualParams[$paramKey] = $this->provideParams($paramValue, $actualParams[$paramKey], $this->getParametersPath($parentKeys, $paramKey)); + } else { + if (!$this->isStarted) { + $this->isStarted = true; + $this->io->write('Some parameters are missing. Please provide them.'); + } + + $default = Inline::dump($paramValue); + $parametersPath = $this->getParametersPath($parentKeys, $paramKey); + $value = $this->io->ask( + sprintf('%s (%s): ', $parametersPath, $default), + $default + ); + $actualParams[$paramKey] = Inline::parse($value); } - $default = Inline::dump($message); - $value = $this->io->ask(sprintf('%s (%s): ', $key, $default), $default); - - $actualParams[$key] = Inline::parse($value); } return $actualParams; } + + /** + * @param $parentKeys + * @param $key + * + * @return string + */ + private function getParametersPath($parentKeys, $key) + { + return $parentKeys ? $parentKeys . '.' . $key : $key; + } } diff --git a/Tests/fixtures/testcases/extra_array_keys/dist.yml b/Tests/fixtures/testcases/extra_array_keys/dist.yml new file mode 100644 index 0000000..069d020 --- /dev/null +++ b/Tests/fixtures/testcases/extra_array_keys/dist.yml @@ -0,0 +1,10 @@ +parameters: + foo: bar + bare: + foo2: existing_foo2 + foo3: existing_foo3 + foo4: existing_foo4 + foo5: + bare2: + - bloup + - glop diff --git a/Tests/fixtures/testcases/extra_array_keys/existing.yml b/Tests/fixtures/testcases/extra_array_keys/existing.yml new file mode 100644 index 0000000..6583f6f --- /dev/null +++ b/Tests/fixtures/testcases/extra_array_keys/existing.yml @@ -0,0 +1,6 @@ +# This file is auto-generated during the composer install +parameters: + foo: existing_foo + bare: + foo1: existing_foo1 + foo3: existing_foo3 diff --git a/Tests/fixtures/testcases/extra_array_keys/expected.yml b/Tests/fixtures/testcases/extra_array_keys/expected.yml new file mode 100644 index 0000000..30c3457 --- /dev/null +++ b/Tests/fixtures/testcases/extra_array_keys/expected.yml @@ -0,0 +1,12 @@ +# This file is auto-generated during the composer install +parameters: + foo: existing_foo + bare: + foo2: existing_foo2 + foo3: existing_foo3 + foo4: existing_foo4 + foo5: + bare2: + - bloup + - glop + foo1: existing_foo1 diff --git a/Tests/fixtures/testcases/extra_array_keys/setup.yml b/Tests/fixtures/testcases/extra_array_keys/setup.yml new file mode 100644 index 0000000..726a209 --- /dev/null +++ b/Tests/fixtures/testcases/extra_array_keys/setup.yml @@ -0,0 +1 @@ +title: Extra deep array keys and preserve existing values From 21b0ff1cd0551b40167020cb79c8b61b5c1a9f5b Mon Sep 17 00:00:00 2001 From: Lucas Courot Date: Fri, 2 May 2014 21:15:50 +0200 Subject: [PATCH 2/3] Update Readme to mention multi-level parameters nesting. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0f42600..9711bbd 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,8 @@ This tool allows you to manage your ignored parameters when running a composer install or update. It works when storing the parameters in a Yaml file under a single top-level key (named ``parameters`` by default). Other keys are -copied without change. +copied without change. It's obviously possible to nest multi-level parameters +in your Yaml file. [![Build Status](https://travis-ci.org/Incenteev/ParameterHandler.png)](https://travis-ci.org/Incenteev/ParameterHandler) [![Code Coverage](https://scrutinizer-ci.com/g/Incenteev/ParameterHandler/badges/coverage.png?s=ea5de28d9764fdcb6a576a41e244c0ac537b3c81)](https://scrutinizer-ci.com/g/Incenteev/ParameterHandler/) From 1a4a9afb761abce3e63bbc23eb1e0edc562376e1 Mon Sep 17 00:00:00 2001 From: Lucas Courot Date: Fri, 2 May 2014 23:32:51 +0200 Subject: [PATCH 3/3] Remove alignment of the equal signs. --- Processor.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Processor.php b/Processor.php index b03efaf..7290838 100644 --- a/Processor.php +++ b/Processor.php @@ -14,7 +14,7 @@ class Processor public function __construct(IOInterface $io) { - $this->io = $io; + $this->io = $io; $this->isStarted = false; } @@ -22,7 +22,7 @@ public function processFile(array $config) { $config = $this->processConfig($config); - $realFile = $config['file']; + $realFile = $config['file']; $parameterKey = $config['parameter-key']; $exists = is_file($realFile); @@ -89,7 +89,7 @@ private function processConfig(array $config) private function processParams(array $config, array $expectedParams, array $actualParams) { // Grab values for parameters that were renamed - $renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map']; + $renameMap = empty($config['rename-map']) ? array() : (array) $config['rename-map']; $actualParams = array_replace($actualParams, $this->processRenamedValues($renameMap, $actualParams)); $keepOutdatedParams = false; @@ -177,9 +177,9 @@ private function provideParams(array $expectedParams, array $actualParams, $pare $this->io->write('Some parameters are missing. Please provide them.'); } - $default = Inline::dump($paramValue); - $parametersPath = $this->getParametersPath($parentKeys, $paramKey); - $value = $this->io->ask( + $default = Inline::dump($paramValue); + $parametersPath = $this->getParametersPath($parentKeys, $paramKey); + $value = $this->io->ask( sprintf('%s (%s): ', $parametersPath, $default), $default );