- 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>
128 lines
4 KiB
PHP
128 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Plugins;
|
|
|
|
use App\Version;
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* Inhalt einer plugin.json.
|
|
*
|
|
* {
|
|
* "slug": "mav-kalender",
|
|
* "name": "MAV-Kalender",
|
|
* "version": "1.0.0",
|
|
* "description": "…",
|
|
* "author": "…",
|
|
* "namespace": "Acme\\MavKalender\\",
|
|
* "class": "Acme\\MavKalender\\Plugin",
|
|
* "requires": { "core": ">=0.3.0", "php": ">=8.3", "plugins": ["news"] },
|
|
* "update_url": "https://…"
|
|
* }
|
|
*/
|
|
final class PluginManifest
|
|
{
|
|
public const SLUG_PATTERN = '/^[a-z0-9]+(?:-[a-z0-9]+)*$/';
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
private function __construct(
|
|
public readonly string $slug,
|
|
public readonly string $name,
|
|
public readonly string $version,
|
|
public readonly string $description,
|
|
public readonly string $author,
|
|
public readonly string $namespace,
|
|
public readonly string $class,
|
|
public readonly string $path,
|
|
public readonly bool $core,
|
|
public readonly array $requires,
|
|
public readonly ?string $updateUrl,
|
|
) {}
|
|
|
|
public static function fromDirectory(string $path, bool $core): self
|
|
{
|
|
$file = rtrim($path, '/').'/plugin.json';
|
|
$data = json_decode((string) @file_get_contents($file), true);
|
|
|
|
if (! is_array($data)) {
|
|
throw new InvalidArgumentException('plugin.json fehlt oder ist kein gültiges JSON.');
|
|
}
|
|
|
|
return self::fromArray($data, $path, $core);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $data
|
|
*/
|
|
public static function fromArray(array $data, string $path, bool $core): self
|
|
{
|
|
foreach (['slug', 'name', 'version', 'namespace', 'class'] as $field) {
|
|
if (! is_string($data[$field] ?? null) || trim($data[$field]) === '') {
|
|
throw new InvalidArgumentException("plugin.json: Feld „{$field}“ fehlt.");
|
|
}
|
|
}
|
|
|
|
if (! preg_match(self::SLUG_PATTERN, $data['slug'])) {
|
|
throw new InvalidArgumentException('plugin.json: „slug“ darf nur Kleinbuchstaben, Ziffern und Bindestriche enthalten.');
|
|
}
|
|
|
|
$namespace = rtrim($data['namespace'], '\\').'\\';
|
|
|
|
if (! str_starts_with(ltrim($data['class'], '\\'), $namespace)) {
|
|
throw new InvalidArgumentException('plugin.json: „class“ muss im angegebenen Namespace liegen.');
|
|
}
|
|
|
|
if (str_starts_with($namespace, 'App\\') || str_starts_with($namespace, 'Illuminate\\')) {
|
|
throw new InvalidArgumentException('plugin.json: Der Namespace „App\\“ ist dem Kern vorbehalten.');
|
|
}
|
|
|
|
return new self(
|
|
slug: $data['slug'],
|
|
name: $data['name'],
|
|
version: $data['version'],
|
|
description: (string) ($data['description'] ?? ''),
|
|
author: (string) ($data['author'] ?? ''),
|
|
namespace: $namespace,
|
|
class: ltrim($data['class'], '\\'),
|
|
path: rtrim($path, '/'),
|
|
core: $core,
|
|
requires: is_array($data['requires'] ?? null) ? $data['requires'] : [],
|
|
updateUrl: is_string($data['update_url'] ?? null) ? $data['update_url'] : null,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return list<string>
|
|
*/
|
|
public function requiredPlugins(): array
|
|
{
|
|
return array_values(array_filter((array) ($this->requires['plugins'] ?? []), 'is_string'));
|
|
}
|
|
|
|
/**
|
|
* Gründe, warum das Plugin in dieser Umgebung nicht laufen kann.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
public function incompatibilities(): array
|
|
{
|
|
$problems = [];
|
|
|
|
if (! VersionConstraint::satisfies(Version::VERSION, $this->requires['core'] ?? null)) {
|
|
$problems[] = 'benötigt Inhaus '.$this->requires['core'];
|
|
}
|
|
|
|
if (! VersionConstraint::satisfies(PHP_VERSION, $this->requires['php'] ?? null)) {
|
|
$problems[] = 'benötigt PHP '.$this->requires['php'];
|
|
}
|
|
|
|
return $problems;
|
|
}
|
|
|
|
public function migrationsPath(): string
|
|
{
|
|
return $this->path.'/database/migrations';
|
|
}
|
|
}
|