diff --git a/Processor.php b/Processor.php index 1b960bd..7290838 100644 --- a/Processor.php +++ b/Processor.php @@ -10,10 +10,12 @@ class Processor { private $io; + private $isStarted; public function __construct(IOInterface $io) { $this->io = $io; + $this->isStarted = false; } public function processFile(array $config) @@ -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'])) { @@ -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/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/) 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