111 lines
3.6 KiB
PHP
111 lines
3.6 KiB
PHP
<?php
|
|
/**
|
|
* FlowManager helper functies
|
|
*/
|
|
|
|
// Deploy een JSON-flow naar Node-RED
|
|
function deployFlow(string $flowPath): string {
|
|
if (!file_exists($flowPath)) return "Flowbestand niet gevonden: $flowPath";
|
|
|
|
$flowJson = file_get_contents($flowPath);
|
|
$url = "http://localhost:1880/flows";
|
|
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
|
CURLOPT_POSTFIELDS => $flowJson,
|
|
]);
|
|
$response = curl_exec($ch);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return $error ?: $response;
|
|
}
|
|
|
|
// Haal alle versie-bestanden op in een map
|
|
function findVersionedJsonFilesInDir(string $dir): array {
|
|
$matches = [];
|
|
foreach (scandir($dir) as $file) {
|
|
if (preg_match('/^version\d+_\d{12}\.json$/', $file)) {
|
|
$matches[] = ['name' => $file];
|
|
}
|
|
}
|
|
usort($matches, fn($a,$b) => strcmp($a['name'],$b['name']));
|
|
return $matches;
|
|
}
|
|
|
|
// Vind het laatst gedeployde bestand
|
|
function findLatestDeployFile(string $dir): ?string {
|
|
$files = findVersionedJsonFilesInDir($dir);
|
|
return !empty($files) ? end($files)['name'] : null;
|
|
}
|
|
|
|
// Haal versie uit een JSON-bestand
|
|
function getVersionFromJson(string $filePath): int {
|
|
if (!file_exists($filePath)) return 0;
|
|
$json = json_decode(file_get_contents($filePath), true);
|
|
return $json[0]['version'] ?? 0;
|
|
}
|
|
|
|
// Haal versie uit een template
|
|
function getVersionFromTemplate(string $templatePath): int {
|
|
if (!file_exists($templatePath)) return 0;
|
|
preg_match('/"version"\s*:\s*(\d+)/', file_get_contents($templatePath), $m);
|
|
return $m[1] ?? 0;
|
|
}
|
|
|
|
// Controleer of de template een hogere versie heeft dan de laatste deploy
|
|
function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string {
|
|
$templateVersion = getVersionFromTemplate($templatePath);
|
|
$latestDeployFile = findLatestDeployFile($deployDir);
|
|
$latestVersion = $latestDeployFile ? getVersionFromJson($deployDir.'/'.$latestDeployFile) : 0;
|
|
|
|
if ($templateVersion > $latestVersion) {
|
|
return "<div class='alert alert-warning'>
|
|
Nieuwe template-versie beschikbaar!
|
|
(Template v$templateVersion > Laatste deploy v$latestVersion)
|
|
</div>";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Genereer de bestandsnaam voor een nieuwe deploy
|
|
function generateNextDeployFileName(int $version): string {
|
|
return "version{$version}_".date('YmdHi').".json";
|
|
}
|
|
|
|
// Template renderen met placeholders
|
|
function renderTemplate(string $templatePath, array $placeholders): string {
|
|
$content = file_get_contents($templatePath);
|
|
foreach ($placeholders as $key => $value) {
|
|
$content = str_replace("{{{$key}}}", $value, $content);
|
|
}
|
|
return $content;
|
|
}
|
|
|
|
// Render een standaard alert
|
|
function renderAlert(string $message, string $type = 'info'): string {
|
|
return "<div class='alert alert-{$type}'>$message</div>";
|
|
}
|
|
|
|
// Verkrijg een overzicht van alle deploys inclusief versies
|
|
function getDeploysWithVersions(string $dir): array {
|
|
$files = findVersionedJsonFilesInDir($dir);
|
|
return array_map(function($f) use ($dir) {
|
|
return [
|
|
'name' => $f['name'],
|
|
'version' => getVersionFromJson($dir.'/'.$f['name'])
|
|
];
|
|
}, array_reverse($files));
|
|
}
|
|
|
|
function getNodeRedEnvironments(string $envFile = '/environments.json'): array {
|
|
$json = @file_get_contents($envFile); // @ om warnings te onderdrukken
|
|
if (!$json) return [];
|
|
$data = json_decode($json, true);
|
|
return is_array($data) ? $data : [];
|
|
}
|
|
|