From: Tim Otten Date: Sun, 20 Sep 2015 23:17:06 +0000 (-0700) Subject: Construct runtime+extensions+container through boot services X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7f835399d23900a450a3be16d81950c129f4d92a;p=civicrm-core.git Construct runtime+extensions+container through boot services --- diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 3e40e9ff9d..e4313de4b7 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -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); diff --git a/CRM/Core/Config/MagicMerge.php b/CRM/Core/Config/MagicMerge.php index c3ab29b586..edb2c8db1d 100644 --- a/CRM/Core/Config/MagicMerge.php +++ b/CRM/Core/Config/MagicMerge.php @@ -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 */ diff --git a/CRM/Core/Config/Runtime.php b/CRM/Core/Config/Runtime.php index 5011c36743..28583382d3 100644 --- a/CRM/Core/Config/Runtime.php +++ b/CRM/Core/Config/Runtime.php @@ -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()); } diff --git a/Civi.php b/Civi.php index 06bfba7fad..fe16326de7 100644 --- 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(); } /** diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 757806aa39..6c3f0955ba 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -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']; } }