Skip to content

Commit

Permalink
fix: add cache into the Info::version() method; move version.json i…
Browse files Browse the repository at this point in the history
…nto `resources` dir
  • Loading branch information
roxblnfk committed May 18, 2024
1 parent 46b2460 commit eb129ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
config-file: .github/.release-please-config.json
manifest-file: src/version.json
manifest-file: resources/version.json
target-branch: master

...
File renamed without changes.
34 changes: 17 additions & 17 deletions src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ class Info
public const TRAP_ROOT = __DIR__ . '/..';
private const VERSION = 'experimental';

private static ?string $cachedVersion = null;

/**
* Returns the version of the Trap.
*
* @return non-empty-string
*/
public static function version(): string
{
if (self::$cachedVersion !== null) {
return self::$cachedVersion;
}

$versionPath = self::TRAP_ROOT . '/src/version.json';
$versionContents = file_get_contents($versionPath);
/** @var non-empty-string|null $cache */
static $cache = null;

Check warning on line 39 in src/Info.php

View check run for this annotation

Codecov / codecov/patch

src/Info.php#L39

Added line #L39 was not covered by tests

if ($versionContents === false) {
self::$cachedVersion = self::VERSION;
return self::$cachedVersion;
if ($cache !== null) {
return $cache;

Check warning on line 42 in src/Info.php

View check run for this annotation

Codecov / codecov/patch

src/Info.php#L41-L42

Added lines #L41 - L42 were not covered by tests
}

$versionData = json_decode($versionContents, true);
$fileContent = \file_get_contents(self::TRAP_ROOT . '/resources/version.json');

Check warning on line 45 in src/Info.php

View check run for this annotation

Codecov / codecov/patch

src/Info.php#L45

Added line #L45 was not covered by tests

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

Check warning on line 48 in src/Info.php

View check run for this annotation

Codecov / codecov/patch

src/Info.php#L47-L48

Added lines #L47 - L48 were not covered by tests
}

self::$cachedVersion = $versionData['.'];
/** @var mixed $version */
$version = \json_decode($fileContent, true)['.'] ?? null;

Check warning on line 52 in src/Info.php

View check run for this annotation

Codecov / codecov/patch

src/Info.php#L52

Added line #L52 was not covered by tests

return self::$cachedVersion;
return $cache = \is_string($version) && $version !== ''
? $version
: self::VERSION;

Check warning on line 56 in src/Info.php

View check run for this annotation

Codecov / codecov/patch

src/Info.php#L54-L56

Added lines #L54 - L56 were not covered by tests
}
}

0 comments on commit eb129ba

Please sign in to comment.