Civi::dispatcher() - Extend protection against pre-boot hooks
authorTim Otten <totten@civicrm.org>
Wed, 15 Jul 2020 03:38:09 +0000 (20:38 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 15 Jul 2020 06:41:27 +0000 (23:41 -0700)
Before: The protection extends up until `createLockManager()`

After: The protection extends up until `CRM_Extension_System...->register()`

Civi/Core/Container.php

index c8a107014b7365e3316cfc8edb148decc5c6a833..1c3374f98bbc936ccdb31e2ecba936681fe14d84 100644 (file)
@@ -499,10 +499,15 @@ class Container {
     $bootServices['paths'] = new \Civi\Core\Paths();
 
     $bootServices['dispatcher.boot'] = new CiviEventDispatcher();
-    $bootServices['dispatcher.boot']->setDispatchPolicy([
-      // Quality control: during pre-boot, we can register hook listeners - but not dispatch them.
-      '/./' => 'not-ready',
-    ]);
+
+    // Quality control: There should be no pre-boot hooks because they make it harder to understand/support/refactor.
+    // If a pre-boot hook sneaks in, we'll raise an error.
+    $bootDispatchPolicy = [
+      '/^hook_/' => 'not-ready',
+      '/^civi\./' => 'run',
+    ];
+    $mainDispatchPolicy = \CRM_Core_Config::isUpgradeMode() ? \CRM_Upgrade_DispatchPolicy::get('upgrade.main') : NULL;
+    $bootServices['dispatcher.boot']->setDispatchPolicy($bootDispatchPolicy);
 
     $class = $runtime->userFrameworkClass;
     $bootServices['userSystem'] = $userSystem = new $class();
@@ -520,13 +525,12 @@ class Container {
 
     $bootServices['lockManager'] = self::createLockManager();
 
-    $bootServices['dispatcher.boot']->setDispatchPolicy(\CRM_Core_Config::isUpgradeMode() ? \CRM_Upgrade_DispatchPolicy::get('upgrade.main') : NULL);
-
     if ($loadFromDB && $runtime->dsn) {
       \CRM_Core_DAO::init($runtime->dsn);
       \CRM_Utils_Hook::singleton(TRUE);
       \CRM_Extension_System::singleton(TRUE);
       \CRM_Extension_System::singleton(TRUE)->getClassLoader()->register();
+      $bootServices['dispatcher.boot']->setDispatchPolicy($mainDispatchPolicy);
 
       $runtime->includeCustomPath();
 
@@ -537,6 +541,9 @@ class Container {
       }
       \Civi::$statics[__CLASS__]['container'] = $container;
     }
+    else {
+      $bootServices['dispatcher.boot']->setDispatchPolicy($mainDispatchPolicy);
+    }
   }
 
   public static function getBootService($name) {