-
Notifications
You must be signed in to change notification settings - Fork 99
Support for nested parameters #54
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will break if you have a parameter containing an index array and you expect to replace it (a list of trusted proxy IPs for instance)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just tried with test:
- one
- two
- threeand this actually works for me using
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It works when you add more elements than previously. It fails if your new array has 2 elements only while there were 3 before (the third one will be kept) |
||
| } | ||
|
|
||
| $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('<comment>Some parameters are missing. Please provide them.</comment>'); | ||
| 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('<comment>Some parameters are missing. Please provide them.</comment>'); | ||
| } | ||
|
|
||
| $default = Inline::dump($paramValue); | ||
| $parametersPath = $this->getParametersPath($parentKeys, $paramKey); | ||
| $value = $this->io->ask( | ||
| sprintf('<question>%s</question> (<comment>%s</comment>): ', $parametersPath, $default), | ||
| $default | ||
| ); | ||
| $actualParams[$paramKey] = Inline::parse($value); | ||
| } | ||
|
|
||
| $default = Inline::dump($message); | ||
| $value = $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $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; | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| parameters: | ||
| foo: bar | ||
| bare: | ||
| foo2: existing_foo2 | ||
| foo3: existing_foo3 | ||
| foo4: existing_foo4 | ||
| foo5: | ||
| bare2: | ||
| - bloup | ||
| - glop |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # This file is auto-generated during the composer install | ||
| parameters: | ||
| foo: existing_foo | ||
| bare: | ||
| foo1: existing_foo1 | ||
| foo3: existing_foo3 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| title: Extra deep array keys and preserve existing values |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't align the
=signs. I don't follow this rule in my projects as I find it is a bad one. It forces to introduces useless git conflicts risks each time we add a longer variable nameThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.