Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.2 KiB
PHP
107 lines
3.2 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace LIT\MCP\Integrations\Divi\Release;
|
||
|
||
use Closure;
|
||
use LIT\MCP\Log;
|
||
use Throwable;
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
/**
|
||
* The release status of this request and the lock every writing Divi path asks for the feature it needs (spec §5.5,
|
||
* E1). whileProbing() lets the admin check write its throwaway page whatever the status – only that post, only
|
||
* during the call, and only from code (there is no parameter or option for it).
|
||
*/
|
||
final class ReleaseGate
|
||
{
|
||
private static ?ReleaseStatus $status = null;
|
||
|
||
/** @var (Closure(): ReleaseStatus)|null */
|
||
private static ?Closure $resolver = null;
|
||
|
||
private static ?int $probe = null;
|
||
|
||
private const UNRESOLVED = 'Der Freigabe-Zustand ließ sich nicht ermitteln (Details im PHP-Error-Log).';
|
||
|
||
/** Spec §9: a status that cannot be determined locks every feature as incompatible (and is logged). */
|
||
public static function status(): ReleaseStatus
|
||
{
|
||
if (self::$status !== null) {
|
||
return self::$status;
|
||
}
|
||
|
||
try {
|
||
self::$status = (self::$resolver ?? static fn (): ReleaseStatus => (new ReleaseResolver())->resolve())();
|
||
} catch (Throwable $e) {
|
||
Log::failure('divi release status', $e);
|
||
self::$status = ReleaseStatus::of('', CompatibilityMatrix::allIncompatible(self::UNRESOLVED));
|
||
}
|
||
|
||
return self::$status;
|
||
}
|
||
|
||
/** @param (Closure(): ReleaseStatus)|null $resolver null = ReleaseResolver; tests set their own. */
|
||
public static function useResolver(?Closure $resolver): void
|
||
{
|
||
self::$resolver = $resolver;
|
||
self::$status = null;
|
||
self::$probe = null;
|
||
}
|
||
|
||
/** The next status() computes again (after a check, a release or a withdrawal). */
|
||
public static function forget(): void
|
||
{
|
||
self::$status = null;
|
||
}
|
||
|
||
public static function feature(DiviFeature $feature): FeatureStatus
|
||
{
|
||
return self::status()->matrix->of($feature);
|
||
}
|
||
|
||
/** @throws DiviLocked When $feature is not usable – before anything is written. */
|
||
public static function assertFeature(DiviFeature $feature, ?int $postId = null): void
|
||
{
|
||
self::assertFeatures([$feature], $postId);
|
||
}
|
||
|
||
/**
|
||
* Every one of $features usable, else DiviLocked with the locked ones. The probe post passes whatever the status.
|
||
*
|
||
* @param list<DiviFeature> $features
|
||
* @throws DiviLocked
|
||
*/
|
||
public static function assertFeatures(array $features, ?int $postId = null): void
|
||
{
|
||
if (self::$probe !== null && $postId === self::$probe) {
|
||
return;
|
||
}
|
||
|
||
$status = self::status();
|
||
$locked = [];
|
||
foreach ($features as $feature) {
|
||
if (! $status->matrix->of($feature)->usable()) {
|
||
$locked[$feature->value] = $feature;
|
||
}
|
||
}
|
||
if ($locked !== []) {
|
||
throw new DiviLocked($status, array_values($locked));
|
||
}
|
||
}
|
||
|
||
public static function whileProbing(int $postId, callable $write): mixed
|
||
{
|
||
$previous = self::$probe;
|
||
self::$probe = $postId;
|
||
|
||
try {
|
||
return $write();
|
||
} finally {
|
||
self::$probe = $previous;
|
||
}
|
||
}
|
||
}
|