Files
panel-pelican-dev/app/Services/Helpers/SoftwareVersionService.php

101 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2017-08-23 21:34:11 -05:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Services\Helpers;
2017-08-23 21:34:11 -05:00
use Exception;
use Illuminate\Support\Facades\Http;
2017-08-23 21:34:11 -05:00
class SoftwareVersionService
{
public function latestPanelVersionChangelog(): string
{
$key = 'panel:latest_version_changelog';
if (cache()->get($key) === 'error') {
cache()->forget($key);
}
return cache()->remember($key, now()->addMinutes(config('panel.cdn.cache_time', 60)), function () {
try {
$response = Http::timeout(5)->connectTimeout(1)->get('https://api.github.com/repos/pelican-dev/panel/releases/latest')->throw()->json();
return $response['body'];
} catch (Exception) {
return 'error';
}
});
}
public function latestPanelVersion(): string
2017-08-23 21:34:11 -05:00
{
$key = 'panel:latest_version';
if (cache()->get($key) === 'error') {
cache()->forget($key);
}
return cache()->remember($key, now()->addMinutes(config('panel.cdn.cache_time', 60)), function () {
try {
$response = Http::timeout(5)->connectTimeout(1)->get('https://api.github.com/repos/pelican-dev/panel/releases/latest')->throw()->json();
2017-08-23 21:34:11 -05:00
return trim($response['tag_name'], 'v');
} catch (Exception) {
return 'error';
}
});
2017-08-23 21:34:11 -05:00
}
public function latestWingsVersion(): string
2017-08-23 21:34:11 -05:00
{
$key = 'wings:latest_version';
if (cache()->get($key) === 'error') {
cache()->forget($key);
}
return cache()->remember($key, now()->addMinutes(config('panel.cdn.cache_time', 60)), function () {
try {
$response = Http::timeout(5)->connectTimeout(1)->get('https://api.github.com/repos/pelican-dev/wings/releases/latest')->throw()->json();
2017-08-23 21:34:11 -05:00
return trim($response['tag_name'], 'v');
} catch (Exception) {
return 'error';
}
});
}
public function isLatestPanel(): bool
2017-08-23 21:34:11 -05:00
{
if (config('app.version') === 'canary') {
2017-08-23 21:34:11 -05:00
return true;
}
return version_compare(config('app.version'), $this->latestPanelVersion()) >= 0;
2017-08-23 21:34:11 -05:00
}
public function isLatestWings(string $version): bool
2017-08-23 21:34:11 -05:00
{
if ($version === 'develop') {
2017-08-23 21:34:11 -05:00
return true;
}
return version_compare($version, $this->latestWingsVersion()) >= 0;
2017-08-23 21:34:11 -05:00
}
public function currentPanelVersion(): string
2017-08-23 21:34:11 -05:00
{
return cache()->remember('panel:current_version', now()->addMinutes(5), function () {
if (file_exists(base_path('.git/HEAD'))) {
$head = explode(' ', file_get_contents(base_path('.git/HEAD')));
if (array_key_exists(1, $head)) {
$path = base_path('.git/' . trim($head[1]));
if (file_exists($path)) {
return 'canary (' . substr(file_get_contents($path), 0, 7) . ')';
}
}
}
return config('app.version');
});
2024-03-12 22:39:16 -04:00
}
2017-08-23 21:34:11 -05:00
}