Skip to content

Commit

Permalink
Use sensible caching for current domain
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jul 22, 2020
1 parent 5b9deab commit d357f22
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 49 deletions.
56 changes: 26 additions & 30 deletions CRM/Core/BAO/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
*/
class CRM_Core_BAO_Domain extends CRM_Core_DAO_Domain {

/**
* Cache for the current domain object.
* @var object
*/
public static $_domain = NULL;

/**
* Cache for a domain's location array
* @var array
*/
private $_location = NULL;

/**
* Flushes the cache set by getDomain.
*
* @see CRM_Core_BAO_Domain::getDomain()
* @param CRM_Core_DAO_Domain $domain
*/
public static function onPostSave($domain) {
Civi::$statics[__CLASS__]['current'] = NULL;
}

/**
* Fetch object based on array of properties.
*
Expand All @@ -47,39 +51,37 @@ public static function retrieve(&$params, &$defaults) {
}

/**
* Get the domain BAO.
*
* @param bool $reset
* Get the current domain.
*
* @return \CRM_Core_BAO_Domain
* @throws \CRM_Core_Exception
*/
public static function getDomain($reset = NULL) {
static $domain = NULL;
if (!$domain || $reset) {
public static function getDomain() {
$domain = Civi::$statics[__CLASS__]['current'] ?? NULL;
if (!$domain) {
$domain = new CRM_Core_BAO_Domain();
$domain->id = CRM_Core_Config::domainID();
if (!$domain->find(TRUE)) {
throw new CRM_Core_Exception('No domain in DB');
}
Civi::$statics[__CLASS__]['current'] = $domain;
}
return $domain;
}

/**
* @param bool $skipUsingCache
*
* @return null|string
* @return string
*
* @throws \CRM_Core_Exception
*/
public static function version($skipUsingCache = FALSE) {
return CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
CRM_Core_Config::domainID(),
'version',
'id',
$skipUsingCache
);
if ($skipUsingCache) {
Civi::$statics[__CLASS__]['current'] = NULL;
}

return self::getDomain()->version;
}

/**
Expand All @@ -90,7 +92,7 @@ public static function version($skipUsingCache = FALSE) {
* @throws \CRM_Core_Exception
*/
public static function isDBUpdateRequired() {
$dbVersion = CRM_Core_BAO_Domain::version();
$dbVersion = self::version();
$codeVersion = CRM_Utils_System::version();
return version_compare($dbVersion, $codeVersion) < 0;
}
Expand All @@ -108,16 +110,12 @@ public static function isDBVersionAtLeast($min) {
/**
* Get the location values of a domain.
*
* @return array
* Location::getValues
*
* @throws \CRM_Core_Exception
* @return CRM_Core_BAO_Location[]|NULL
*/
public function &getLocationValues() {
public function getLocationValues() {
if ($this->_location == NULL) {
$domain = self::getDomain(NULL);
$params = [
'contact_id' => $domain->contact_id,
'contact_id' => $this->contact_id,
];
$this->_location = CRM_Core_BAO_Location::getValues($params, TRUE);

Expand Down Expand Up @@ -242,9 +240,7 @@ public static function getGroupId() {
}
elseif ($multisite) {
// create a group with that of domain name
$title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
CRM_Core_Config::domainID(), 'name'
);
$title = self::getDomain()->name;
$groupID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group',
$title, 'id', 'title', TRUE
);
Expand Down
2 changes: 1 addition & 1 deletion CRM/Core/BAO/FinancialTrxn.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ public static function recordFees($params) {
$fItemParams
= [
'financial_account_id' => $financialAccount,
'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
'contact_id' => CRM_Core_BAO_Domain::getDomain()->contact_id,
'created_date' => date('YmdHis'),
'transaction_date' => $params['trxnParams']['trxn_date'],
'amount' => $amount,
Expand Down
5 changes: 2 additions & 3 deletions CRM/Core/BAO/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,9 @@ public static function dataExists(&$params) {
* @param array $entityBlock
* @param bool $microformat
*
* @return array
* array of objects(CRM_Core_BAO_Location)
* @return CRM_Core_BAO_Location[]|NULL
*/
public static function &getValues($entityBlock, $microformat = FALSE) {
public static function getValues($entityBlock, $microformat = FALSE) {
if (empty($entityBlock)) {
return NULL;
}
Expand Down
13 changes: 4 additions & 9 deletions CRM/Core/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,10 +590,8 @@ public function setGettextDomain($key) {
* True if CiviCRM is in multilingual mode.
*/
public static function isMultilingual() {
return (bool) CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
CRM_Core_Config::domainID(),
'locales'
);
$domain = CRM_Core_BAO_Domain::getDomain();
return (bool) $domain->locales;
}

/**
Expand All @@ -602,11 +600,8 @@ public static function isMultilingual() {
* @return array|bool
*/
public static function getMultilingual() {
$locales = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain',
CRM_Core_Config::domainID(),
'locales'
);
return $locales ? CRM_Core_DAO::unSerializeField($locales, CRM_Core_DAO::SERIALIZE_SEPARATOR_TRIMMED) : FALSE;
$domain = CRM_Core_BAO_Domain::getDomain();
return $domain->locales ? CRM_Core_DAO::unSerializeField($domain->locales, CRM_Core_DAO::SERIALIZE_SEPARATOR_TRIMMED) : FALSE;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Financial/BAO/FinancialTypeAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static function createDefaultFinancialAccounts($financialType) {
if (!$dao->N) {
$params = [
'name' => $financialType->name,
'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
'contact_id' => CRM_Core_BAO_Domain::getDomain()->contact_id,
'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
'description' => $financialType->description,
'account_type_code' => 'INC',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Financial/Form/FinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static function formRule($values, $files, $self) {
public function setDefaultValues() {
$defaults = parent::setDefaultValues();
if ($this->_action & CRM_Core_Action::ADD) {
$defaults['contact_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id');
$defaults['contact_id'] = CRM_Core_BAO_Domain::getDomain()->contact_id;
}
return $defaults;
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Utils/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function ($matches) use (&$domain, $html, $escapeSmarty) {

/**
* @param $token
* @param $domain
* @param CRM_Core_BAO_Domain $domain
* @param bool $html
* @param bool $escapeSmarty
*
Expand All @@ -261,7 +261,7 @@ public static function getDomainTokenReplacement($token, &$domain, $html = FALSE
// we have to do this because this function is
// called only when we find a token in the string

$loc = &$domain->getLocationValues();
$loc = $domain->getLocationValues();

if (!in_array($token, self::$_tokens['domain'])) {
$value = "{domain.$token}";
Expand Down
1 change: 1 addition & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public function createEventDispatcher() {
$dispatcher->addListener('civi.dao.postInsert', ['\CRM_Core_BAO_RecurringEntity', 'triggerInsert']);
$dispatcher->addListener('civi.dao.postUpdate', ['\CRM_Core_BAO_RecurringEntity', 'triggerUpdate']);
$dispatcher->addListener('civi.dao.postDelete', ['\CRM_Core_BAO_RecurringEntity', 'triggerDelete']);
$dispatcher->addListener('hook_civicrm_postSave_civicrm_domain', ['\CRM_Core_BAO_Domain', 'onPostSave']);
$dispatcher->addListener('hook_civicrm_unhandled_exception', [
'CRM_Core_LegacyErrorHandler',
'handleException',
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Core/DAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,14 @@ public function testModifyAndBreakQuery() {

public function testSupportedFields() {
// Hack a different db version which will trigger getSupportedFields to filter out newer fields
\CRM_Core_DAO::$_dbColumnValueCache['CRM_Core_DAO_Domain']['id'][1]['version'] = '5.26.0';
CRM_Core_BAO_Domain::getDomain()->version = '5.26.0';

$customGroupFields = CRM_Core_DAO_CustomGroup::getSupportedFields();
// 'icon' was added in 5.28
$this->assertArrayNotHasKey('icon', $customGroupFields);

// Remove domain version override:
\CRM_Core_DAO::$_dbColumnValueCache = NULL;
CRM_Core_BAO_Domain::version(TRUE);

$activityFields = CRM_Activity_DAO_Activity::getSupportedFields();
// Fields should be indexed by name not unique_name (which is "activity_id")
Expand Down

0 comments on commit d357f22

Please sign in to comment.