Some checks failed
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 11m34s
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 12m8s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 15m14s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 15m47s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 13m48s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 14m15s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 16m38s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 17m2s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 1m16s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m53s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
112 lines
3.5 KiB
PHP
112 lines
3.5 KiB
PHP
<?php
|
||
|
||
namespace App\Plugins;
|
||
|
||
use App\Support\Update\AtomicFile;
|
||
|
||
/**
|
||
* Aktivierungszustand der Plugins in storage/app/plugins.json – als Datei, damit
|
||
* das Laden der Plugins keine Datenbankabfrage kostet und auch ohne DB klappt.
|
||
* Mitgelieferte Plugins sind aktiv, solange nichts anderes vermerkt ist – außer sie
|
||
* sind als optional markiert („enabled_by_default“: false in plugin.json).
|
||
*/
|
||
class PluginState
|
||
{
|
||
/**
|
||
* Test-Suite: optionale mitgelieferte Plugins ebenfalls laden, damit ihre
|
||
* Tabellen und Tests dabei sind (gesetzt in tests/TestCase.php).
|
||
*/
|
||
public static bool $enableOptionalCorePlugins = false;
|
||
|
||
/** @var array<string, array<string, mixed>>|null */
|
||
private ?array $data = null;
|
||
|
||
public function __construct(private readonly ?string $file = null) {}
|
||
|
||
public function isEnabled(PluginManifest $manifest): bool
|
||
{
|
||
$entry = $this->all()[$manifest->slug] ?? null;
|
||
|
||
// Ein Eintrag kann auch nur einen vermerkten Absturz enthalten – dann gilt die Voreinstellung
|
||
return is_array($entry) && array_key_exists('enabled', $entry)
|
||
? (bool) $entry['enabled']
|
||
: $manifest->core && ($manifest->enabledByDefault || self::$enableOptionalCorePlugins);
|
||
}
|
||
|
||
public function setEnabled(string $slug, bool $enabled, ?string $reason = null): void
|
||
{
|
||
$this->update($slug, ['enabled' => $enabled, 'disabled_reason' => $enabled ? null : $reason, 'disabled_at' => $enabled ? null : time(), 'crashed_at' => null]);
|
||
}
|
||
|
||
/** Letzter Absturz beim Laden, der das Plugin noch nicht abgeschaltet hat (Unix-Zeit) */
|
||
public function crashedAt(string $slug): ?int
|
||
{
|
||
$this->data = null; // frisch lesen – eine parallele Anfrage kann ihn eben vermerkt haben
|
||
$at = $this->all()[$slug]['crashed_at'] ?? null;
|
||
|
||
return is_int($at) ? $at : null;
|
||
}
|
||
|
||
public function recordCrash(string $slug): void
|
||
{
|
||
$this->update($slug, ['crashed_at' => time()]);
|
||
}
|
||
|
||
/** Zeitpunkt der Deaktivierung (Unix-Zeit), falls bekannt */
|
||
public function disabledAt(string $slug): ?int
|
||
{
|
||
$at = $this->all()[$slug]['disabled_at'] ?? null;
|
||
|
||
return is_int($at) ? $at : null;
|
||
}
|
||
|
||
public function disabledReason(string $slug): ?string
|
||
{
|
||
return $this->all()[$slug]['disabled_reason'] ?? null;
|
||
}
|
||
|
||
public function forget(string $slug): void
|
||
{
|
||
$data = $this->all();
|
||
unset($data[$slug]);
|
||
$this->write($data);
|
||
}
|
||
|
||
/**
|
||
* @return array<string, array<string, mixed>>
|
||
*/
|
||
public function all(): array
|
||
{
|
||
if ($this->data === null) {
|
||
$decoded = json_decode((string) @file_get_contents($this->path()), true);
|
||
$this->data = is_array($decoded) ? $decoded : [];
|
||
}
|
||
|
||
return $this->data;
|
||
}
|
||
|
||
/**
|
||
* @param array<string, mixed> $values
|
||
*/
|
||
private function update(string $slug, array $values): void
|
||
{
|
||
$this->data = null;
|
||
$data = $this->all();
|
||
$data[$slug] = array_merge($data[$slug] ?? [], $values);
|
||
$this->write($data);
|
||
}
|
||
|
||
/**
|
||
* @param array<string, array<string, mixed>> $data
|
||
*/
|
||
private function write(array $data): void
|
||
{
|
||
AtomicFile::write($this->path(), json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||
$this->data = $data;
|
||
}
|
||
|
||
private function path(): string
|
||
{
|
||
return $this->file ?? storage_path('app/plugins.json');
|
||
}
|
||
}
|