feat: expose module version

This commit is contained in:
2025-12-04 22:07:41 +03:00
parent 44d2af3b30
commit f1a39eeb00
5 changed files with 86 additions and 59 deletions

View File

@@ -47,5 +47,6 @@ class JsonResponse extends Response
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Allow-Credentials: true');
header('X-TELECART-VERSION: ' . module_version());
}
}

View File

@@ -101,3 +101,32 @@ if (! function_exists('env')) {
return $value;
}
}
if (! function_exists('module_version')) {
function module_version(): string
{
static $version = null;
if ($version !== null) {
return $version;
}
$versionFile = BP_BASE_PATH . '/version.txt';
if (is_readable($versionFile)) {
$raw = trim((string) file_get_contents($versionFile));
if ($raw !== '') {
return $version = $raw;
}
}
$composerFile = BP_BASE_PATH . '/composer.json';
if (is_readable($composerFile)) {
$composer = json_decode((string) file_get_contents($composerFile), true);
if (is_array($composer) && ! empty($composer['version'])) {
return $version = (string) $composer['version'];
}
}
return $version = 'unknown';
}
}

View File

@@ -106,7 +106,7 @@ class TeleCartPulseService
$botName = $me['username'] ?? 'unknown';
$moduleVersion = $this->getModuleVersion();
$moduleVersion = module_version();
$payload = [
'event' => 'HEARTBEAT',
@@ -184,7 +184,7 @@ class TeleCartPulseService
'timeout' => env('PULSE_TIMEOUT', 5.0),
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey,
'X-TELECART-VERSION' => $this->getModuleVersion(),
'X-TELECART-VERSION' => module_version(),
],
]);
@@ -202,35 +202,13 @@ class TeleCartPulseService
'base_uri' => $baseUri,
'timeout' => env('PULSE_TIMEOUT', 2.0),
'headers' => [
'X-TELECART-VERSION' => $this->getModuleVersion(),
'X-TELECART-VERSION' => module_version(),
],
]);
$client->post('heartbeat', compact('json'));
}
private function getModuleVersion(): string
{
if ($this->moduleVersion !== null) {
return $this->moduleVersion;
}
$moduleVersion = 'unknown';
$composerPath = __DIR__ . '/../../composer.json';
if (file_exists($composerPath)) {
$composerRaw = @file_get_contents($composerPath);
if ($composerRaw !== false) {
$composer = json_decode($composerRaw, true);
if (is_array($composer) && isset($composer['version'])) {
$moduleVersion = (string)$composer['version'];
}
}
}
return $this->moduleVersion = $moduleVersion;
}
private function handleOrderCreated(array $data, array $deserialized): void
{
if (isset($deserialized['campaign_id'], $deserialized['tracking_id'])) {

View File

@@ -0,0 +1 @@
v2.0.0