Using composer.lock for LeftAndMain->CMSVersion()

See https://github.com/silverstripe/silverstripe-cms/pull/289 for context
This commit is contained in:
Ingo Schommer 2013-02-28 13:38:19 +01:00
parent e6fffb9ef9
commit 2b6d735182

View File

@ -1339,19 +1339,67 @@ class LeftAndMain extends Controller implements PermissionProvider {
/** /**
* Return the version number of this application. * Return the version number of this application.
* Uses the subversion path information in <mymodule>/silverstripe_version * Uses the number in <mymodule>/silverstripe_version
* (automacially replaced by build scripts). * (automatically replaced by build scripts).
* * If silverstripe_version is empty,
* then attempts to get it from composer.lock
*
* @return string * @return string
*/ */
public function CMSVersion() { public function CMSVersion() {
$frameworkVersion = file_get_contents(FRAMEWORK_PATH . '/silverstripe_version'); $versions = array();
if(!$frameworkVersion) $frameworkVersion = _t('LeftAndMain.VersionUnknown', 'Unknown'); $modules = array(
'silverstripe/framework' => array(
return sprintf( 'title' => 'Framework',
"Framework: %s", 'versionFile' => FRAMEWORK_PATH . '/silverstripe_version',
$frameworkVersion )
); );
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 = 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);
} }
/** /**