fix: DesignArea mit restore() fürs Zurückschreiben und differences() für bereinigte Werte
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
60c31f9697
commit
fc8ffeba9e
@@ -9,7 +9,8 @@ defined('ABSPATH') || exit;
|
||||
/**
|
||||
* One area of the design system for DesignChange (spec §2, §4): colors, variables, presets or theme options – its
|
||||
* raw inventory as Divi stores it (also the snapshot), its design_rev, how it is written through Divi and how a
|
||||
* read-back is judged. write() throws on failure; it is also how a snapshot is written back ($mayRemove = true).
|
||||
* read-back is judged. write() applies a planned change, restore() writes a snapshot back (full replace); both throw
|
||||
* on failure. verify() judges the structure (entries, defaults), differences() the values Divi changed.
|
||||
*/
|
||||
interface DesignArea
|
||||
{
|
||||
@@ -32,10 +33,28 @@ interface DesignArea
|
||||
public function write(array $raw, bool $mayRemove): array;
|
||||
|
||||
/**
|
||||
* Whether Divi stored what was written – sanitizing may change values, never which entries exist.
|
||||
* Writes $raw back as the whole inventory (full replace: entries missing in $raw are removed).
|
||||
*
|
||||
* @param array<array-key, mixed> $raw A snapshot.
|
||||
* @return array<array-key, mixed> The inventory as read back after writing.
|
||||
*/
|
||||
public function restore(array $raw): array;
|
||||
|
||||
/**
|
||||
* Whether Divi stored the entries and defaults that were written – sanitizing may change values, never which
|
||||
* entries exist.
|
||||
*
|
||||
* @param array<array-key, mixed> $expected
|
||||
* @param array<array-key, mixed> $actual
|
||||
*/
|
||||
public function verify(array $expected, array $actual): bool;
|
||||
|
||||
/**
|
||||
* German warnings for entries whose values Divi changed while saving (e.g. sanitized settings).
|
||||
*
|
||||
* @param array<array-key, mixed> $expected
|
||||
* @param array<array-key, mixed> $actual
|
||||
* @return list<string>
|
||||
*/
|
||||
public function differences(array $expected, array $actual): array;
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ final class DesignChange
|
||||
return ToolResult::ok($planned->summary)
|
||||
->snapshotId($snapshotId)
|
||||
->logId($logId)
|
||||
->warnings($planned->warnings)
|
||||
->warnings(array_merge($planned->warnings, $area->differences($planned->raw, $saved)))
|
||||
->with('diff', $planned->diff)
|
||||
->with('design_rev', $area->rev($saved))
|
||||
->toArray();
|
||||
@@ -203,7 +203,7 @@ final class DesignChange
|
||||
Throwable $cause
|
||||
): never {
|
||||
$reason = sprintf('Die %s konnten nicht gespeichert werden.', $area->label());
|
||||
$failures = Rollback::attempt(static fn () => $area->write($current, true));
|
||||
$failures = Rollback::attempt(static fn () => $area->restore($current));
|
||||
if ($failures === []) {
|
||||
throw WriteFailed::rolledBack(
|
||||
$reason,
|
||||
|
||||
@@ -9,7 +9,7 @@ defined('ABSPATH') || exit;
|
||||
/**
|
||||
* The area "presets" (spec §6): the inventory in Divi's stored format (PresetBook), written as REST format through
|
||||
* DesignSystemWriter::savePresets(). Divi's prepare_data() may sanitize values; verify() only insists on the same
|
||||
* presets and defaults (PresetDiff without "changed").
|
||||
* presets and defaults (PresetDiff without "changed"), differences() names the presets whose values changed.
|
||||
*/
|
||||
final class PresetArea implements DesignArea
|
||||
{
|
||||
@@ -43,6 +43,11 @@ final class PresetArea implements DesignArea
|
||||
->data();
|
||||
}
|
||||
|
||||
public function restore(array $raw): array
|
||||
{
|
||||
return $this->write($raw, true);
|
||||
}
|
||||
|
||||
public function verify(array $expected, array $actual): bool
|
||||
{
|
||||
$structural = array_filter(
|
||||
@@ -52,4 +57,19 @@ final class PresetArea implements DesignArea
|
||||
|
||||
return $structural === [];
|
||||
}
|
||||
|
||||
public function differences(array $expected, array $actual): array
|
||||
{
|
||||
$warnings = [];
|
||||
foreach (PresetDiff::between(PresetBook::fromDivi($expected), PresetBook::fromDivi($actual)) as $row) {
|
||||
if ($row['action'] === 'changed') {
|
||||
$warnings[] = sprintf(
|
||||
'Divi hat die Einstellungen von Preset „%s“ beim Speichern bereinigt.',
|
||||
$row['name']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $warnings;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user