mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #7125 from robbieaverill/pulls/3.7/version-provider
NEW Add new SilverStripeVersionProvider to provider module versions
This commit is contained in:
commit
ddd1584e44
@ -17,3 +17,9 @@ HTTP:
|
||||
must-revalidate: "true"
|
||||
no-transform: "true"
|
||||
vary: "Cookie, X-Forwarded-Protocol, User-Agent, Accept"
|
||||
LeftAndMain:
|
||||
dependencies:
|
||||
versionProvider: %$SilverStripeVersionProvider
|
||||
SilverStripeVersionProvider:
|
||||
modules:
|
||||
silverstripe/framework: Framework
|
||||
|
@ -177,6 +177,11 @@ class LeftAndMain extends Controller implements PermissionProvider {
|
||||
*/
|
||||
protected $responseNegotiator;
|
||||
|
||||
/**
|
||||
* @var SilverStripeVersionProvider
|
||||
*/
|
||||
protected $versionProvider;
|
||||
|
||||
/**
|
||||
* @param Member $member
|
||||
* @return boolean
|
||||
@ -1565,72 +1570,13 @@ class LeftAndMain extends Controller implements PermissionProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the version number of this application.
|
||||
* Uses the number in <mymodule>/silverstripe_version
|
||||
* (automatically replaced by build scripts).
|
||||
* If silverstripe_version is empty,
|
||||
* then attempts to get it from composer.lock
|
||||
* Return the version number of the core packages making up this application,
|
||||
* using the {@link SilverStripeVersionProvider}.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function CMSVersion() {
|
||||
$versions = array();
|
||||
$modules = array(
|
||||
'silverstripe/framework' => array(
|
||||
'title' => 'Framework',
|
||||
'versionFile' => FRAMEWORK_PATH . '/silverstripe_version',
|
||||
)
|
||||
);
|
||||
if(defined('CMS_PATH')) {
|
||||
$modules['silverstripe/cms'] = array(
|
||||
'title' => 'CMS',
|
||||
'versionFile' => CMS_PATH . '/silverstripe_version',
|
||||
);
|
||||
}
|
||||
|
||||
// Tries to obtain version number from composer.lock if it exists
|
||||
$composerLockPath = BASE_PATH . '/composer.lock';
|
||||
if (file_exists($composerLockPath)) {
|
||||
$cache = SS_Cache::factory('LeftAndMain_CMSVersion');
|
||||
$cacheKey = filemtime($composerLockPath);
|
||||
$versions = $cache->load($cacheKey);
|
||||
if($versions) {
|
||||
$versions = json_decode($versions, true);
|
||||
} else {
|
||||
$versions = array();
|
||||
}
|
||||
if(!$versions && $jsonData = file_get_contents($composerLockPath)) {
|
||||
$lockData = json_decode($jsonData);
|
||||
if($lockData && isset($lockData->packages)) {
|
||||
foreach ($lockData->packages as $package) {
|
||||
if(
|
||||
array_key_exists($package->name, $modules)
|
||||
&& isset($package->version)
|
||||
) {
|
||||
$versions[$package->name] = $package->version;
|
||||
}
|
||||
}
|
||||
$cache->save(json_encode($versions), $cacheKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to static version file
|
||||
foreach($modules as $moduleName => $moduleSpec) {
|
||||
if(!isset($versions[$moduleName])) {
|
||||
if($staticVersion = file_get_contents($moduleSpec['versionFile'])) {
|
||||
$versions[$moduleName] = $staticVersion;
|
||||
} else {
|
||||
$versions[$moduleName] = _t('LeftAndMain.VersionUnknown', 'Unknown');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$out = array();
|
||||
foreach($modules as $moduleName => $moduleSpec) {
|
||||
$out[] = $modules[$moduleName]['title'] . ': ' . $versions[$moduleName];
|
||||
}
|
||||
return implode(', ', $out);
|
||||
return $this->versionProvider->getVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
|
103
core/manifest/SilverStripeVersionProvider.php
Normal file
103
core/manifest/SilverStripeVersionProvider.php
Normal file
@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* The version provider will look up configured modules and examine the composer.lock file
|
||||
* to find the current version installed for each. This is used for the logo title in the CMS
|
||||
* via {@link LeftAndMain::CMSVersion()}
|
||||
*
|
||||
* Example configuration:
|
||||
*
|
||||
* <code>
|
||||
* SilverStripeVersionProvider:
|
||||
* modules:
|
||||
* # package/name: Package Title
|
||||
* silverstripe/framework: Framework
|
||||
* silverstripe/cms: CMS
|
||||
* </code>
|
||||
*/
|
||||
class SilverStripeVersionProvider
|
||||
{
|
||||
/**
|
||||
* Gets a comma delimited string of package titles and versions
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion()
|
||||
{
|
||||
$modules = $this->getModules();
|
||||
$lockModules = $this->getModuleVersionFromComposer(array_keys($modules));
|
||||
$output = array();
|
||||
foreach ($modules as $module => $title) {
|
||||
$version = isset($lockModules[$module])
|
||||
? $lockModules[$module]
|
||||
: _t('SilverStripeVersionProvider.VERSIONUNKNOWN', 'Unknown');
|
||||
$output[] = $title . ': ' . $version;
|
||||
}
|
||||
return implode(', ', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the configured core modules to use for the SilverStripe application version. Filtering
|
||||
* is used to ensure that modules can turn the result off for other modules, e.g. CMS can disable Framework.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getModules()
|
||||
{
|
||||
$modules = Config::inst()->get(get_class($this), 'modules');
|
||||
return array_filter($modules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to obtain version number from composer.lock if it exists
|
||||
*
|
||||
* @param array $modules
|
||||
* @return array
|
||||
*/
|
||||
public function getModuleVersionFromComposer($modules = array())
|
||||
{
|
||||
$versions = array();
|
||||
$lockData = $this->getComposerLock();
|
||||
if ($lockData && !empty($lockData['packages'])) {
|
||||
foreach ($lockData['packages'] as $package) {
|
||||
if (in_array($package['name'], $modules) && isset($package['version'])) {
|
||||
$versions[$package['name']] = $package['version'];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $versions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load composer.lock's contents and return it
|
||||
*
|
||||
* @param bool $cache
|
||||
* @return array
|
||||
*/
|
||||
protected function getComposerLock($cache = true)
|
||||
{
|
||||
$composerLockPath = BASE_PATH . '/composer.lock';
|
||||
if (!file_exists($composerLockPath)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$lockData = array();
|
||||
if ($cache) {
|
||||
$cache = SS_Cache::factory('SilverStripeVersionProvider_composerlock');
|
||||
$cacheKey = filemtime($composerLockPath);
|
||||
if ($versions = $cache->load($cacheKey)) {
|
||||
$lockData = json_decode($versions, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($lockData) && $jsonData = file_get_contents($composerLockPath)) {
|
||||
$lockData = json_decode($jsonData, true);
|
||||
|
||||
if ($cache) {
|
||||
$cache->save(json_encode($lockData), $cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
return $lockData;
|
||||
}
|
||||
}
|
@ -444,7 +444,6 @@ en:
|
||||
ShowAsList: 'show as list'
|
||||
TooManyPages: 'Too many pages'
|
||||
ValidationError: 'Validation error'
|
||||
VersionUnknown: unknown
|
||||
LeftAndMain_Menu_ss:
|
||||
Hello: Hi
|
||||
LOGOUT: 'Log out'
|
||||
@ -657,6 +656,8 @@ en:
|
||||
Tablet: Tablet
|
||||
ViewDeviceWidth: 'Select a preview width'
|
||||
Width: width
|
||||
SilverStripeVersionProvider:
|
||||
VERSIONUNKNOWN: Unknown
|
||||
SilverStripe\Admin\CMSProfileController:
|
||||
MENUTITLE: 'My Profile'
|
||||
SilverStripe\Admin\CampaignAdmin:
|
||||
|
84
tests/core/manifest/SilverStripeVersionProviderTest.php
Normal file
84
tests/core/manifest/SilverStripeVersionProviderTest.php
Normal file
@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
class SilverStripeVersionProviderTest extends SapphireTest
|
||||
{
|
||||
/**
|
||||
* @var SilverStripeVersionProvider
|
||||
*/
|
||||
protected $provider;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->provider = new SilverStripeVersionProvider;
|
||||
}
|
||||
|
||||
public function testGetModules()
|
||||
{
|
||||
Config::inst()->update('SilverStripeVersionProvider', 'modules', array(
|
||||
'silverstripe/somepackage' => 'Some Package',
|
||||
'silverstripe/hidden' => '',
|
||||
'silverstripe/another' => 'Another'
|
||||
));
|
||||
|
||||
$result = $this->provider->getModules();
|
||||
$this->assertArrayHasKey('silverstripe/somepackage', $result);
|
||||
$this->assertSame('Some Package', $result['silverstripe/somepackage']);
|
||||
$this->assertArrayHasKey('silverstripe/another', $result);
|
||||
$this->assertArrayNotHasKey('silverstripe/hidden', $result);
|
||||
}
|
||||
|
||||
public function testGetModuleVersionFromComposer()
|
||||
{
|
||||
Config::inst()->update('SilverStripeVersionProvider', 'modules', array(
|
||||
'silverstripe/framework' => 'Framework',
|
||||
'silverstripe/siteconfig' => 'SiteConfig'
|
||||
));
|
||||
|
||||
$result = $this->provider->getModules(array('silverstripe/framework'));
|
||||
$this->assertArrayHasKey('silverstripe/framework', $result);
|
||||
$this->assertNotEmpty($result['silverstripe/framework']);
|
||||
}
|
||||
|
||||
public function testGetVersion()
|
||||
{
|
||||
Config::inst()->update('SilverStripeVersionProvider', 'modules', array(
|
||||
'silverstripe/framework' => 'Framework',
|
||||
'silverstripe/siteconfig' => 'SiteConfig'
|
||||
));
|
||||
|
||||
$result = $this->provider->getVersion();
|
||||
$this->assertContains('SiteConfig: ', $result);
|
||||
$this->assertContains('Framework: ', $result);
|
||||
$this->assertContains(', ', $result);
|
||||
}
|
||||
|
||||
public function testGetModulesFromComposerLock()
|
||||
{
|
||||
$mock = $this->getMockBuilder('SilverStripeVersionProvider')
|
||||
->setMethods(array('getComposerLock'))
|
||||
->getMock();
|
||||
|
||||
$mock->expects($this->once())
|
||||
->method('getComposerLock')
|
||||
->will($this->returnValue(array(
|
||||
'packages' => array(
|
||||
array(
|
||||
'name' => 'silverstripe/somepackage',
|
||||
'version' => '1.2.3'
|
||||
),
|
||||
array(
|
||||
'name' => 'silverstripe/another',
|
||||
'version' => '2.3.4'
|
||||
)
|
||||
)
|
||||
)));
|
||||
|
||||
Config::inst()->update('SilverStripeVersionProvider', 'modules', array(
|
||||
'silverstripe/somepackage' => 'Some Package'
|
||||
));
|
||||
|
||||
$result = $mock->getVersion();
|
||||
$this->assertContains('Some Package: 1.2.3', $result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user