Skip to content

Commit

Permalink
Construct runtime+extensions+container through boot services
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Sep 21, 2015
1 parent d4330c6 commit 7f83539
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 75 deletions.
11 changes: 2 additions & 9 deletions CRM/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,8 @@ public static function &singleton($loadFromDB = TRUE, $force = FALSE) {
}

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);

Expand Down
16 changes: 3 additions & 13 deletions CRM/Core/Config/MagicMerge.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CRM_Core_Config_MagicMerge {
*/
private $map;

private $runtime, $locals, $settings;
private $locals, $settings;

private $cache = array();

Expand Down Expand Up @@ -103,7 +103,7 @@ public static function getPropertyMap() {
'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'),

Expand Down Expand Up @@ -241,7 +241,7 @@ public function __get($k) {
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);
Expand Down Expand Up @@ -339,16 +339,6 @@ public function __unset($k) {
}
}

/**
* @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
*/
Expand Down
1 change: 1 addition & 0 deletions CRM/Core/Config/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function initialize($loadFromDB = TRUE) {

$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());
}
Expand Down
3 changes: 1 addition & 2 deletions Civi.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public static function service($id) {
*/
public static function reset() {
self::$statics = array();
Civi\Core\Container::getBootServices();
Civi\Core\Container::singleton(TRUE);
Civi\Core\Container::singleton();
}

/**
Expand Down
112 changes: 61 additions & 51 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

/**
Expand Down Expand Up @@ -175,7 +169,10 @@ public function createContainer() {
$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');
Expand Down Expand Up @@ -305,57 +302,70 @@ public function createApiKernel($dispatcher, $magicFunctionProvider) {
*
* 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'];
}

}

0 comments on commit 7f83539

Please sign in to comment.