(REF) Civi\Core\Container - Extract method for overriding the active container
authorTim Otten <totten@civicrm.org>
Tue, 16 Aug 2022 21:46:05 +0000 (14:46 -0700)
committerTim Otten <totten@civicrm.org>
Wed, 17 Aug 2022 02:16:29 +0000 (19:16 -0700)
Civi/Core/Container.php

index bf7c4672ca20d36208f08e05410508a4043edde3..e0f205c0ce478fbd8fbf657a3bf8a4228f911ccc 100644 (file)
@@ -642,21 +642,30 @@ class Container {
       $runtime->includeCustomPath();
 
       $c = new self();
-      $container = $c->loadContainer();
-      foreach ($bootServices as $name => $obj) {
-        $container->set($name, $obj);
-      }
-      \Civi::$statics[__CLASS__]['container'] = $container;
-      // Ensure all container-based serivces have a chance to add their listeners.
-      // Without this, it's a matter of happenstance (dependent upon particular page-request/configuration/etc).
-      $container->get('dispatcher');
-
+      static::useContainer($c->loadContainer());
     }
     else {
       $bootServices['dispatcher.boot']->setDispatchPolicy(\CRM_Core_Config::isUpgradeMode() ? \CRM_Upgrade_DispatchPolicy::pick() : NULL);
     }
   }
 
+  /**
+   * Set the active container (over-writing the current container, if defined).
+   *
+   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
+   * @internal Intended for bootstrap and unit-testing.
+   */
+  public static function useContainer($container): void {
+    $bootServices = \Civi::$statics[__CLASS__]['boot'];
+    foreach ($bootServices as $name => $obj) {
+      $container->set($name, $obj);
+    }
+    \Civi::$statics[__CLASS__]['container'] = $container;
+    // Ensure all container-based serivces have a chance to add their listeners.
+    // Without this, it's a matter of happenstance (dependent upon particular page-request/configuration/etc).
+    $container->get('dispatcher');
+  }
+
   /**
    * @param string $name
    *