From 1eae56521ddc9da4b9c354dd02bbe4eea119070f Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 17 Feb 2022 12:52:32 +1300 Subject: [PATCH] NEW Get the version for an individual module --- _config/cache.yml | 4 + .../01_Templates/02_Common_Variables.md | 20 ++++- docs/en/04_Changelogs/4.11.0.md | 13 ++++ src/Core/Manifest/VersionProvider.php | 75 ++++++++++++++++++- .../php/Core/Manifest/VersionProviderTest.php | 14 ++++ 5 files changed, 120 insertions(+), 6 deletions(-) diff --git a/_config/cache.yml b/_config/cache.yml index 2372735a918..b0b4f898a81 100644 --- a/_config/cache.yml +++ b/_config/cache.yml @@ -42,3 +42,7 @@ SilverStripe\Core\Injector\Injector: constructor: namespace: 'EmbedShortcodeProvider' defaultLifetime: 86400 + Psr\SimpleCache\CacheInterface.VersionProvider: + factory: SilverStripe\Core\Cache\CacheFactory + constructor: + namespace: 'VersionProvider' diff --git a/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md b/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md index 1df99e75b49..b9c855222d1 100644 --- a/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md +++ b/docs/en/02_Developer_Guides/01_Templates/02_Common_Variables.md @@ -123,18 +123,18 @@ on a per-page basis. If you don’t want to include the title tag use `$MetaTags(false)`. [/notice] -By default `$MetaTags` renders: +By default `$MetaTags` renders (assuming 4.11.0 is the current version of silverstripe/framework): ```ss Title of the Page - + ``` `$MetaTags(false)` will render ```ss - + ``` @@ -145,6 +145,20 @@ $MetaTags(false) $Title - Bob's Fantasy Football ``` +### Disabling the meta generator tag + +The meta generator tag includes the current version of `silverstripe/framework`. This version number +provides aggregate installation numbers to the product team who maintain Silverstripe CMS which is +used to make informed product decisions. + +If it's felt this creates a security concern for your website, then the version portion of the meta +generator tag can be disabled via yaml configuration: + +```yml +SilverStripe\CMS\Model\SiteTree: + show_meta_generator_version: false +``` + ### Modifying Meta Tags You can override the `MetaComponents()` method on your `SiteTree` sub-classes or make use of the `MetaComponents` extension point to manipulate the underlying data that is rendered by `$MetaTags`. Example (for `Page` class): diff --git a/docs/en/04_Changelogs/4.11.0.md b/docs/en/04_Changelogs/4.11.0.md index 6946d1175bc..79501b8e80e 100644 --- a/docs/en/04_Changelogs/4.11.0.md +++ b/docs/en/04_Changelogs/4.11.0.md @@ -21,6 +21,19 @@ In accordance with our [PHP support policy](/Getting_Started/Server_Requirements ## Features and enhancements {#features-and-enhancements} +### Meta generator tag now shows framework version number + +The meta generator tag, which can be seen in the meta tags when you view source on a regular page, now includes the framework version truncated to show just the major and minor version, e.g. 4.11. + +This version number will provide aggregate installation numbers to the product team who maintain Silverstripe CMS which will be used to make informed product decisions. + +If it's felt this creates a security concern for your website, then the version portion of the meta +generator tag can be configured off via + +```yml +SilverStripe\CMS\Model\SiteTree: + show_meta_generator_version: false +``` ## Bugfixes {#bugfixes} diff --git a/src/Core/Manifest/VersionProvider.php b/src/Core/Manifest/VersionProvider.php index c821573f946..a577436524d 100644 --- a/src/Core/Manifest/VersionProvider.php +++ b/src/Core/Manifest/VersionProvider.php @@ -2,10 +2,11 @@ namespace SilverStripe\Core\Manifest; +use InvalidArgumentException; use SilverStripe\Core\Config\Config; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\Config\Configurable; -use SilverStripe\Core\Convert; +use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; /** @@ -25,8 +26,8 @@ */ class VersionProvider { - use Configurable; + use Injectable; /** * @var array @@ -43,6 +44,11 @@ class VersionProvider */ public function getVersion() { + $key = sprintf('%s-%s', $this->getComposerLockPath(), 'all'); + $version = $this->getCachedValue($key); + if ($version) { + return $version; + } $modules = $this->getModules(); $lockModules = $this->getModuleVersionFromComposer(array_keys($modules)); $moduleVersions = []; @@ -59,7 +65,70 @@ public function getVersion() list($title, $version) = $value; $ret[] = "$title: $version"; } - return implode(', ', $ret); + $version = implode(', ', $ret); + $this->setCacheValue($key, $version); + return $version; + } + + /** + * Get the version of a specific module + * Will strip out the leading "module name" from the returned value + * + * @param string $module - e.g. silverstripe/framework + * @return string - e.g. 4.10 + */ + public function getModuleVersion(string $module): string + { + $key = sprintf('%s-%s', $this->getComposerLockPath(), $module); + $version = $this->getCachedValue($key); + if ($version) { + return $version; + } + $previousModules = $this->getModules(); + Config::modify()->set(self::class, 'modules', [$module => 'MODULE_NAME']); + $version = $this->getVersion(); + $version = str_replace('MODULE_NAME: ', '', $version); + Config::modify()->set(self::class, 'modules', $previousModules); + $this->setCacheValue($key, $version); + return $version; + } + + /** + * @return CacheInterface + */ + private function getCache(): CacheInterface + { + return Injector::inst()->get(CacheInterface::class . '.VersionProvider'); + } + + /** + * @param string $key + * + * @return string + */ + private function getCachedValue(string $key): string + { + $cache = $this->getCache(); + try { + if ($cache->has($key)) { + return $cache->get($key); + } + } catch (InvalidArgumentException $e) { + } + return ''; + } + + /** + * @param string $key + * @param string $value + */ + private function setCacheValue(string $key, string $value): void + { + $cache = $this->getCache(); + try { + $cache->set($key, $value); + } catch (InvalidArgumentException $e) { + } } /** diff --git a/tests/php/Core/Manifest/VersionProviderTest.php b/tests/php/Core/Manifest/VersionProviderTest.php index be4319a3e11..0c5d4e01022 100644 --- a/tests/php/Core/Manifest/VersionProviderTest.php +++ b/tests/php/Core/Manifest/VersionProviderTest.php @@ -188,4 +188,18 @@ public function testGetModulesFromComposerLock() $result = $mock->getVersion(); $this->assertStringContainsString('Some Package: 1.2.3', $result); } + + public function testGetModuleVersion() + { + $provider = $this->getMockProvider(__DIR__ . '/fixtures/VersionProviderTest/composer.recipe-core.testlock'); + Config::modify()->set(VersionProvider::class, 'modules', [ + 'silverstripe/framework' => 'Framework', + 'silverstripe/recipe-core' => 'Core Recipe' + ]); + $this->assertSame('1.2.3', $provider->getModuleVersion('silverstripe/framework')); + // assert that the temporary config changes in getModuleVersion() had no side-effects + $result = $provider->getVersion(); + $this->assertStringNotContainsString('Framework: 1.2.3', $result); + $this->assertStringContainsString('Core Recipe: 7.7.7', $result); + } }