Convert $config->userSystem to boot service
authorTim Otten <totten@civicrm.org>
Sat, 19 Sep 2015 08:33:57 +0000 (01:33 -0700)
committerTim Otten <totten@civicrm.org>
Mon, 21 Sep 2015 04:24:28 +0000 (21:24 -0700)
CRM/Core/Config.php
CRM/Core/Config/MagicMerge.php
CRM/Core/Config/Runtime.php
CRM/Utils/System/Base.php
Civi.php
Civi/Core/Container.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 4bb8f5cb204d34abea0a469b71065e8b5f612cca..3e40e9ff9d841fd61c331ebe5dc830775326bb30 100644 (file)
@@ -389,17 +389,6 @@ class CRM_Core_Config extends CRM_Core_Config_MagicMerge {
     return FALSE;
   }
 
-
-  /**
-   * Wrapper function to allow unit tests to switch user framework on the fly.
-   *
-   * @param string $userFramework
-   *   One of 'Drupal', 'Joomla', etc.
-   * @deprecated
-   */
-  public function setUserFramework($userFramework) {
-    $this->getRuntime()->setUserFramework($userFramework);
-  }
   /**
    * Is back office credit card processing enabled for this site - ie are there any installed processors that support
    * it?
index 5cdf540906b52ef6fe80615b151ed04295744ea0..c3ab29b5860ea825e1cd2013da12d88bd8215ecd 100644 (file)
@@ -86,27 +86,31 @@ class CRM_Core_Config_MagicMerge {
       'doNotResetCache' => array('local'),
       'inCiviCRM' => array('local'),
       'userFrameworkFrontend' => array('local'),
+      'userPermissionTemp' => array('local'),
 
       // "runtime" properties are computed from define()s, $_ENV, etc.
       // See also: CRM_Core_Config_Runtime.
       'dsn' => array('runtime'),
       'initialized' => array('runtime'),
       'userFramework' => array('runtime'),
-      'userFrameworkBaseURL' => array('runtime'),
       'userFrameworkClass' => array('runtime'),
       'userFrameworkDSN' => array('runtime'),
-      'useFrameworkRelativeBase' => array('runtime', 'useFrameworkRelativeBase'),
       'userFrameworkURLVar' => array('runtime'),
-      'userFrameworkVersion' => array('runtime'),
-      'userPermissionClass' => array('runtime'),
-      'userPermissionTemp' => array('runtime'),
-      'userSystem' => array('runtime'),
       'userHookClass' => array('runtime'),
       'cleanURL' => array('runtime'),
       'configAndLogDir' => array('runtime'),
       'templateCompileDir' => array('runtime'),
       'templateDir' => array('runtime'),
 
+      // "boot-svc" properties are critical services needed during init.
+      // See also: Civi\Core\Container::getBootServices().
+      'userSystem' => array('boot-svc'),
+      'userPermissionClass' => array('boot-svc'),
+
+      'userFrameworkBaseURL' => array('user-system', 'getAbsoluteBaseURL'),
+      'userFrameworkVersion' => array('user-system', 'getVersion'),
+      'useFrameworkRelativeBase' => array('user-system', 'getRelativeBaseURL'), // ugh typo.
+
       // "setting" properties are loaded through the setting layer, esp
       // table "civicrm_setting" and global $civicrm_setting.
       // See also: Civi::settings().
@@ -239,10 +243,19 @@ class CRM_Core_Config_MagicMerge {
       case 'runtime':
         return $this->getRuntime()->{$name};
 
+      case 'boot-svc':
+        $this->cache[$k] = \Civi\Core\Container::getBootService($name);
+        return $this->cache[$k];
+
       case 'local':
         $this->initLocals();
         return $this->locals[$name];
 
+      case 'user-system':
+        $userSystem = \Civi\Core\Container::getBootService('userSystem');
+        $this->cache[$k] = call_user_func(array($userSystem, $name));
+        return $this->cache[$k];
+
       case 'service':
         return \Civi::service($name);
 
@@ -271,6 +284,7 @@ class CRM_Core_Config_MagicMerge {
       case 'setting-path':
       case 'setting-url-abs':
       case 'setting-url-rel':
+      case 'user-system':
       case 'runtime':
       case 'callback':
         // In the past, changes to $config were not persisted automatically.
@@ -352,6 +366,7 @@ class CRM_Core_Config_MagicMerge {
         'doNotResetCache' => 0,
         'initialized' => FALSE,
         'userFrameworkFrontend' => FALSE,
+        'userPermissionTemp' => NULL,
       );
     }
   }
index bfaf7c08e651b79c3f62cd83654002cce4ed45c8..5011c36743496d1ebb38e15616f7b092041cc851 100644 (file)
@@ -67,25 +67,6 @@ class CRM_Core_Config_Runtime {
 
   public $userHookClass;
 
-  public $userPermissionClass;
-
-  /**
-   * Manager for temporary permissions.
-   * @todo move to container
-   *
-   * @var CRM_Core_Permission_Temp
-   */
-  public $userPermissionTemp;
-
-  /**
-   * The connector module for the CMS/UF
-   * @todo Introduce an interface.
-   * @todo move to container
-   *
-   * @var CRM_Utils_System_Base
-   */
-  public $userSystem;
-
   /**
    * Are we generating clean url's and using mod_rewrite
    * @var string
@@ -131,56 +112,19 @@ class CRM_Core_Config_Runtime {
     if (!defined('CIVICRM_UF')) {
       $this->fatal('You need to define CIVICRM_UF in civicrm.settings.php');
     }
-    $this->setUserFramework(CIVICRM_UF);
-
-    $this->templateDir = array(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR);
-
-    if (CRM_Utils_System::isSSL()) {
-      $this->userSystem->mapConfigToSSL();
-    }
-
-    if (isset($this->customPHPPathDir) && $this->customPHPPathDir) {
-      set_include_path($this->customPHPPathDir . PATH_SEPARATOR . get_include_path());
-    }
-
-    $this->initialized = 1;
-  }
-
-  public function setUserFramework($userFramework) {
-    $this->userFramework = $userFramework;
-    $this->userFrameworkClass = 'CRM_Utils_System_' . $userFramework;
-    $this->userHookClass = 'CRM_Utils_Hook_' . $userFramework;
-    $userPermissionClass = 'CRM_Core_Permission_' . $userFramework;
-    $this->userPermissionClass = new $userPermissionClass();
 
-    $class = $this->userFrameworkClass;
-    $this->userSystem = new $class();
+    $this->userFramework = CIVICRM_UF;
+    $this->userFrameworkClass = 'CRM_Utils_System_' . CIVICRM_UF;
+    $this->userHookClass = 'CRM_Utils_Hook_' . CIVICRM_UF;
 
-    if ($userFramework == 'Joomla') {
+    if (CIVICRM_UF == 'Joomla') {
       $this->userFrameworkURLVar = 'task';
     }
 
-    if (defined('CIVICRM_UF_BASEURL')) {
-      $this->userFrameworkBaseURL = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/');
-
-      //format url for language negotiation, CRM-7803
-      $this->userFrameworkBaseURL = CRM_Utils_System::languageNegotiationURL($this->userFrameworkBaseURL);
-
-      if (CRM_Utils_System::isSSL()) {
-        $this->userFrameworkBaseURL = str_replace('http://', 'https://', $this->userFrameworkBaseURL);
-      }
-
-      $base = parse_url($this->userFrameworkBaseURL);
-      $this->useFrameworkRelativeBase = $base['path'];
-      //$this->useFrameworkRelativeBase = empty($base['path']) ? '/' : $base['path'];
-    }
-
     if (defined('CIVICRM_UF_DSN')) {
       $this->userFrameworkDSN = CIVICRM_UF_DSN;
     }
 
-    $this->userFrameworkVersion = $this->userSystem->getVersion();
-
     // this is dynamically figured out in the civicrm.settings.php file
     if (defined('CIVICRM_CLEANURL')) {
       $this->cleanURL = CIVICRM_CLEANURL;
@@ -188,6 +132,14 @@ class CRM_Core_Config_Runtime {
     else {
       $this->cleanURL = 0;
     }
+
+    $this->templateDir = array(dirname(dirname(dirname(__DIR__))) . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR);
+
+    if (isset($this->customPHPPathDir) && $this->customPHPPathDir) {
+      set_include_path($this->customPHPPathDir . PATH_SEPARATOR . get_include_path());
+    }
+
+    $this->initialized = 1;
   }
 
   private function fatal($message) {
@@ -218,5 +170,4 @@ class CRM_Core_Config_Runtime {
     return Civi::$statics[__CLASS__]['id'];
   }
 
-
 }
index 1fd5424126ca1725782bc5181ccee3d1bceb63e6..85ff17cf03948bbf0d8bb62b8e2e666ab43b25aa 100644 (file)
@@ -50,6 +50,12 @@ abstract class CRM_Utils_System_Base {
    */
   var $supports_form_extensions = FALSE;
 
