=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 $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 readonly bool $enabledByDefault = true, /** Frühere Kürzel, die dieses Plugin ersetzt (deren Ordner bleiben nach Updates liegen). */ public readonly array $replaces = [], ) {} 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 $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, enabledByDefault: (bool) ($data['enabled_by_default'] ?? true), replaces: array_values(array_filter((array) ($data['replaces'] ?? []), 'is_string')), ); } /** * @return list */ 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 */ 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'; } }