Skip to content

Commit fc8a8c0

Browse files
authored
[Feature] Added system checker for local and remote versions (alexjustesen#979)
1 parent e3831c0 commit fc8a8c0

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

app/Services/SystemChecker.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Illuminate\Support\Facades\Cache;
6+
7+
/**
8+
* 🤔 inspired by: https://github.com/ploi/roadmap/blob/main/app/Services/SystemChecker.php
9+
*/
10+
class SystemChecker
11+
{
12+
public $localVersion;
13+
14+
public $remoteVersion;
15+
16+
public string $cacheKeyLocal = 'speedtest-local-version';
17+
18+
public string $cacheKeyRemote = 'speedtest-remote-version';
19+
20+
public function getVersions(): self
21+
{
22+
$this->localVersion = trim($this->getLocalVersion());
23+
24+
$this->remoteVersion = trim($this->getRemoteVersion());
25+
26+
return $this;
27+
}
28+
29+
public function getLocalVersion()
30+
{
31+
return cache()->remember($this->cacheKeyLocal, now()->addDay(), function () {
32+
return shell_exec('git describe --tag --abbrev=0');
33+
});
34+
}
35+
36+
public function getRemoteVersion()
37+
{
38+
return cache()->remember($this->cacheKeyRemote, now()->addDay(), function () {
39+
return shell_exec('curl https://api.github.com/repos/alexjustesen/speedtest-tracker/releases/latest -s | grep \'"tag_name":\' | sed -E \'s/.*"([^"]+)".*/\1/\'');
40+
});
41+
}
42+
43+
public function isOutOfDate()
44+
{
45+
$this->getVersions();
46+
47+
return $this->localVersion < $this->remoteVersion || $this->localVersion != $this->remoteVersion;
48+
}
49+
50+
public function flushVersionData()
51+
{
52+
try {
53+
Cache::forget($this->cacheKeyLocal);
54+
55+
Cache::forget($this->cacheKeyRemote);
56+
} catch (\Exception $exception) {
57+
// fail silently, it's ok
58+
}
59+
}
60+
61+
public function getPhpVersion(): string
62+
{
63+
return phpversion();
64+
}
65+
}

0 commit comments

Comments
 (0)