Use composer metadata as primary source for version and ping build
This commit is contained in:
@@ -9,15 +9,38 @@ class PingController extends Controller
|
|||||||
{
|
{
|
||||||
public function __invoke(): JsonResponse
|
public function __invoke(): JsonResponse
|
||||||
{
|
{
|
||||||
|
$build = $this->readComposerBuild();
|
||||||
|
if ($build === null) {
|
||||||
$build = Setting::query()->where('key', 'build')->value('value');
|
$build = Setting::query()->where('key', 'build')->value('value');
|
||||||
$reportedBuild = $build !== null ? ((int) $build) + 1 : 1;
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'connect' => 'ok',
|
'connect' => 'ok',
|
||||||
'version_status' => [
|
'version_status' => [
|
||||||
'build' => $reportedBuild,
|
'build' => $build !== null ? (int) $build : null,
|
||||||
],
|
],
|
||||||
'notification_state' => false,
|
'notification_state' => false,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function readComposerBuild(): ?int
|
||||||
|
{
|
||||||
|
$path = base_path('composer.json');
|
||||||
|
if (!is_file($path) || !is_readable($path)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$raw = file_get_contents($path);
|
||||||
|
if ($raw === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = json_decode($raw, true);
|
||||||
|
if (!is_array($data)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$build = trim((string) ($data['build'] ?? ''));
|
||||||
|
return ctype_digit($build) ? (int) $build : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,39 @@ class VersionController extends Controller
|
|||||||
{
|
{
|
||||||
public function __invoke(): JsonResponse
|
public function __invoke(): JsonResponse
|
||||||
{
|
{
|
||||||
$version = Setting::where('key', 'version')->value('value');
|
$composer = $this->readComposerMetadata();
|
||||||
$build = Setting::where('key', 'build')->value('value');
|
$version = $composer['version'] ?? Setting::where('key', 'version')->value('value');
|
||||||
|
$build = $composer['build'] ?? Setting::where('key', 'build')->value('value');
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'version' => $version,
|
'version' => $version,
|
||||||
'build' => $build !== null ? (int) $build : null,
|
'build' => $build !== null ? (int) $build : null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function readComposerMetadata(): array
|
||||||
|
{
|
||||||
|
$path = base_path('composer.json');
|
||||||
|
if (!is_file($path) || !is_readable($path)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$raw = file_get_contents($path);
|
||||||
|
if ($raw === false) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = json_decode($raw, true);
|
||||||
|
if (!is_array($data)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$version = trim((string) ($data['version'] ?? ''));
|
||||||
|
$build = trim((string) ($data['build'] ?? ''));
|
||||||
|
|
||||||
|
return [
|
||||||
|
'version' => $version !== '' ? $version : null,
|
||||||
|
'build' => ctype_digit($build) ? (int) $build : null,
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Models\Setting;
|
|
||||||
|
|
||||||
it('returns ping status with build and notification state', function (): void {
|
it('returns ping status with build and notification state', function (): void {
|
||||||
Setting::updateOrCreate(['key' => 'build'], ['value' => '1337']);
|
$composer = json_decode((string) file_get_contents(base_path('composer.json')), true);
|
||||||
|
$expectedBuild = isset($composer['build']) ? (int) $composer['build'] : null;
|
||||||
|
|
||||||
$response = $this->getJson('/ping');
|
$response = $this->getJson('/ping');
|
||||||
|
|
||||||
@@ -11,7 +10,7 @@ it('returns ping status with build and notification state', function (): void {
|
|||||||
$response->assertJson([
|
$response->assertJson([
|
||||||
'connect' => 'ok',
|
'connect' => 'ok',
|
||||||
'version_status' => [
|
'version_status' => [
|
||||||
'build' => 1337,
|
'build' => $expectedBuild,
|
||||||
],
|
],
|
||||||
'notification_state' => false,
|
'notification_state' => false,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Models\Setting;
|
it('returns version and build info from composer metadata', function (): void {
|
||||||
|
$composer = json_decode((string) file_get_contents(base_path('composer.json')), true);
|
||||||
it('returns version and build info', function (): void {
|
$expectedVersion = (string) ($composer['version'] ?? '');
|
||||||
Setting::updateOrCreate(['key' => 'version'], ['value' => '1.2.3']);
|
$expectedBuild = isset($composer['build']) ? (int) $composer['build'] : null;
|
||||||
Setting::updateOrCreate(['key' => 'build'], ['value' => '42']);
|
|
||||||
|
|
||||||
$response = $this->getJson('/api/version');
|
$response = $this->getJson('/api/version');
|
||||||
|
|
||||||
$response->assertOk();
|
$response->assertOk();
|
||||||
$response->assertJsonFragment([
|
$response->assertJsonFragment([
|
||||||
'version' => '1.2.3',
|
'version' => $expectedVersion,
|
||||||
'build' => 42,
|
'build' => $expectedBuild,
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user