Loheide-eu-mitarbeiterplatt.../app/Plugins/PluginState.php
Friederich Loheide bf68be68f9
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 12m25s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 12m51s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 10m43s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 10m37s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 12m42s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 13m38s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 9m39s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m33s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Schneller und stabiler bei Updates (0.45.0)
- Ladefehler während eines Updates schalten keine Plugins mehr ab: Wartungsmodus
  merkt sich den Update-Zeitraum (+2 min Nachlauf), Plugin-Uploads markieren den
  Dateiaustausch ohne Wartungsmodus; guard/recoverFromCrash prüfen das, betroffene
  Plugins (mit disabled_at im Zeitraum) werden wieder eingeschaltet
- Plugin-Oberflächen werden nach public/plugin-assets kopiert (Webserver statt PHP),
  bei Deaktivieren/Löschen/Neu-Hochladen entfernt; Route bleibt als Rückfall
- RichTextEditor in SDK, Leichte-Sprache-Leiste und Glossar asynchron;
  fehlende Menü-Symbole kuratiert → kein Laden des kompletten Symbolsatzes
- Inertia-Prefetch (hover, 10 s) für Menülinks und Neuigkeiten-Vorschauen;
  Prefetch zählt nicht als Lesen (Reach)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 14:41:53 +00:00

95 lines
2.8 KiB
PHP
Raw Permalink 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
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;
return is_array($entry) ? (bool) ($entry['enabled'] ?? false) : $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()]);
}
/** 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');
}
}