Skip to content

Commit

Permalink
fix: cache version variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed May 10, 2024
1 parent 54c1e51 commit 46b2460
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,31 @@ class Info
public const TRAP_ROOT = __DIR__ . '/..';
private const VERSION = 'experimental';

private static ?string $cachedVersion = null;

public static function version(): string
{
if (self::$cachedVersion !== null) {
return self::$cachedVersion;
}

$versionPath = self::TRAP_ROOT . '/src/version.json';
$versionContents = file_get_contents($versionPath);

if ($versionContents === false) {
return self::VERSION;
self::$cachedVersion = self::VERSION;
return self::$cachedVersion;
}

$versionData = json_decode($versionContents, true);

if (!is_array($versionData) || !isset($versionData['.']) || !is_string($versionData['.'])) {
return self::VERSION;
self::$cachedVersion = self::VERSION;
return self::$cachedVersion;
}

return $versionData['.'];
self::$cachedVersion = $versionData['.'];

return self::$cachedVersion;
}
}

0 comments on commit 46b2460

Please sign in to comment.