Skip to content

Instantly share code, notes, and snippets.

@chx
Last active May 2, 2020 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chx/e690aae552d10f05e45fdd018f58e8b8 to your computer and use it in GitHub Desktop.
Save chx/e690aae552d10f05e45fdd018f58e8b8 to your computer and use it in GitHub Desktop.
Drupal config export YAML diff resolver
<?php
use Drupal\Core\Serialization\Yaml;
include 'autoload.php';
chdir('..');
$old = Yaml::decode(shell_exec('git show origin/master^:' . $argv[1]));
$new = Yaml::decode(shell_exec('git show origin/master:' . $argv[1]));
$diff = make_diff($new, $old);
$third = Yaml::decode(file_get_contents($argv[1]));
fix($third, $diff);
file_put_contents($argv[1], Yaml::encode($third));
function make_diff($arr1, $arr2) {
$keys = array_keys($arr1);
if (is_numeric(reset($keys))) {
return array_diff($arr1, $arr2);
}
$outputDiff = [];
foreach ($arr1 as $key => $value) {
if (!isset($arr2[$key])) {
$outputDiff[$key] = $value;
}
elseif (is_array($value)) {
if ($recursiveDiff = make_diff($value, $arr2[$key])) {
$outputDiff[$key] = $recursiveDiff;
}
}
elseif ($arr2[$key] !== $value) {
$outputDiff[$key] = $value;
}
}
return $outputDiff;
}
function fix(&$original, $diff) {
foreach ($diff as $key => $value) {
if (is_array($value)) {
$original += [$key => []];
$keys = array_keys($value);
if (is_numeric(reset($keys))) {
foreach ($value as $add) {
$original[$key][] = $add;
}
}
else {
fix($original[$key], $value);
}
}
else {
$original[$key] = $value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment