refactor: integrate diagnostics into Commandocentrum and split EmulatorUpdateService

- Add DiagnosticRunner integration to Commandocentrum for system health display
- Refactor EmulatorUpdateService from 2524 lines to 395 lines (facade pattern)
- Extract EmulatorStatusService, EmulatorJarService, EmulatorSourceService
- Extract EmulatorBuildService, EmulatorSqlService, EmulatorBackupService
- Add shared EmulatorConfiguration trait for dependency injection
- Preserve backward compatibility on all public methods
This commit is contained in:
root
2026-05-19 20:20:43 +02:00
parent b1739cabbf
commit f5666c104d
17 changed files with 2743 additions and 7197 deletions
+197
View File
@@ -0,0 +1,197 @@
<?php
declare(strict_types=1);
namespace App\Services\Emulator;
use App\Services\SettingsService;
use Illuminate\Support\Facades\Process;
trait EmulatorConfiguration
{
private ?SettingsService $settings;
private ?string $githubUrl;
private ?string $jarDirectUrl;
private ?string $githubRepo;
private ?string $githubBranch;
private ?string $jarPath;
private ?string $emulatorService;
private ?string $emulatorSourcePath;
private ?string $sourceRepo;
private ?string $sourceBranch;
protected function loadConfiguration(): void
{
$this->settings = app(SettingsService::class);
$this->settings->clearInstanceCache();
$this->githubUrl = $this->settings->getOrDefault('emulator_github_url', '');
$this->jarDirectUrl = $this->settings->getOrDefault('emulator_jar_direct_url', '');
$basePath = $this->detectBasePath();
$this->jarPath = $this->resolveEmulatorPath($this->settings->getOrDefault('emulator_jar_path', $basePath . '/Emulator'));
$this->emulatorService = $this->settings->getOrDefault('emulator_service_name', $this->detectEmulatorService());
$this->emulatorSourcePath = $this->resolveEmulatorPath($this->settings->getOrDefault('emulator_source_path', $basePath . '/emulator-source'));
$this->sourceRepo = $this->settings->getOrDefault('emulator_source_repo', '');
$this->parseGitHubUrl($this->githubUrl);
$this->parseSourceRepo($this->sourceRepo);
$this->ensureGitSafeDirectories();
$settingBranch = $this->settings->getOrDefault('emulator_github_branch', null);
if ($settingBranch) {
$this->githubBranch = strtolower((string) $settingBranch);
$this->sourceBranch = strtolower((string) $settingBranch);
}
}
private function detectBasePath(): string
{
$possiblePaths = [
base_path(),
'/var/www/atomcms',
'/var/www/html',
'/var/www',
dirname(base_path()),
];
foreach ($possiblePaths as $path) {
if (is_dir($path)) {
return $path;
}
}
return '/var/www';
}
private function resolveEmulatorPath(string $path): string
{
if (str_starts_with($path, '/')) {
return $path;
}
return $this->detectBasePath() . '/' . ltrim($path, '/');
}
private function detectEmulatorService(): string
{
$possibleServices = ['emulator', 'arcturus', 'morningstar', 'habbo', 'hotel', 'game'];
foreach ($possibleServices as $service) {
$result = Process::timeout(5)->run("systemctl list-unit-files {$service}.service 2>/dev/null | grep -q '{$service}.service' && echo 'found'");
if ($result->successful() && trim($result->output()) === 'found') {
return $service;
}
$result = Process::timeout(5)->run("systemctl list-units --type=service --all 2>/dev/null | grep -q '{$service}.service' && echo 'found'");
if ($result->successful() && trim($result->output()) === 'found') {
return $service;
}
}
return 'emulator';
}
private function ensureGitSafeDirectories(): void
{
$directories = [
base_path(),
'/var/www/atomcms',
'/var/www',
$this->emulatorSourcePath,
$this->jarPath,
dirname((string) $this->emulatorSourcePath),
dirname((string) $this->jarPath),
];
foreach (array_unique($directories) as $dir) {
if (! empty($dir) && $this->isDirAccessible($dir)) {
Process::timeout(5)->run('git config --global --add safe.directory ' . escapeshellarg($dir) . ' 2>/dev/null || true');
}
}
Process::timeout(5)->run("git config --global --add safe.directory '*' 2>/dev/null || true");
}
private function isDirAccessible(string $path): bool
{
if ($path === '' || $path === '0') {
return false;
}
$result = Process::timeout(5)->run('test -d ' . escapeshellarg($path) . ' && echo "yes" || echo "no"');
return trim($result->output()) === 'yes';
}
private function parseGitHubUrl(string $url): void
{
$parsed = $this->parseGithubRepoUrl($url);
$this->githubRepo = $parsed['repo'];
$this->githubBranch = $parsed['branch'];
}
private function parseSourceRepo(string $url): void
{
$parsed = $this->parseGithubRepoUrl($url);
$this->sourceRepo = $parsed['repo'];
$this->sourceBranch = $parsed['branch'];
}
private function parseGithubRepoUrl(string $url): array
{
if ($url === '' || $url === '0') {
return ['repo' => null, 'branch' => 'main'];
}
if (preg_match('/github\.com\/([^\/]+)\/([^\/\?#]+)/', $url, $matches)) {
$repo = $matches[1] . '/' . $matches[2];
$branch = 'main';
if (preg_match('/\/tree\/([^\/]+)/', $url, $branchMatch)) {
$branch = $branchMatch[1];
}
return ['repo' => $repo, 'branch' => $branch];
}
return ['repo' => null, 'branch' => 'main'];
}
protected function extractVersionFromFilename(string $filename): string
{
$filename = basename($filename, '.jar');
if (preg_match('/[\d]+\.[\d]+\.[\d]+/', $filename, $matches)) {
return $matches[0];
}
if (preg_match('/v?([\d]+)/', $filename, $matches)) {
return $matches[1];
}
return '';
}
protected function commandDirExists(string $path): bool
{
$result = Process::timeout(5)->run('[ -d ' . escapeshellarg($path) . ' ] && echo "1" || echo "0"');
return trim($result->output()) === '1';
}
protected function commandFileExists(string $path): bool
{
$result = Process::timeout(5)->run('ls ' . $path . ' 2>/dev/null | head -1');
return $result->successful() && trim($result->output()) !== '';
}
}