NEW Get the version for an individual module

This commit is contained in:
Steve Boyd 2022-03-08 13:48:43 +13:00
parent 4f14f5c8aa
commit f488f38f7d
5 changed files with 132 additions and 7 deletions

View File

@ -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'

View File

@ -123,18 +123,18 @@ on a per-page basis.
If you dont 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>Title of the Page</title>
<meta name="generator" content="SilverStripe - https://www.silverstripe.org">
<meta name="generator" content="Silverstripe CMS 4.11">
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
```
`$MetaTags(false)` will render
```ss
<meta name="generator" content="SilverStripe - https://www.silverstripe.org">
<meta name="generator" content="Silverstripe CMS 4.11">
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
```
@ -145,6 +145,26 @@ $MetaTags(false)
<title>$Title - Bob's Fantasy Football</title>
```
### 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 you dislike this behaviour, the entire meta generator tag can be disabled via:
```yml
SilverStripe\CMS\Model\SiteTree:
meta_generator: ''
```
The version portion of the metagenerator tag can be disabled via:
```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):

View File

@ -31,12 +31,31 @@ This can be used to allow content authors to see the content they are creating i
The [Preview Documentation](https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/preview/) has been updated with code examples which show how to enable CMS preview on `DataObject`s in a couple of different scenarios.
### 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 you dislike this behaviour, the entire meta generator tag can be disabled via:
```yml
SilverStripe\CMS\Model\SiteTree:
meta_generator: ''
```
The version portion of the metagenerator tag can be disabled via:
```yml
SilverStripe\CMS\Model\SiteTree:
show_meta_generator_version: false
```
### Other new features {#other-features}
- A new [AbstractGridFieldComponent](https://api.silverstripe.org/4/SilverStripe/Forms/GridField/AbstractGridFieldComponent.html) class has been added to make it easier to globally add fundamental functionality to `GridFieldComponent`s. All classes packaged with the Silverstripe framework which implement the `GridFieldComponent` interface are subclasses of the new abstract class, making them all `Injectable`. Maintainers of third-party packages which include classes that implement `GridFieldComponent` are encouraged to subclass the `AbstractGridFieldComponent` abstract class.
- New options have been added to the [dnadesign/silverstripe-elemental module](https://github.com/silverstripe/silverstripe-elemental) to control what content is indexed for searching elemental blocks. see [the documentation](https://github.com/silverstripe/silverstripe-elemental/blob/4/docs/en/searching-blocks.md) for details.
## Bugfixes {#bugfixes}
This release includes a number of bug fixes to improve a broad range of areas. Check the change logs for full details of these fixes split by module. Thank you to the community members that helped contribute these fixes as part of the release!

View File

@ -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 @@ use SilverStripe\Core\Injector\Injector;
*/
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,69 @@ class VersionProvider
list($title, $version) = $value;
$ret[] = "$title: $version";
}
return implode(', ', $ret);
$version = implode(', ', $ret);
if ($version) {
$this->setCacheValue($key, $version);
}
return $version;
}
/**
* Get the version of a specific module
*
* @param string $module - e.g. silverstripe/framework
* @return string - e.g. 4.11
*/
public function getModuleVersion(string $module): string
{
$key = sprintf('%s-%s', $this->getComposerLockPath(), $module);
$version = $this->getCachedValue($key);
if ($version) {
return $version;
}
$version = $this->getModuleVersionFromComposer([$module])[$module] ?? '';
if ($version) {
$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) {
}
}
/**

View File

@ -188,4 +188,18 @@ class VersionProviderTest extends SapphireTest
$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);
}
}