Skip to content

Commit

Permalink
Fix config:diff
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Dec 29, 2023
1 parent 411f184 commit abe9c8f
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Tools/ConfigDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Knuckles\Scribe\Tools;

use Illuminate\Support\Str;
use Symfony\Component\VarExporter\VarExporter;

class ConfigDiffer
{
Expand Down Expand Up @@ -59,8 +60,8 @@ protected function diffList(mixed $oldValue, array $value)
return "changed to a list";
}

$added = array_map(fn ($v) => "$v", array_diff($value, $oldValue));
$removed = array_map(fn ($v) => "$v", array_diff($oldValue, $value));
$added = array_map(fn ($v) => "$v", $this->subtractArraysFlat($value, $oldValue));
$removed = array_map(fn ($v) => "$v", $this->subtractArraysFlat($oldValue, $value));

$diff = [];
if (!empty($added)) {
Expand All @@ -72,4 +73,27 @@ protected function diffList(mixed $oldValue, array $value)

return empty($diff) ? "" : implode(": ", $diff);
}

/**
* Basically array_diff, but handling items which may also be arrays
*/
protected function subtractArraysFlat(array $a, array $b)
{
$mapped_a = array_map(function ($item) {
if (is_array($item)) {
return VarExporter::export($item);
}

return $item;
}, $a);
$mapped_b = array_map(function ($item) {
if (is_array($item)) {
return VarExporter::export($item);
}

return $item;
}, $b);

return array_diff($mapped_a, $mapped_b);
}
}

0 comments on commit abe9c8f

Please sign in to comment.