X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FExtension%2FMapper.php;h=3e0c9dd14e4b7dc190becc1a83d8124a807aa9b9;hb=2fa5a830869672f79de1304ff1ea6169ccfcd27f;hp=ce50f74495d3a000f28f9cadcaf7fc39c13acb4a;hpb=4a00ebd175e4819197ddec91312675c58b25096a;p=civicrm-core.git diff --git a/CRM/Extension/Mapper.php b/CRM/Extension/Mapper.php index ce50f74495..3e0c9dd14e 100644 --- a/CRM/Extension/Mapper.php +++ b/CRM/Extension/Mapper.php @@ -66,6 +66,12 @@ class CRM_Extension_Mapper { protected $civicrmUrl; + /** + * @var array + * Array(string $extKey => CRM_Extension_Upgrader_Interface $upgrader) + */ + protected $upgraders = []; + /** * @param CRM_Extension_Container_Interface $container * @param CRM_Utils_Cache_Interface $cache @@ -535,4 +541,56 @@ class CRM_Extension_Mapper { CRM_Extension_System::singleton()->getClassLoader()->refresh(); } + /** + * This returns a formatted string containing an extension upgrade link for the UI. + * @todo We should improve this to return more appropriate text. eg. when an extension is not installed + * it should not say "version xx is installed". + * + * @param array $remoteExtensionInfo + * @param array $localExtensionInfo + * + * @return string + */ + public function getUpgradeLink($remoteExtensionInfo, $localExtensionInfo) { + if (!empty($remoteExtensionInfo) && version_compare($localExtensionInfo['version'], $remoteExtensionInfo->version, '<')) { + return ts('Version %1 is installed. Upgrade to version %3.', [ + 1 => $localExtensionInfo['version'], + 2 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', "action=update&id={$localExtensionInfo['key']}&key={$localExtensionInfo['key']}") . '"', + 3 => $remoteExtensionInfo->version, + ]); + } + } + + /** + * @param string $key + * Long name of the extension. + * Ex: 'org.example.myext' + * + * @return \CRM_Extension_Upgrader_Interface + */ + public function getUpgrader(string $key) { + if (!array_key_exists($key, $this->upgraders)) { + $this->upgraders[$key] = NULL; + + try { + $info = $this->keyToInfo($key); + } + catch (CRM_Extension_Exception_ParseException $e) { + CRM_Core_Session::setStatus(ts('Parse error in extension: %1', [ + 1 => $e->getMessage(), + ]), '', 'error'); + CRM_Core_Error::debug_log_message("Parse error in extension: " . $e->getMessage()); + return NULL; + } + + if (!empty($info->upgrader)) { + $class = $info->upgrader; + $u = new $class(); + $u->init(['key' => $key]); + $this->upgraders[$key] = $u; + } + } + return $this->upgraders[$key]; + } + }