Skip to content

Commit f656b70

Browse files
author
Pete Bishop
committed
Implement GitHub class to retrieve versions
1 parent 90b5bbd commit f656b70

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

app/Providers/AppServiceProvider.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Providers;
44

5+
use App\Support\GitHub;
56
use Illuminate\Support\Facades\View;
67
use Illuminate\Support\ServiceProvider;
78

@@ -12,19 +13,20 @@ class AppServiceProvider extends ServiceProvider
1213
*/
1314
public function register(): void
1415
{
15-
$this->registerSharedViewVariables();
16+
//
1617
}
1718

1819
/**
1920
* Bootstrap any application services.
2021
*/
2122
public function boot(): void
2223
{
23-
//
24+
$this->registerSharedViewVariables();
2425
}
2526

2627
private function registerSharedViewVariables(): void
2728
{
29+
View::share('electronGitHubVersion', GitHub::electron()->latestVersion());
2830
View::share('discordLink', 'https://discord.gg/X62tWNStZK');
2931
View::share('bskyLink', 'https://bsky.app/profile/nativephp.bsky.social');
3032
View::share('openCollectiveLink', 'https://opencollective.com/nativephp');

app/Support/GitHub.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Support;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
use Illuminate\Support\Facades\Http;
7+
8+
class GitHub
9+
{
10+
public const PACKAGE_ELECTRON = 'nativephp/electron';
11+
public const PACKAGE_LARAVEL = 'nativephp/laravel';
12+
13+
public function __construct(
14+
private string $package
15+
) {}
16+
17+
public static function electron(): static
18+
{
19+
return new static(static::PACKAGE_ELECTRON);
20+
}
21+
22+
public static function laravel(): static
23+
{
24+
return new static(static::PACKAGE_LARAVEL);
25+
}
26+
27+
public function latestVersion()
28+
{
29+
$version = cache()->remember(
30+
$this->getCacheKey('latest-version'),
31+
now()->addHour(),
32+
function () {
33+
return $this->fetchLatestVersion();
34+
}
35+
);
36+
37+
return $version['name'] ?? 'Unknown';
38+
}
39+
40+
private function fetchLatestVersion()
41+
{
42+
// Make a request to GitHub
43+
$response = Http::get('https://api.github.com/repos/'.$this->package.'/releases/latest');
44+
45+
// Check if the request was successful
46+
if ($response->failed()) {
47+
return null;
48+
}
49+
50+
return $response->json();
51+
}
52+
53+
private function getCacheKey(string $string): string
54+
{
55+
return sprintf('%s-%s', $this->package, $string);
56+
}
57+
}

resources/views/components/navigation-bar.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class="2xl:max-w-8xl mx-auto flex w-full max-w-5xl items-center justify-between
2121
class="hidden rounded-full bg-gray-200/60 px-2 py-1 text-xs text-gray-600 lg:block dark:bg-[#1f2032] dark:text-white/50"
2222
aria-label="Version information"
2323
>
24-
1.0.0-beta.2
24+
{{ $electronGitHubVersion }}
2525
</div>
2626
</div>
2727

0 commit comments

Comments
 (0)