Construct runtime+extensions+container through boot services
authorTim Otten <totten@civicrm.org>
Sun, 20 Sep 2015 23:17:06 +0000 (16:17 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 21 Sep 2015 05:35:16 +0000 (22:35 -0700)
CRM/Core/Config.php
CRM/Core/Config/MagicMerge.php
CRM/Core/Config/Runtime.php
Civi.php
Civi/Core/Container.php

index 3e40e9ff9d841fd61c331ebe5dc830775326bb30..e4313de4b745d2013e1bb0b7957b442d7cc23e3d 100644 (file)
@@ -87,15 +87,8 @@ class CRM_Core_Config extends CRM_Core_Config_MagicMerge {
       }
 
       self::$_singleton = new CRM_Core_Config();
-      self::$_singleton->getRuntime()->initialize($loadFromDB);
-      if ($loadFromDB && self::$_singleton->getRuntime()->dsn) {
-        CRM_Core_DAO::init(self::$_singleton->getRuntime()->dsn);
-      }
-      \Civi\Core\Container::getBootServices();
-      if ($loadFromDB && self::$_singleton->getRuntime()->dsn) {
-        CRM_Extension_System::singleton();
-        \Civi\Core\Container::singleton();
-
+      \Civi\Core\Container::boot($loadFromDB);
+      if ($loadFromDB && self::$_singleton->dsn) {
         $domain = \CRM_Core_BAO_Domain::getDomain();
         \CRM_Core_BAO_ConfigSetting::applyLocale(\Civi::settings($domain->id), $domain->locales);
 
index c3ab29b5860ea825e1cd2013da12d88bd8215ecd..edb2c8db1d4d7cd7ee6527c53b2ebda63a2948e5 100644 (file)
@@ -54,7 +54,7 @@ class CRM_Core_Config_MagicMerge {
    */
   private $map;
 
-  private $runtime, $locals, $settings;
+  private $locals, $settings;
 
   private $cache = array();
 
@@ -103,7 +103,7 @@ class CRM_Core_Config_MagicMerge {
       'templateDir' => array('runtime'),
 
       // "boot-svc" properties are critical services needed during init.
-      // See also: Civi\Core\Container::getBootServices().
+      // See also: Civi\Core\Container::getBootService().
       'userSystem' => array('boot-svc'),
       'userPermissionClass' => array('boot-svc'),
 
@@ -241,7 +241,7 @@ class CRM_Core_Config_MagicMerge {
         return $this->cache[$k];
 
       case 'runtime':
-        return $this->getRuntime()->{$name};
+        return \Civi\Core\Container::getBootService('runtime')->{$name};
 
       case 'boot-svc':
         $this->cache[$k] = \Civi\Core\Container::getBootService($name);
@@ -339,16 +339,6 @@ class CRM_Core_Config_MagicMerge {
     }
   }
 
-  /**
-   * @return CRM_Core_Config_Runtime
-   */
-  protected function getRuntime() {
-    if ($this->runtime === NULL) {
-      $this->runtime = new CRM_Core_Config_Runtime();
-    }
-    return $this->runtime;
-  }
-
   /**
    * @return \Civi\Core\SettingsBag
    */
index 5011c36743496d1ebb38e15616f7b092041cc851..28583382d31c3dac118c8764214547b3a7f3eca1 100644 (file)
@@ -135,6 +135,7 @@ class CRM_Core_Config_Runtime {
 
     $this->templateDir = array(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR);
 
+    // FIXME
     if (isset($this->customPHPPathDir) && $this->customPHPPathDir) {
       set_include_path($this->customPHPPathDir . PATH_SEPARATOR . get_include_path());
     }
index 06bfba7fad17e8a1d3ffe296109b9a97d015160f..fe16326de75dec4adc5e30a6069cdb2cd449e869 100644 (file)
--- a/Civi.php
+++ b/Civi.php
@@ -94,8 +94,7 @@ class Civi {
    */
   public static function reset() {
     self::$statics = array();
-    Civi\Core\Container::getBootServices();
-    Civi\Core\Container::singleton(TRUE);
+    Civi\Core\Container::singleton();
   }
 
   /**
index 757806aa3910f3b3ee9de9e32f9088bf5360a347..6c3f0955ba96a97eb0ec66f6eebb13b4a149062d 100644 (file)
@@ -28,22 +28,16 @@ class Container {
 
   const SELF = 'civi_container_factory';
 
-  /**
-   * @var ContainerBuilder
-   */
-  private static $singleton;
-
   /**
    * @param bool $reset
    *   Whether to forcibly rebuild the entire container.
    * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface
    */
   public static function singleton($reset = FALSE) {
-    if ($reset || self::$singleton === NULL) {
-      $c = new self();
-      self::$singleton = $c->loadContainer();
+    if ($reset || !isset(\Civi::$statics[__CLASS__]['container'])) {
+      self::boot(TRUE);
     }
-    return self::$singleton;
+    return \Civi::$statics[__CLASS__]['container'];
   }
 
   /**
@@ -175,7 +169,10 @@ class Container {
     $container->setDefinition('pear_mail', new Definition('Mail'))
       ->setFactoryClass('CRM_Utils_Mail')->setFactoryMethod('createMailer');
 
-    foreach (self::getBootServices() as $bootService => $def) {
+    if (empty(\Civi::$statics[__CLASS__]['boot'])) {
+      throw new \RuntimeException("Cannot initialize container. Boot services are undefined.");
+    }
+    foreach (\Civi::$statics[__CLASS__]['boot'] as $bootService => $def) {
       $container->setDefinition($bootService, new Definition($def['class'], array($bootService)))
         ->setFactoryClass(__CLASS__)
         ->setFactoryMethod('getBootService');
@@ -305,57 +302,70 @@ class Container {
    *
    * These are services which must be setup *before* the container can operate.
    *
-   * @return array
-   *   Ex: $result['serviceName'] = array('class' => $, 'obj' => $).
+   * @param bool $loadFromDB
    * @throws \CRM_Core_Exception
    */
-  public static function getBootServices() {
-    if (!isset(\Civi::$statics[__CLASS__])) {
-      $bootServices = array();
-      \Civi::$statics[__CLASS__] = &$bootServices;
+  public static function boot($loadFromDB) {
+    $bootServices = array();
+    \Civi::$statics[__CLASS__]['boot'] = &$bootServices;
 
-      $config = \CRM_Core_Config::singleton();
+    $bootServices['runtime'] = array(
+      'class' => 'CRM_Core_Config_Runtime',
+      'obj' => ($runtime = new \CRM_Core_Config_Runtime()),
+    );
+    $runtime->initialize($loadFromDB);
 
-      $class = $config->userFrameworkClass;
-      $userSystem = new $class();
-      $userSystem->initialize();
+    if ($loadFromDB && $runtime->dsn) {
+      \CRM_Core_DAO::init($runtime->dsn);
+    }
 
-      $userPermissionClass = 'CRM_Core_Permission_' . $config->userFramework;
+    $bootServices['paths'] = array(
+      'class' => 'Civi\Core\Paths',
+      'obj' => new \Civi\Core\Paths(),
+    );
 
-      $bootServices['paths'] = array(
-        'class' => 'Civi\Core\Paths',
-        'obj' => new \Civi\Core\Paths(),
-      );
-      $bootServices['userSystem'] = array(
-        'class' => 'CRM_Utils_Cache_Interface',
-        'obj' => $userSystem,
-      );
-      $bootServices['userPermissionClass'] = array(
-        // Ugh, silly name.
-        'class' => 'CRM_Core_Permission_Base',
-        'obj' => new $userPermissionClass(),
-      );
-      $bootServices['cache.settings'] = array(
-        'class' => 'CRM_Utils_Cache_Interface',
-        'obj' => \CRM_Utils_Cache::create(array(
-          'name' => 'settings',
-          'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
-        )),
-      );
-      $bootServices['settings_manager'] = array(
-        'class' => 'Civi\Core\SettingsManager',
-        'obj' => new \Civi\Core\SettingsManager($bootServices['cache.settings']['obj']),
-      );
-      $bootServices['lockManager'] = array(
-        'class' => 'Civi\Core\Lock\LockManager',
-        'obj' => self::createLockManager(),
-      );
+    $class = $runtime->userFrameworkClass;
+    $bootServices['userSystem'] = array(
+      'class' => 'CRM_Utils_Cache_Interface',
+      'obj' => ($userSystem = new $class()),
+    );
+    $userSystem->initialize();
+
+    $userPermissionClass = 'CRM_Core_Permission_' . $runtime->userFramework;
+    $bootServices['userPermissionClass'] = array(
+      // Ugh, silly name.
+      'class' => 'CRM_Core_Permission_Base',
+      'obj' => new $userPermissionClass(),
+    );
+
+    $bootServices['cache.settings'] = array(
+      'class' => 'CRM_Utils_Cache_Interface',
+      'obj' => \CRM_Utils_Cache::create(array(
+        'name' => 'settings',
+        'type' => array('*memory*', 'SqlGroup', 'ArrayCache'),
+      )),
+    );
+
+    $bootServices['settings_manager'] = array(
+      'class' => 'Civi\Core\SettingsManager',
+      'obj' => new \Civi\Core\SettingsManager($bootServices['cache.settings']['obj']),
+    );
+
+    $bootServices['lockManager'] = array(
+      'class' => 'Civi\Core\Lock\LockManager',
+      'obj' => self::createLockManager(),
+    );
+
+    if ($loadFromDB && $runtime->dsn) {
+      \CRM_Extension_System::singleton(TRUE);
+
+      $c = new self();
+      \Civi::$statics[__CLASS__]['container'] = $c->loadContainer();
     }
-    return \Civi::$statics[__CLASS__];
   }
 
   public static function getBootService($name) {
-    return \Civi::$statics[__CLASS__][$name]['obj'];
+    return \Civi::$statics[__CLASS__]['boot'][$name]['obj'];
   }
 
 }