feat(compat): Profile, Gates und Pods über den Kompatibilitätskern
Neu: Tier, Profile, Compatibility, CapabilityLocked, CapabilityGate, CompatibilityComponent, ProvidesCompatibility, DiviProfile sowie PodsFeature, PodsCompatibility und PodsIntegration. Das CapabilityGate sperrt gesperrte und unbekannte Fähigkeits-IDs (übernimmt die Sperre unbekannter IDs vom DiviFeatureGate) und delegiert nur Divi ausdrücklich. Pods-Schema, -Lesen, -Schreiben und -Feldrechte laufen über usable(PodsFeature::…); Schreiben verlangt FieldRead und FieldWrite, damit ein Rollback keine Werte verliert. lit-site-status meldet Pods über die CompatibilityComponent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b511ef6348
commit
c85131603b
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Compatibility;
|
||||
|
||||
use Closure;
|
||||
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, CompatibilityMatrix> */
|
||||
private array $matrices = [];
|
||||
/** @var array<string, ManifestCheck> */
|
||||
private array $checks = [];
|
||||
/** @var Closure(Profile): ManifestCheck */
|
||||
private Closure $check;
|
||||
|
||||
/**
|
||||
* @param list<Profile> $profiles
|
||||
* @param (Closure(Profile): ManifestCheck)|null $check Builds the check of a profile; default: its manifests
|
||||
* without signature checks.
|
||||
* @throws LogicException For a profile ID or a capability ID that occurs twice.
|
||||
*/
|
||||
public function __construct(private array $profiles, ?Closure $check = null)
|
||||
{
|
||||
$this->check = $check ?? static fn (Profile $profile): ManifestCheck => new ManifestCheck(
|
||||
$profile->manifests,
|
||||
signatures: false
|
||||
);
|
||||
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 $profile->active === null || self::attempt($profile->active) === true;
|
||||
}
|
||||
|
||||
public function version(Profile $profile): ?string
|
||||
{
|
||||
$version = $profile->version === null ? null : self::attempt($profile->version);
|
||||
|
||||
return is_string($version) ? $version : null;
|
||||
}
|
||||
|
||||
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) {
|
||||
return [self::failed($profile)];
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return CompatibilityMatrix::fromProblems(
|
||||
$capabilities,
|
||||
$this->checkOf($profile)->problems(),
|
||||
null,
|
||||
automatic: true
|
||||
);
|
||||
} catch (Throwable) {
|
||||
return CompatibilityMatrix::allIncompatible($capabilities, self::failed($profile));
|
||||
}
|
||||
}
|
||||
|
||||
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): mixed
|
||||
{
|
||||
try {
|
||||
return $closure();
|
||||
} catch (Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user