Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Using composer.lock for LeftAndMain->CMSVersion()

  • Loading branch information...
commit 2b6d735182c323e5943365cea70eba61753c7492 1 parent e6fffb9
@chillu chillu authored
Showing with 57 additions and 9 deletions.
  1. +57 −9 admin/code/LeftAndMain.php
View
66 admin/code/LeftAndMain.php
@@ -1339,19 +1339,67 @@ public function LinkPreview() {
/**
* Return the version number of this application.
- * Uses the subversion path information in <mymodule>/silverstripe_version
- * (automacially replaced by build scripts).
- *
+ * 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 string
*/
public function CMSVersion() {
- $frameworkVersion = file_get_contents(FRAMEWORK_PATH . '/silverstripe_version');
- if(!$frameworkVersion) $frameworkVersion = _t('LeftAndMain.VersionUnknown', 'Unknown');
-
- return sprintf(
- "Framework: %s",
- $frameworkVersion
+ $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 = 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);
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.