- Plugins nach WordPress-Prinzip: plugin.json-Manifest mit Versions- und Abhängigkeitsprüfung, eigenes PSR-4-Autoloading, register()/boot(), activate()/deactivate()/uninstall(), Migrationen, Routen und Views je Plugin - plugins-core/ (mitgeliefert, aktiv bis zur Deaktivierung) und plugins/ (Kunden-Plugins, geschützt vor Updates, inaktiv bis zur Aktivierung) - Zustand in storage/app/plugins.json – Laden ohne Datenbankabfrage - Wiederherstellungsmodus: Ausnahmen und Fatal Errors beim Laden deaktivieren das Plugin, das Portal bleibt erreichbar - Hooks (Actions/Filter mit Prioritäten) und Registries für Navigation, Startseiten-Kacheln, Rechte und Admin-Seiten (Filament-Discovery) - Admin-Seite „Plugins“: aktivieren, deaktivieren, per ZIP installieren (ZIP-Slip-Schutz), löschen mit oder ohne Daten; abschaltbar per INHAUS_DISALLOW_FILE_MODS - Startseite komplett aus Kacheln; Plugin-Seiten als „slug::Seite“ - Version 0.3.0 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
80 lines
2.2 KiB
PHP
80 lines
2.2 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.
|
||
*/
|
||
class PluginState
|
||
{
|
||
/** @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;
|
||
}
|
||
|
||
public function setEnabled(string $slug, bool $enabled, ?string $reason = null): void
|
||
{
|
||
$this->update($slug, ['enabled' => $enabled, 'disabled_reason' => $enabled ? null : $reason]);
|
||
}
|
||
|
||
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');
|
||
}
|
||
}
|