From acb56a51f06ff4753173daad95577f97930324af Mon Sep 17 00:00:00 2001 From: Neluxx Date: Wed, 28 Oct 2026 14:48:51 +0100 Subject: [PATCH 01/33] Add AppVersion class as single source of the version --- src/AppVersion.php | 84 ++++++++++++++++++ src/Middleware/AppVersionHeaderMiddleware.php | 3 +- tests/TestCase/AppVersionTest.php | 86 +++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 src/AppVersion.php create mode 100644 tests/TestCase/AppVersionTest.php diff --git a/src/AppVersion.php b/src/AppVersion.php new file mode 100644 index 0000000..ca38fa2 --- /dev/null +++ b/src/AppVersion.php @@ -0,0 +1,84 @@ +> + */ + public const DEFAULTS = [ + 'meta' => [ + 'enabled' => false, + 'name' => 'app-version', + ], + 'endpoint' => [ + 'enabled' => false, + 'path' => '/version', + 'middleware' => [], + ], + 'cli' => [ + 'enabled' => true, + 'banner' => false, + ], + ]; + + /** + * Get the configured app version. + * + * @return string The app version or "Unknown" if it is not set. + */ + public static function get(): string + { + $version = Configure::read('App.version'); + + if ($version === null || $version === '' || !is_scalar($version)) { + return self::UNKNOWN; + } + + return (string)$version; + } + + /** + * Read a plugin configuration value, falling back to the plugin default. + * + * @param string $key The key below `AppVersion`, e.g. `endpoint.path`. + * @return mixed The configured value or the default. + */ + public static function config(string $key): mixed + { + return Configure::read('AppVersion.' . $key, Hash::get(self::DEFAULTS, $key)); + } + + /** + * Check whether an output option is enabled. + * + * @param string $output The output option, one of `meta`, `endpoint` or `cli`. + * @return bool + */ + public static function isEnabled(string $output): bool + { + return (bool)self::config($output . '.enabled'); + } +} diff --git a/src/Middleware/AppVersionHeaderMiddleware.php b/src/Middleware/AppVersionHeaderMiddleware.php index b5f2660..766ad7f 100644 --- a/src/Middleware/AppVersionHeaderMiddleware.php +++ b/src/Middleware/AppVersionHeaderMiddleware.php @@ -3,6 +3,7 @@ namespace AppVersion\Middleware; +use AppVersion\AppVersion; use Cake\Core\Configure; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -35,7 +36,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $response = $handler->handle($request); $appVersionPrefix = Configure::read('App.versionPrefix', 'X'); - $appVersion = Configure::read('App.version', 'Unknown'); + $appVersion = AppVersion::get(); $response = $response->withHeader($appVersionPrefix . '-App-Version', $appVersion); diff --git a/tests/TestCase/AppVersionTest.php b/tests/TestCase/AppVersionTest.php new file mode 100644 index 0000000..211174d --- /dev/null +++ b/tests/TestCase/AppVersionTest.php @@ -0,0 +1,86 @@ + true, 'path' => '/status/version']); + + self::assertTrue(AppVersion::isEnabled('endpoint')); + self::assertSame('/status/version', AppVersion::config('endpoint.path')); + self::assertFalse(AppVersion::isEnabled('meta')); + } +} From 9a5c0bed9454adb890e2139248cd553b88a015c2 Mon Sep 17 00:00:00 2001 From: Neluxx Date: Wed, 28 Oct 2026 15:01:58 +0100 Subject: [PATCH 02/33] Add AppVersion view helper with meta tag --- src/View/Helper/AppVersionHelper.php | 54 +++++++++ .../View/Helper/AppVersionHelperTest.php | 110 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/View/Helper/AppVersionHelper.php create mode 100644 tests/TestCase/View/Helper/AppVersionHelperTest.php diff --git a/src/View/Helper/AppVersionHelper.php b/src/View/Helper/AppVersionHelper.php new file mode 100644 index 0000000..4a11248 --- /dev/null +++ b/src/View/Helper/AppVersionHelper.php @@ -0,0 +1,54 @@ + $options Options passed to `HtmlHelper::meta()` + * @return string|null The meta tag, an empty string if disabled, or null if it was appended to a block. + */ + public function meta(array $options = []): ?string + { + if (!AppVersion::isEnabled('meta')) { + return ''; + } + + $name = (string)AppVersion::config('meta.name'); + + return $this->Html->meta(['name' => $name, 'content' => $this->get()], null, $options); + } +} diff --git a/tests/TestCase/View/Helper/AppVersionHelperTest.php b/tests/TestCase/View/Helper/AppVersionHelperTest.php new file mode 100644 index 0000000..e6a18fe --- /dev/null +++ b/tests/TestCase/View/Helper/AppVersionHelperTest.php @@ -0,0 +1,110 @@ +view = new View(); + $this->AppVersion = new AppVersionHelper($this->view); + } + + /** + * Test get method + * + * @return void + * @uses AppVersionHelper::get + */ + public function testGet(): void + { + self::assertSame('1.2.3', $this->AppVersion->get()); + + Configure::delete('App.version'); + self::assertSame('Unknown', $this->AppVersion->get()); + } + + /** + * Test meta method is disabled by default + * + * @return void + * @uses AppVersionHelper::meta + */ + public function testMetaDisabledByDefault(): void + { + self::assertSame('', $this->AppVersion->meta()); + } + + /** + * Test meta method when enabled + * + * @return void + * @uses AppVersionHelper::meta + */ + public function testMeta(): void + { + Configure::write('AppVersion.meta.enabled', true); + + self::assertSame('', $this->AppVersion->meta()); + } + + /** + * Test meta method with a custom name and escaping + * + * @return void + * @uses AppVersionHelper::meta + */ + public function testMetaCustomNameAndEscaping(): void + { + Configure::write('AppVersion.meta', ['enabled' => true, 'name' => 'acme-version']); + Configure::write('App.version', '1.2.3">