Merge pull request #21852 from demeritcowboy/extension-name
[civicrm-core.git] / CRM / Extension / Mapper.php
index 59caf165e00925f38ee63606941722198ef46f64..3e0c9dd14e4b7dc190becc1a83d8124a807aa9b9 100644 (file)
@@ -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
@@ -555,4 +561,36 @@ class CRM_Extension_Mapper {
     }
   }
 
+  /**
+   * @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];
+  }
+
 }