Loheide-eu-mitarbeiterplatt.../app/Plugins/PluginAssets.php
Friederich Loheide ea2200cb3e
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m46s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 3m49s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m7s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m8s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m51s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m29s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m12s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m11s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m33s
Plugin-SDK: Oberflächen für Kunden-Plugins, Generator, Doku (0.20.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 04:57:28 +00:00

105 lines
3.5 KiB
PHP
Raw 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\Version;
/**
* Vorkompilierte Oberflächen separat installierter Plugins (Plugin-SDK).
*
* Ein Plugin bringt unter dist/ ES-Module und optional CSS mit und beschreibt sie in
* dist/inhaus.json:
*
* { "components": { "pages/Index": "pages/Index.js", "widgets/Verse": "widgets/Verse.js" },
* "css": ["style.css"] }
*
* Mitgelieferte Plugins brauchen das nicht – sie sind Teil des Portal-Builds.
*/
class PluginAssets
{
public const MANIFEST = 'dist/inhaus.json';
/** Dateitypen, die ausgeliefert werden */
public const TYPES = [
'js' => 'text/javascript', 'mjs' => 'text/javascript', 'css' => 'text/css', 'map' => 'application/json',
'json' => 'application/json', 'svg' => 'image/svg+xml', 'png' => 'image/png', 'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg', 'webp' => 'image/webp', 'gif' => 'image/gif', 'woff2' => 'font/woff2',
];
/** @var array<string, array{base: string, components: array<string, string>, css: list<string>}>|null */
private ?array $cache = null;
public function __construct(private readonly PluginManager $plugins) {}
/**
* Für das Frontend (window.__inhausPlugins): aktive Plugins mit Oberfläche.
*
* @return array<string, array{base: string, components: array<string, string>, css: list<string>}>
*/
public function forFrontend(): array
{
if ($this->cache !== null) {
return $this->cache;
}
$this->cache = [];
foreach ($this->plugins->loaded() as $slug => $plugin) {
$manifest = $plugin->manifest;
if ($manifest->core || ! is_file($manifest->path.'/'.self::MANIFEST)) {
continue;
}
$data = json_decode((string) file_get_contents($manifest->path.'/'.self::MANIFEST), true);
if (! is_array($data)) {
continue;
}
$clean = fn ($file) => is_string($file) && preg_match('#^[A-Za-z0-9_./-]+$#', $file) && ! str_contains($file, '..') ? $file : null;
$this->cache[$slug] = [
'base' => '/plugin-assets/'.rawurlencode($slug).'/'.rawurlencode($manifest->version).'/',
'components' => array_filter(array_map($clean, (array) ($data['components'] ?? []))),
'css' => array_values(array_filter(array_map($clean, (array) ($data['css'] ?? [])))),
];
}
return $this->cache;
}
/**
* Import Map für Plugins: dieselben Module wie im Portal.
*
* @return array{imports: array<string, string>}
*/
public static function importMap(): array
{
$v = '?v='.Version::VERSION;
return ['imports' => [
'vue' => '/sdk/vue.js'.$v,
'@inertiajs/vue3' => '/sdk/inertia.js'.$v,
'@inhaus/ui' => '/sdk/ui.js'.$v,
]];
}
/**
* Pfad einer ausgelieferten Datei – nur aus dist/ eines aktiven Kunden-Plugins.
*/
public function file(string $slug, string $path): ?string
{
$plugin = $this->plugins->loaded()[$slug] ?? null;
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if (! $plugin || $plugin->manifest->core || ! isset(self::TYPES[$extension]) || str_contains($path, '..')) {
return null;
}
$dist = realpath($plugin->manifest->path.'/dist');
$file = realpath($plugin->manifest->path.'/dist/'.$path);
return $dist && $file && str_starts_with($file, $dist.DIRECTORY_SEPARATOR) && is_file($file) ? $file : null;
}
}