Loheide-eu-mitarbeiterplatt.../app/Plugins/Hooks.php
Friederich Loheide 28ef1e4a20 Plugin-System; Neuigkeiten und Termine als mitgelieferte Plugins
- 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>
2026-09-26 07:06:16 +00:00

67 lines
1.9 KiB
PHP

<?php
namespace App\Plugins;
/**
* Actions und Filter nach WordPress-Vorbild.
*
* Hooks::on('post.published', fn (NewsPost $post) => …); // Action
* Hooks::do('post.published', $post);
*
* Hooks::filter('navigation.items', fn (array $items) => [...]); // Filter
* $items = Hooks::apply('navigation.items', $items);
*
* Niedrigere Priorität läuft zuerst (Standard 10).
*/
class Hooks
{
/** @var array<string, array<int, list<callable>>> */
private array $actions = [];
/** @var array<string, array<int, list<callable>>> */
private array $filters = [];
public static function on(string $name, callable $callback, int $priority = 10): void
{
app(self::class)->actions[$name][$priority][] = $callback;
}
public static function do(string $name, mixed ...$arguments): void
{
foreach (app(self::class)->sorted(app(self::class)->actions[$name] ?? []) as $callback) {
$callback(...$arguments);
}
}
public static function filter(string $name, callable $callback, int $priority = 10): void
{
app(self::class)->filters[$name][$priority][] = $callback;
}
public static function apply(string $name, mixed $value, mixed ...$arguments): mixed
{
foreach (app(self::class)->sorted(app(self::class)->filters[$name] ?? []) as $callback) {
$value = $callback($value, ...$arguments);
}
return $value;
}
public static function has(string $name): bool
{
$hooks = app(self::class);
return ! empty($hooks->actions[$name]) || ! empty($hooks->filters[$name]);
}
/**
* @param array<int, list<callable>> $byPriority
* @return list<callable>
*/
private function sorted(array $byPriority): array
{
ksort($byPriority);
return array_merge(...array_values($byPriority) ?: [[]]);
}
}