You've already forked Atomcms-edit
0bb35d6c8a
- Create GitHubService with parseUrl, extractRepo, getBranches, getLatestCommit, getLatestRelease, hasUpdates - Replace duplicated GitHub parsing in EmulatorConfiguration with GitHubService - Replace fetchGitHubBranches, extractGitHubRepo, getEmulatorRemoteVersion in Commandocentrum - Reduce code duplication across services and controllers
179 lines
5.5 KiB
PHP
Executable File
179 lines
5.5 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Emulator;
|
|
|
|
use App\Services\GitHubService;
|
|
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 = app(GitHubService::class)->parseUrl($url);
|
|
$this->githubRepo = $parsed['repo'];
|
|
$this->githubBranch = $parsed['branch'];
|
|
}
|
|
|
|
private function parseSourceRepo(string $url): void
|
|
{
|
|
$parsed = app(GitHubService::class)->parseUrl($url);
|
|
$this->sourceRepo = $parsed['repo'];
|
|
$this->sourceBranch = $parsed['branch'];
|
|
}
|
|
|
|
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()) !== '';
|
|
}
|
|
}
|