Files
Atomcms-edit/app/Services/Emulator/EmulatorStatusService.php
T
root f5666c104d 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
2026-05-19 20:20:43 +02:00

145 lines
4.7 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Services\Emulator;
use App\Services\RconService;
use Illuminate\Support\Facades\Process;
class EmulatorStatusService
{
use EmulatorConfiguration;
public function __construct()
{
$this->loadConfiguration();
}
public function getStatus(): array
{
$rconService = new RconService;
return [
'is_connected' => $rconService->isConnected(),
'service_running' => $this->isServiceRunning(),
'jar_exists' => $this->commandFileExists($this->jarPath . '/*.jar'),
'jar_files' => $this->getJarFiles(),
'source_exists' => $this->commandDirExists($this->emulatorSourcePath),
'emulator_db_connected' => $this->testEmulatorDbConnection(),
'jar_path' => $this->jarPath,
'source_path' => $this->emulatorSourcePath,
'service_name' => $this->emulatorService,
];
}
public function isServiceRunning(): bool
{
try {
$result = Process::timeout(5)->run('systemctl is-active ' . escapeshellarg((string) $this->emulatorService) . ' 2>/dev/null');
return trim($result->output()) === 'active';
} catch (\Exception) {
return false;
}
}
public function testEmulatorDbConnection(): bool
{
try {
$host = $this->settings->getOrDefault('emulator_database_host', config('database.connections.mysql.host', '127.0.0.1'));
$port = $this->settings->getOrDefault('emulator_database_port', config('database.connections.mysql.port', '3306'));
$name = $this->settings->getOrDefault('emulator_database_name', '');
$username = $this->settings->getOrDefault('emulator_database_username', '');
$password = $this->settings->getOrDefault('emulator_database_password', '');
if (empty($name) || empty($username)) {
return false;
}
$result = Process::timeout(5)->run(
'mysql -h ' . escapeshellarg((string) $host) .
' -P ' . escapeshellarg((string) $port) .
' -u ' . escapeshellarg((string) $username) .
' -p' . escapeshellarg((string) $password) .
' -e "SELECT 1" ' . escapeshellarg((string) $name) . ' 2>/dev/null | head -1',
);
return $result->successful();
} catch (\Exception) {
return false;
}
}
public function getJarFiles(): array
{
$result = Process::timeout(5)->run('ls -1 ' . escapeshellarg((string) $this->jarPath) . '/*.jar 2>/dev/null | head -5');
if ($result->successful()) {
$files = array_filter(explode("\n", trim($result->output())));
return array_map(basename(...), $files);
}
return [];
}
public function getInstalledVersion(): string
{
return setting('emulator_version', 'Onbekend');
}
public function getInstalledJar(): ?string
{
try {
$result = Process::timeout(5)->run("ls -1 {$this->jarPath}/*.jar 2>/dev/null | head -1");
if ($result->successful()) {
$jarPath = trim($result->output());
if ($jarPath !== '' && $jarPath !== '0' && str_contains($jarPath, '.jar')) {
return basename($jarPath);
}
}
} catch (\Exception) {
}
return setting('emulator_version', 'Onbekend') !== 'Onbekend'
? 'Emulator v' . setting('emulator_version')
: null;
}
public function getInstalledJarInfo(): array
{
$files = [];
try {
$result = Process::timeout(5)->run("ls -1lh {$this->jarPath}/*.jar 2>/dev/null");
if ($result->successful()) {
$lines = array_filter(explode("\n", trim($result->output())));
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line === '0') {
continue;
}
$parts = preg_split('/\s+/', $line);
$filename = end($parts);
if (str_contains((string) $filename, '.jar')) {
$size = $parts[4] ?? '?';
$files[] = [
'name' => basename((string) $filename),
'size' => $size,
];
}
}
}
} catch (\Exception) {
}
return $files;
}
public function isConfigured(): bool
{
return ! in_array($this->githubUrl, [null, '', '0'], true) || ! in_array($this->jarDirectUrl, [null, '', '0'], true);
}
}