Skip to content
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

Add possibility of merge missing nested array values under the top-level key #46

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 29 additions & 3 deletions Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,14 @@ 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;

foreach ($expectedParams as $key => $message) {
$diffParams = self::arrayDiffKeyRecursive($expectedParams, $actualParams);

foreach ($diffParams as $key => $message) {
if (array_key_exists($key, $actualParams)) {
continue;
}
Expand All @@ -159,9 +161,33 @@ private function getParams(array $expectedParams, array $actualParams)
$default = Inline::dump($message);
$value = $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $key, $default), $default);

$actualParams[$key] = Inline::parse($value);
if (array_key_exists($key, $actualParams)) {
$actualParams[$key] = array_merge($actualParams[$key], Inline::parse($value));
} else {
$actualParams[$key] = Inline::parse($value);
}
}

return $actualParams;
}

/**
* Return recursive diff based on keys
* @param array $arr1 [description]
* @param array $arr2 [description]
* @return [type] [description]
*/
private static function arrayDiffKeyRecursive(array $arr1, array $arr2)
{
$diff = array_diff_key($arr1, $arr2);
$intersect = array_intersect_key($arr1, $arr2);
foreach ($intersect as $k => $v) {
if (is_array($arr1[$k]) && is_array($arr2[$k])) {
$d = self::arrayDiffKeyRecursive($arr1[$k], $arr2[$k]);
if ($d) $diff[$k] = $d;
}
}

return $diff;
}
}
12 changes: 12 additions & 0 deletions Tests/fixtures/testcases/extra_array_keys/dist.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
parameters:
foo: bar
bare:
foo2: existing_foo2
foo3: existing_foo3
foo4: existing_foo4
foo5:
bare2:
- bloup
- glop

extra_key: a new extra key
7 changes: 7 additions & 0 deletions Tests/fixtures/testcases/extra_array_keys/existing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file is auto-generated during the composer install
parameters:
foo: existing_foo
bare:
foo1: existing_foo1
foo3: existing_foo3
another_key: foo
14 changes: 14 additions & 0 deletions Tests/fixtures/testcases/extra_array_keys/expected.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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
extra_key: 'a new extra key'
another_key: foo
1 change: 1 addition & 0 deletions Tests/fixtures/testcases/extra_array_keys/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: Extra deep array keys and preserve existing values