Skip to content

Commit

Permalink
Using composer.lock for LeftAndMain->CMSVersion()
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Feb 28, 2013
1 parent e6fffb9 commit 2b6d735
Showing 1 changed file with 57 additions and 9 deletions.
66 changes: 57 additions & 9 deletions admin/code/LeftAndMain.php
Expand Up @@ -1339,19 +1339,67 @@ public function LinkPreview() {


/** /**
* 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);
} }


/** /**
Expand Down

0 comments on commit 2b6d735

Please sign in to comment.