feat: add trmnlp support

This commit is contained in:
Benjamin Nussbaum 2025-08-22 21:09:12 +02:00
parent f1d5c196e8
commit 989ad2e985
8 changed files with 718 additions and 61 deletions

View file

@ -0,0 +1,186 @@
<?php
namespace App\Services;
use App\Models\Plugin;
use App\Models\User;
use Exception;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\Yaml\Yaml;
use ZipArchive;
/**
* PluginExportService
*
* Exports plugins to ZIP files in the same format that can be imported by PluginImportService.
*
* The exported ZIP file contains:
* - settings.yml: Plugin configuration including custom fields, polling settings, etc.
* - full.liquid or full.blade.php: The main template file
* - shared.liquid: Optional shared template (for liquid templates)
*
* This format is compatible with the PluginImportService and can be used to:
* - Backup plugins
* - Share plugins between users
* - Migrate plugins between environments
* - Create plugin templates
*/
class PluginExportService
{
/**
* Export a plugin to a ZIP file in the same format that can be imported
*
* @param Plugin $plugin The plugin to export
* @param User $user The user exporting the plugin
* @return BinaryFileResponse The ZIP file response
*
* @throws Exception If the ZIP file cannot be created
*/
public function exportToZip(Plugin $plugin, User $user): BinaryFileResponse
{
// Create a temporary directory
$tempDirName = 'temp/'.uniqid('plugin_export_', true);
Storage::makeDirectory($tempDirName);
$tempDir = Storage::path($tempDirName);
try {
// Generate settings.yml content
$settings = $this->generateSettingsYaml($plugin);
$settingsYaml = Yaml::dump($settings, 10, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);
File::put($tempDir.'/settings.yml', $settingsYaml);
// Generate full template content
$fullTemplate = $this->generateFullTemplate($plugin);
$extension = $plugin->markup_language === 'liquid' ? 'liquid' : 'blade.php';
File::put($tempDir.'/full.'.$extension, $fullTemplate);
// Generate shared.liquid if needed (for liquid templates)
if ($plugin->markup_language === 'liquid') {
$sharedTemplate = $this->generateSharedTemplate($plugin);
if ($sharedTemplate) {
File::put($tempDir.'/shared.liquid', $sharedTemplate);
}
}
// Create ZIP file
$zipPath = $tempDir.'/plugin_'.$plugin->trmnlp_id.'.zip';
$zip = new ZipArchive();
if ($zip->open($zipPath, ZipArchive::CREATE) !== true) {
throw new Exception('Could not create ZIP file.');
}
// Add files directly to ZIP root
$this->addDirectoryToZip($zip, $tempDir, '');
$zip->close();
// Return the ZIP file as a download response
return response()->download($zipPath, 'plugin_'.$plugin->trmnlp_id.'.zip');
} catch (Exception $e) {
throw $e;
}
}
/**
* Generate the settings.yml content for the plugin
*/
private function generateSettingsYaml(Plugin $plugin): array
{
$settings = [];
// Add fields in the specific order requested
$settings['name'] = $plugin->name;
$settings['no_screen_padding'] = 'no'; // Default value
$settings['dark_mode'] = 'no'; // Default value
$settings['strategy'] = $plugin->data_strategy;
// Add static data if available
if ($plugin->data_payload) {
$settings['static_data'] = json_encode($plugin->data_payload, JSON_PRETTY_PRINT);
}
// Add polling configuration if applicable
if ($plugin->data_strategy === 'polling') {
if ($plugin->polling_verb) {
$settings['polling_verb'] = $plugin->polling_verb;
}
if ($plugin->polling_url) {
$settings['polling_url'] = $plugin->polling_url;
}
if ($plugin->polling_header) {
// Convert header format from "key: value" to "key=value"
$settings['polling_headers'] = str_replace(':', '=', $plugin->polling_header);
}
if ($plugin->polling_body) {
$settings['polling_body'] = $plugin->polling_body;
}
}
$settings['refresh_interval'] = $plugin->data_stale_minutes;
$settings['id'] = $plugin->trmnlp_id;
// Add custom fields from configuration template
if (isset($plugin->configuration_template['custom_fields'])) {
$settings['custom_fields'] = $plugin->configuration_template['custom_fields'];
}
return $settings;
}
/**
* Generate the full template content
*/
private function generateFullTemplate(Plugin $plugin): string
{
$markup = $plugin->render_markup;
// Remove the wrapper div if it exists (it will be added during import)
$markup = preg_replace('/^<div class="view view--\{\{ size \}\}">\s*/', '', $markup);
$markup = preg_replace('/\s*<\/div>\s*$/', '', $markup);
return trim($markup);
}
/**
* Generate the shared template content (for liquid templates)
*/
private function generateSharedTemplate(Plugin $plugin)
{
// For now, we don't have a way to store shared templates separately
// TODO - add support for shared templates
return null;
}
/**
* Add a directory and its contents to a ZIP file
*/
private function addDirectoryToZip(ZipArchive $zip, string $dirPath, string $zipPath): void
{
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dirPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $file) {
if (! $file->isDir()) {
$filePath = $file->getRealPath();
$fileName = basename($filePath);
// For root directory, just use the filename
if ($zipPath === '') {
$relativePath = $fileName;
} else {
$relativePath = $zipPath.'/'.mb_substr($filePath, mb_strlen($dirPath) + 1);
}
$zip->addFile($filePath, $relativePath);
}
}
}
}