+  public function initialize() {
+    if (\CRM_Utils_System::isSSL()) {
+      $this->mapConfigToSSL();
+    }
+  }
+
   /**
    * Append an additional breadcrumb tag to the existing breadcrumb.
    *
@@ -284,6 +290,33 @@ abstract class CRM_Utils_System_Base {
     return 'left';
   }
 
+  public function getAbsoluteBaseURL() {
+    if (!defined('CIVICRM_UF_BASEURL')) {
+      return FALSE;
+    }
+
+    $url = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/');
+
+    //format url for language negotiation, CRM-7803
+    $url = $this->languageNegotiationURL($url);
+
+    if (CRM_Utils_System::isSSL()) {
+      $url = str_replace('http://', 'https://', $url);
+    }
+
+    return $url;
+  }
+
+  public function getRelativeBaseURL() {
+    $absoluteBaseURL = $this->getAbsoluteBaseURL();
+    if ($absoluteBaseURL === FALSE) {
+      return FALSE;
+    }
+    $parts = parse_url($absoluteBaseURL);
+    return $parts['path'];
+    //$this->useFrameworkRelativeBase = empty($base['path']) ? '/' : $base['path'];
+  }
+
   /**
    * Get CMS Version.
    *
index 82f738f271946a27ce244bc3d8b05954a9eb7570..06bfba7fad17e8a1d3ffe296109b9a97d015160f 100644 (file)
--- a/Civi.php
+++ b/Civi.php
@@ -74,11 +74,7 @@ class Civi {
    * @return \Civi\Core\Paths
    */
   public static function paths() {
-    // Paths must be available before container can boot.
-    if (!isset(Civi::$statics[__CLASS__]['paths'])) {
-      Civi::$statics[__CLASS__]['paths'] = new \Civi\Core\Paths();
-    }
-    return Civi::$statics[__CLASS__]['paths'];
+    return \Civi\Core\Container::getBootService('paths');
   }
 
   /**
index e75fa4c3f93f86da610c8de46d530480471875dc..757806aa3910f3b3ee9de9e32f9088bf5360a347 100644 (file)
@@ -310,28 +310,52 @@ class Container {
    * @throws \CRM_Core_Exception
    */
   public static function getBootServices() {
-    if (!isset(\Civi::$statics['*boot*'])) {
-      \Civi::$statics['*boot*']['cache.settings'] = array(
+    if (!isset(\Civi::$statics[__CLASS__])) {
+      $bootServices = array();
+      \Civi::$statics[__CLASS__] = &$bootServices;
+
+      $config = \CRM_Core_Config::singleton();
+
+      $class = $config->userFrameworkClass;
+      $userSystem = new $class();
+      $userSystem->initialize();
+
+      $userPermissionClass = 'CRM_Core_Permission_' . $config->userFramework;
+
+      $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'),
         )),
       );
-      \Civi::$statics['*boot*']['settings_manager'] = array(
+      $bootServices['settings_manager'] = array(
         'class' => 'Civi\Core\SettingsManager',
-        'obj' => new \Civi\Core\SettingsManager(\Civi::$statics['*boot*']['cache.settings']['obj']),
+        'obj' => new \Civi\Core\SettingsManager($bootServices['cache.settings']['obj']),
       );
-      \Civi::$statics['*boot*']['lockManager'] = array(
+      $bootServices['lockManager'] = array(
         'class' => 'Civi\Core\Lock\LockManager',
         'obj' => self::createLockManager(),
       );
     }
-    return \Civi::$statics['*boot*'];
+    return \Civi::$statics[__CLASS__];
   }
 
   public static function getBootService($name) {
-    return \Civi::$statics['*boot*'][$name]['obj'];
+    return \Civi::$statics[__CLASS__][$name]['obj'];
   }
 
 }
index cc09cae39c12f74cf38e1a6c7e3bbee7673f1e0d..9219c9f03da9ff51e1ee453d879ce19919778f14 100755 (executable)
@@ -410,7 +410,6 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     $config = CRM_Core_Config::singleton(TRUE, TRUE); // ugh, performance
 
     // when running unit tests, use mockup user framework
-    $config->setUserFramework('UnitTests');
     $this->hookClass = CRM_Utils_Hook::singleton(TRUE);
 
     // Make sure the DB connection is setup properly