You've already forked Atomcms-edit
cbe189fd96
- Create EmulatorControlAction and NitroControlAction classes - Extract business logic from Commandocentrum controller methods - Add Blade components for status cards, diagnostics, and summary cards - Replace shell_exec with file_get_contents in config reading - Remove duplicate methods and unused code - Commandocentrum reduced from 2033 to 1780 lines
120 lines
4.3 KiB
PHP
Executable File
120 lines
4.3 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Actions\Commandocentrum;
|
|
|
|
use App\Services\SettingsService;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Process;
|
|
|
|
class NitroControlAction
|
|
{
|
|
public function __construct(
|
|
private readonly SettingsService $settings,
|
|
) {}
|
|
|
|
public function pullUpdates(string $clientPath, string $rendererPath, string $branch): array
|
|
{
|
|
$this->runGitPull($clientPath, $branch);
|
|
$this->runGitPull($rendererPath, $branch);
|
|
|
|
return ['success' => true, 'message' => 'Nitro bijgewerkt van GitHub'];
|
|
}
|
|
|
|
public function build(string $clientPath, string $rendererPath, string $branch): array
|
|
{
|
|
$this->runGitPull($clientPath, $branch);
|
|
$this->runGitPull($rendererPath, $branch);
|
|
|
|
Process::timeout(120)->run('cd ' . escapeshellarg($clientPath) . ' && sudo -u www-data npm install 2>&1');
|
|
Process::timeout(120)->run('cd ' . escapeshellarg($rendererPath) . ' && sudo -u www-data npm install 2>&1');
|
|
|
|
$exitCode = Artisan::call('build:theme');
|
|
|
|
return [
|
|
'success' => $exitCode === 0,
|
|
'message' => $exitCode === 0 ? 'Nitro build succesvol!' : 'Build gestart - controleer handmatig',
|
|
];
|
|
}
|
|
|
|
public function generateConfigs(string $siteUrl, string $webroot, string $gamedataPath): array
|
|
{
|
|
if (! filter_var($siteUrl, FILTER_VALIDATE_URL)) {
|
|
return ['success' => false, 'message' => 'Voer een geldige URL in'];
|
|
}
|
|
|
|
$existingConfigs = $this->readExistingConfigs($webroot, $gamedataPath);
|
|
|
|
$exitCode = Artisan::call('app:generate-nitro-configs', ['--site-url' => $siteUrl]);
|
|
|
|
if ($existingConfigs !== [] && $exitCode === 0) {
|
|
$this->mergeExistingConfigs($webroot, $existingConfigs);
|
|
}
|
|
|
|
$this->settings->set('nitro_last_checked', now()->toIso8601String());
|
|
|
|
return [
|
|
'success' => $exitCode === 0,
|
|
'message' => $exitCode === 0 ? 'Configs gegenereerd & bestaande instellingen behouden!' : 'Config gegenereerd (controleer handmatig)',
|
|
];
|
|
}
|
|
|
|
private function runGitPull(string $path, string $branch): void
|
|
{
|
|
Process::timeout(60)->run('cd ' . escapeshellarg($path) . ' && sudo -u www-data git pull origin ' . escapeshellarg($branch) . ' 2>&1');
|
|
}
|
|
|
|
private function readExistingConfigs(string $webroot, string $gamedataPath): array
|
|
{
|
|
$configs = [];
|
|
$files = ['renderer-config.json', 'ui-config.json', 'UITexts.json'];
|
|
|
|
foreach ($files as $file) {
|
|
$path = $webroot . '/' . $file;
|
|
$content = @file_get_contents($path);
|
|
if ($content) {
|
|
$decoded = json_decode($content, true);
|
|
if (json_last_error() === JSON_ERROR_NONE) {
|
|
$configs[$file] = $decoded;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($gamedataPath !== '') {
|
|
$gamedataConfigs = [
|
|
'ExternalTexts.json' => $gamedataPath . '/config/ExternalTexts.json',
|
|
'FurnitureData.json' => $gamedataPath . '/config/FurnitureData.json',
|
|
'ProductData.json' => $gamedataPath . '/config/ProductData.json',
|
|
'FigureData.json' => $gamedataPath . '/config/FigureData.json',
|
|
];
|
|
|
|
foreach ($gamedataConfigs as $key => $path) {
|
|
$content = @file_get_contents($path);
|
|
if ($content) {
|
|
$decoded = json_decode($content, true);
|
|
if (json_last_error() === JSON_ERROR_NONE) {
|
|
$configs['gamedata.' . $key] = $decoded;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $configs;
|
|
}
|
|
|
|
private function mergeExistingConfigs(string $webroot, array $existingConfigs): void
|
|
{
|
|
if (isset($existingConfigs['renderer-config.json'])) {
|
|
$newPath = $webroot . '/renderer-config.json';
|
|
$newContent = @file_get_contents($newPath);
|
|
$newConfig = json_decode($newContent, true);
|
|
|
|
if ($newConfig && json_last_error() === JSON_ERROR_NONE) {
|
|
$merged = array_merge($existingConfigs['renderer-config.json'], $newConfig);
|
|
@file_put_contents($newPath, json_encode($merged, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
|
}
|
|
}
|
|
}
|
|
}
|