Extensions - Setup <classloader> during "enable" and "uninstall"
authorTim Otten <totten@civicrm.org>
Wed, 21 Apr 2021 16:43:56 +0000 (09:43 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 21 Apr 2021 16:56:30 +0000 (09:56 -0700)
This is an analog/follow-up to https://github.com/civicrm/civicrm-core/pull/20091

Why is this necessary? Recall that a typical admin will go through this lifecycle:

1. Enable extension $x
2. Disable extension $x
3. Either:
   * (a) Re-enable extension $x
   * (b) Uninstall extension $x

Step `#2` disables the classloader for purposes of regular page-loading. However, when you
get to step `#3a` or `#3b`, then you need the classloader again.

CRM/Extension/Manager/Module.php

index b66f633c31a247bf738556b9ab9ebb9e093620e7..f4ce5ceb78634a1aa16be3213c24c42a32649bd1 100644 (file)
@@ -29,7 +29,7 @@ class CRM_Extension_Manager_Module extends CRM_Extension_Manager_Base {
    * @param CRM_Extension_Info $info
    */
   public function onPreInstall(CRM_Extension_Info $info) {
-    CRM_Extension_System::singleton()->getClassLoader()->installExtension($info, dirname($this->mapper->keyToPath($info->key)));
+    $this->registerClassloader($info);
     $this->callHook($info, 'install');
     $this->callHook($info, 'enable');
   }
@@ -71,6 +71,7 @@ class CRM_Extension_Manager_Module extends CRM_Extension_Manager_Base {
    * @return bool
    */
   public function onPreUninstall(CRM_Extension_Info $info) {
+    $this->registerClassloader($info);
     $this->callHook($info, 'uninstall');
     return TRUE;
   }
@@ -92,7 +93,15 @@ class CRM_Extension_Manager_Module extends CRM_Extension_Manager_Base {
    * @param CRM_Extension_Info $info
    */
   public function onPreEnable(CRM_Extension_Info $info) {
+    $this->registerClassloader($info);
     $this->callHook($info, 'enable');
   }
 
+  /**
+   * @param CRM_Extension_Info $info
+   */
+  private function registerClassloader($info) {
+    CRM_Extension_System::singleton()->getClassLoader()->installExtension($info, dirname($this->mapper->keyToPath($info->key)));
+  }
+
 }