Files
WP-LIT-MCP/src/Compatibility/Compatibility.php
T
Jan-Niclas LoosenandClaude Opus 5.5 7d6a97a54e refactor(compat): Freigabe-Bausteine generisch
ReleaseStore, CheckReportStore, CheckReport, CheckStep und CompatibilityCheck liegen im Kern und arbeiten je Profil (Optionen lit_mcp_<id>_releases|_check_report; Divi behält Namen und Schlüssel divi_version). Divi hängt Katalog-, Format- und Schreibtest über Profile::extraSteps an. Ein roter Prüfbericht für den aktuellen Stand sperrt LockOnFailure-Profile bis zur nächsten grünen Prüfung; Sperrgründe nennen nur Schritt-Labels, Ausnahmen gehen ins Error-Log. Profile bekommt testedVersions, stateKey, extraSteps, manifestCheck und reportVersionKey und wird mit benannten Argumenten gebaut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 12:51:03 +01:00

281 lines
9.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace LIT\MCP\Compatibility;
use Closure;
use LIT\MCP\Log;
use LIT\MCP\Plugin;
use LogicException;
use Throwable;
defined('ABSPATH') || exit;
/**
* The compatibility state of all third-party plugins, evaluated per Profile and memoised per request. Everything
* that cannot be judged (a throwing closure or check, an unknown version under a minimum, an unowned capability)
* counts as locked – fail closed.
*/
final class Compatibility
{
/** @var array<string, Profile> keyed by profile ID */
private array $byId = [];
/** @var array<string, array{0: Capability, 1: Profile}> keyed by capability ID */
private array $byCapability = [];
/** @var array<string, bool> */
private array $actives = [];
/** @var array<string, string|null> */
private array $versions = [];
/** @var array<string, CompatibilityMatrix> */
private array $matrices = [];
/** @var array<string, ManifestCheck> */
private array $checks = [];
/** @var Closure(Profile): ManifestCheck */
private Closure $check;
/** @var Closure(Profile): ?CheckReport */
private Closure $report;
/**
* @param list<Profile> $profiles
* @param (Closure(Profile): ManifestCheck)|null $check Builds the check of a profile; default: Profile::checker().
* @param (Closure(Profile): ?CheckReport)|null $report The latest check report of a profile; a red one for
* the current state locks a LockOnFailure profile.
* Default: none (the structure check is the base).
* @throws LogicException For a profile ID or a capability ID that occurs twice.
*/
public function __construct(private array $profiles, ?Closure $check = null, ?Closure $report = null)
{
$this->report = $report ?? static fn (Profile $profile): ?CheckReport => null;
$this->check = $check ?? static fn (Profile $profile): ManifestCheck => $profile->checker();
foreach ($profiles as $profile) {
if (isset($this->byId[$profile->id])) {
throw new LogicException('profile ID twice: ' . $profile->id);
}
$this->byId[$profile->id] = $profile;
foreach ($profile->capabilities as $capability) {
if (isset($this->byCapability[$capability->id()])) {
throw new LogicException('capability ID twice: ' . $capability->id());
}
$this->byCapability[$capability->id()] = [$capability, $profile];
}
}
}
/** @return list<Profile> */
public function profiles(): array
{
return $this->profiles;
}
public function profile(string $id): ?Profile
{
return $this->byId[$id] ?? null;
}
public function capability(string $id): ?Capability
{
return ($this->byCapability[$id] ?? [null])[0];
}
public function owner(Capability $capability): ?Profile
{
return ($this->byCapability[$capability->id()] ?? [1 => null])[1];
}
public function active(Profile $profile): bool
{
return $this->actives[$profile->id] ??= $profile->active === null
|| self::attempt($profile->active, $profile) === true;
}
public function version(Profile $profile): ?string
{
if (! array_key_exists($profile->id, $this->versions)) {
$version = $profile->version === null ? null : self::attempt($profile->version, $profile);
$this->versions[$profile->id] = is_string($version) ? $version : null;
}
return $this->versions[$profile->id];
}
public function matrix(Profile $profile): CompatibilityMatrix
{
return $this->matrices[$profile->id] ??= $this->build($profile);
}
/** @return list<string> Labels of what is wrong with the plugin; for ReportOnly the problems of the check. */
public function problems(Profile $profile): array
{
if ($profile->tier !== Tier::ReportOnly) {
return $this->matrix($profile)->problems();
}
try {
return array_values(array_map(
static fn (Problem $problem): string => $problem->label,
$this->checkOf($profile)->problems()
));
} catch (Throwable $e) {
Log::failure('compat ' . $profile->id, $e);
return [self::failed($profile)];
}
}
/** Runs the admin check of the profile (spec §6.2); a report for $userId. */
public function check(Profile $profile, int $userId): CheckReport
{
return CompatibilityCheck::forProfile($profile, $this->checkOf($profile))->run($userId);
}
public function usable(Capability $capability): bool
{
$owner = $this->owner($capability);
return $owner !== null && $this->matrix($owner)->of($capability)->usable();
}
/**
* The lock of those of $capabilities that are not usable, null when all are. Unowned ones are left out.
*
* @param list<Capability> $capabilities
*/
public function lockOf(array $capabilities): ?CapabilityLocked
{
$locked = [];
$owners = [];
foreach ($capabilities as $capability) {
$owner = $this->owner($capability);
if ($owner === null) {
continue;
}
$status = $this->matrix($owner)->of($capability);
if (! $status->usable()) {
$locked[$capability->id()] = $status;
$owners[$owner->id] = $owner;
}
}
return $locked === [] ? null : CapabilityLocked::of(array_values($locked), array_values($owners));
}
private function build(Profile $profile): CompatibilityMatrix
{
$capabilities = $profile->capabilities;
try {
if ($profile->tier === Tier::ReleasePerVersion) {
return ($profile->matrix)();
}
if ($profile->tier === Tier::ReportOnly) {
return CompatibilityMatrix::allReleased($capabilities);
}
if (! $this->active($profile)) {
return CompatibilityMatrix::allIncompatible(
$capabilities,
sprintf('%s ist nicht installiert oder nicht aktiv.', $profile->label)
);
}
$tooOld = $this->versionProblem($profile);
if ($tooOld !== null) {
return CompatibilityMatrix::allIncompatible($capabilities, $tooOld);
}
$red = $this->redReport($profile);
if ($red !== null) {
// shortcut: a step names no capability, so a red check locks all of them; refine with step features.
return CompatibilityMatrix::allIncompatible($capabilities, $red);
}
return CompatibilityMatrix::fromProblems(
$capabilities,
$this->checkOf($profile)->problems(),
null,
automatic: true
);
} catch (Throwable $e) {
Log::failure('compat ' . $profile->id, $e);
return CompatibilityMatrix::allIncompatible($capabilities, self::failed($profile));
}
}
/**
* What the latest report says when it is red and made for the current state; null = no reason to lock. A report
* without steps is no check result. The reason names only the labels of the steps, never their problem texts
* (they reach MCP clients in the 423). Only a new green check or a version change (plugin or third-party plugin)
* lifts a lock by a red report.
*/
private function redReport(Profile $profile): ?string
{
$report = ($this->report)($profile);
$version = $this->version($profile) ?? '';
$stateKey = $profile->stateKey === null ? '' : ($profile->stateKey)($version);
if (
$report === null
|| $report->steps === []
|| $report->green()
|| ! $report->currentFor($version, Plugin::VERSION, $stateKey)
) {
return null;
}
$failed = array_map(
static fn (CheckStep $step): string => $step->label(),
array_values(array_filter($report->steps, static fn (CheckStep $step): bool => ! $step->green()))
);
return sprintf(
'Die letzte Prüfung von %s ist fehlgeschlagen (%s); Details im Admin-Bereich.',
$profile->label,
implode('; ', $failed)
);
}
/** The report stored by the admin check, for Compatibility's $report parameter. */
public static function storedReport(Profile $profile): ?CheckReport
{
return CheckReportStore::forProfile($profile)->latest();
}
private function versionProblem(Profile $profile): ?string
{
if ($profile->minVersion === null) {
return null;
}
$version = $this->version($profile);
if ($version === null) {
return sprintf(
'Die Version von %s ist unbekannt (Mindestversion %s).',
$profile->label,
$profile->minVersion
);
}
return version_compare($version, $profile->minVersion, '<')
? sprintf('%s %s ist älter als die Mindestversion %s.', $profile->label, $version, $profile->minVersion)
: null;
}
private function checkOf(Profile $profile): ManifestCheck
{
return $this->checks[$profile->id] ??= ($this->check)($profile);
}
private static function failed(Profile $profile): string
{
return sprintf('Die Kompatibilitätsprüfung von %s ist fehlgeschlagen.', $profile->label);
}
/** The closure's result, null when it throws. */
private static function attempt(Closure $closure, Profile $profile): mixed
{
try {
return $closure();
} catch (Throwable $e) {
Log::failure('compat ' . $profile->id, $e);
return null;
}
}
}