You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.9 KiB
PHTML
57 lines
1.9 KiB
PHTML
|
1 week ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Services\UserCopy;
|
||
|
|
|
||
|
|
/** Ensures the authoring ledger matches the source-owned Manifest contract. */
|
||
|
|
final class UserCopyManifestProjectionGuard
|
||
|
|
{
|
||
|
|
/** @var array<string,mixed> */
|
||
|
|
private readonly array $manifest;
|
||
|
|
|
||
|
|
private readonly UserCopySourceCatalog $source;
|
||
|
|
|
||
|
|
public function __construct(array $manifest)
|
||
|
|
{
|
||
|
|
$this->manifest = UserCopyManifest::validate($manifest);
|
||
|
|
$this->source = new UserCopySourceCatalog($this->manifest);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** @param array<string,object> $databaseEntries */
|
||
|
|
public function assertReady(array $databaseEntries): void
|
||
|
|
{
|
||
|
|
$sourceEntries = $this->source->entries();
|
||
|
|
if (array_keys($databaseEntries) !== array_keys($sourceEntries)) {
|
||
|
|
throw new UserCopyAdminException(
|
||
|
|
'Manifest 尚未同步,不能发布。',
|
||
|
|
409
|
||
|
|
);
|
||
|
|
}
|
||
|
|
$manifestByKey = [];
|
||
|
|
foreach ($this->manifest['entries'] as $entry) {
|
||
|
|
$manifestByKey[$entry['key']] = $entry;
|
||
|
|
}
|
||
|
|
foreach ($sourceEntries as $copyKey => $sourceEntry) {
|
||
|
|
$databaseEntry = $databaseEntries[$copyKey];
|
||
|
|
$placeholderHash = hash(
|
||
|
|
'sha256',
|
||
|
|
UserCopySourceCatalog::canonicalJson(
|
||
|
|
$manifestByKey[$copyKey]['parameters']
|
||
|
|
)
|
||
|
|
);
|
||
|
|
if (!hash_equals(
|
||
|
|
(string) $databaseEntry->source_hash_zh,
|
||
|
|
hash('sha256', $sourceEntry['source'])
|
||
|
|
) || !hash_equals(
|
||
|
|
(string) $databaseEntry->placeholder_hash,
|
||
|
|
$placeholderHash
|
||
|
|
) || (int) $databaseEntry->manifest_revision
|
||
|
|
!== (int) $this->manifest['version']) {
|
||
|
|
throw new UserCopyAdminException(
|
||
|
|
'Manifest 词条合同尚未同步:' . $copyKey,
|
||
|
|
409
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|