View file

@ -10,6 +10,7 @@ use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Yaml\Yaml;
use ZipArchive;
@ -82,34 +83,45 @@ class PluginImportService
'custom_fields' => $settings['custom_fields'],
];
// Extract default values from custom_fields and populate configuration
$configuration = [];
if (isset($settings['custom_fields']) && is_array($settings['custom_fields'])) {
$plugin_updated = isset($settings['id'])
&& Plugin::where('user_id', $user->id)->where('trmnlp_id', $settings['id'])->exists();
// Create a new plugin
$plugin = Plugin::updateOrCreate(
[
'user_id' => $user->id, 'trmnlp_id' => $settings['id'] ?? Uuid::v7(),
],
[
'user_id' => $user->id,
'name' => $settings['name'] ?? 'Imported Plugin',
'trmnlp_id' => $settings['id'] ?? Uuid::v7(),
'data_stale_minutes' => $settings['refresh_interval'] ?? 15,
'data_strategy' => $settings['strategy'] ?? 'static',
'polling_url' => $settings['polling_url'] ?? null,
'polling_verb' => $settings['polling_verb'] ?? 'get',
'polling_header' => isset($settings['polling_headers'])
? str_replace('=', ':', $settings['polling_headers'])
: null,
'polling_body' => $settings['polling_body'] ?? null,
'markup_language' => $markupLanguage,
'render_markup' => $fullLiquid,
'configuration_template' => $configurationTemplate,
'data_payload' => json_decode($settings['static_data'] ?? '{}', true),
]);
if (! $plugin_updated) {
// Extract default values from custom_fields and populate configuration
$configuration = [];
foreach ($settings['custom_fields'] as $field) {
if (isset($field['keyname']) && isset($field['default'])) {
$configuration[$field['keyname']] = $field['default'];
}
}
// set only if plugin is new
$plugin->update([
'configuration' => $configuration,
]);
}
// Create a new plugin
$plugin = Plugin::create([
'user_id' => $user->id,
'name' => $settings['name'] ?? 'Imported Plugin',
'data_stale_minutes' => $settings['refresh_interval'] ?? 15,
'data_strategy' => $settings['strategy'] ?? 'static',
'polling_url' => $settings['polling_url'] ?? null,
'polling_verb' => $settings['polling_verb'] ?? 'get',
'polling_header' => isset($settings['polling_headers'])
? str_replace('=', ':', $settings['polling_headers'])
: null,
'polling_body' => $settings['polling_body'] ?? null,
'markup_language' => $markupLanguage,
'render_markup' => $fullLiquid,
'configuration_template' => $configurationTemplate,
'configuration' => $configuration,
'data_payload' => json_decode($settings['static_data'] ?? '{}', true),
]);
$plugin['trmnlp_yaml'] = $settingsYaml;
return $plugin;
@ -119,12 +131,6 @@ class PluginImportService
}
}
/**
* Find required files in the extracted ZIP directory
*
* @param string $tempDir The temporary directory path
* @return array Array containing paths to required files
*/
private function findRequiredFiles(string $tempDir): array
{
$settingsYamlPath = null;