From 46b2460edef7f6239df6d6b2e00b49e82b1d5cdd Mon Sep 17 00:00:00 2001 From: lotyp Date: Fri, 10 May 2024 18:38:34 +0300 Subject: [PATCH 1/3] fix: cache version variable --- src/Info.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Info.php b/src/Info.php index ee97a046..24c5b85b 100644 --- a/src/Info.php +++ b/src/Info.php @@ -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; } } From eb129ba460c95755302890ae910f4649fb27d11b Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sun, 19 May 2024 00:52:50 +0400 Subject: [PATCH 2/3] fix: add cache into the `Info::version()` method; move version.json into `resources` dir --- .github/workflows/create-release.yml | 2 +- {src => resources}/version.json | 0 src/Info.php | 34 ++++++++++++++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) rename {src => resources}/version.json (100%) diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index eef7915a..d6408be3 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -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 ... diff --git a/src/version.json b/resources/version.json similarity index 100% rename from src/version.json rename to resources/version.json diff --git a/src/Info.php b/src/Info.php index 24c5b85b..2decc2ab 100644 --- a/src/Info.php +++ b/src/Info.php @@ -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; - if ($versionContents === false) { - self::$cachedVersion = self::VERSION; - return self::$cachedVersion; + if ($cache !== null) { + return $cache; } - $versionData = json_decode($versionContents, true); + $fileContent = \file_get_contents(self::TRAP_ROOT . '/resources/version.json'); - if (!is_array($versionData) || !isset($versionData['.']) || !is_string($versionData['.'])) { - self::$cachedVersion = self::VERSION; - return self::$cachedVersion; + if ($fileContent === false) { + return $cache = self::VERSION; } - self::$cachedVersion = $versionData['.']; + /** @var mixed $version */ + $version = \json_decode($fileContent, true)['.'] ?? null; - return self::$cachedVersion; + return $cache = \is_string($version) && $version !== '' + ? $version + : self::VERSION; } } From 70a3c4f44ac73a293e8dd4d47abdc4a0247986bd Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Sun, 19 May 2024 01:00:47 +0400 Subject: [PATCH 3/3] test: test that `Info::version()` returns correct version --- tests/Unit/InfoTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/Unit/InfoTest.php diff --git a/tests/Unit/InfoTest.php b/tests/Unit/InfoTest.php new file mode 100644 index 00000000..ce1678db --- /dev/null +++ b/tests/Unit/InfoTest.php @@ -0,0 +1,15 @@ +