Skip to content
Snippets Groups Projects
Commit 8f2a141a authored by totten's avatar totten
Browse files

(REF) #1724 - SettingsBag - Convert to computeVirtual/updateVirtual

The `SettingsBag` uses the `$combined` property to (locally) cache a full
view of the active settings (based on combining different layers of data --
default-values, explicit-values, and mandatory-values). This design is
good for read-mostly data.

This patch changes the `__ContributionSettings` to be another layer in the
construction of `$combined` -- the virtual-values layer.  It fixes issues
wherein:

1. The virtual value is recomputed during every call to `get($key)`.

2. The virtual value is only based on explicit-values -- it disregards
   default-values and mandatory-values.
parent c8419967
No related branches found
No related tags found
No related merge requests found
......@@ -136,7 +136,6 @@ class SettingsBag {
while ($dao->fetch()) {
$this->values[$dao->name] = ($dao->value !== NULL) ? \CRM_Utils_String::unserialize($dao->value) : NULL;
}
$dao->values['contribution_invoice_settings'] = $this->getContributionSettings();
}
return $this;
......@@ -167,8 +166,11 @@ class SettingsBag {
$this->combined = $this->combine(
[$this->defaults, $this->values, $this->mandatory]
);
// computeVirtual() depends on completion of preceding pass.
$this->combined = $this->combine(
[$this->combined, $this->computeVirtual()]
);
}
$this->combined['contribution_invoice_settings'] = $this->getContributionSettings();
return $this->combined;
}
......@@ -255,8 +257,7 @@ class SettingsBag {
* @return SettingsBag
*/
public function set($key, $value) {
if ($key === 'contribution_invoice_settings') {
$this->setContributionSettings($value);
if ($this->updateVirtual($key, $value)) {
return $this;
}
$this->setDb($key, $value);
......@@ -266,6 +267,8 @@ class SettingsBag {
}
/**
* Update a virtualized/deprecated setting.
*
* Temporary handling for phasing out contribution_invoice_settings.
*
* Until we have transitioned we need to handle setting & retrieving
......@@ -275,29 +278,33 @@ class SettingsBag {
*
* https://lab.civicrm.org/dev/core/issues/1558
*
* @param string $key
* @param array $value
* @return bool
* TRUE if $key is a virtualized setting. FALSE if it is a normal setting.
*/
public function setContributionSettings($value) {
foreach (SettingsBag::getContributionInvoiceSettingKeys() as $possibleKeyName => $settingName) {
$keyValue = $value[$possibleKeyName] ?? '';
$this->set($settingName, $keyValue);
public function updateVirtual($key, $value) {
if ($key === 'contribution_invoice_settings') {
foreach (SettingsBag::getContributionInvoiceSettingKeys() as $possibleKeyName => $settingName) {
$keyValue = $value[$possibleKeyName] ?? '';
$this->set($settingName, $keyValue);
}
return TRUE;
}
$this->values['contribution_invoice_settings'] = $this->getContributionSettings();
return FALSE;
}
/**
* Temporary function to handle returning the contribution_settings key despite it being deprecated.
*
* See more in comment block on previous function.
* Determine the values of any virtual/computed settings.
*
* @return array
*/
public function getContributionSettings() {
public function computeVirtual() {
$contributionSettings = [];
foreach (SettingsBag::getContributionInvoiceSettingKeys() as $keyName => $settingName) {
$contributionSettings[$keyName] = $this->values[$settingName] ?? '';
$contributionSettings[$keyName] = $this->get($settingName);
}
return $contributionSettings;
return ['contribution_invoice_settings' => $contributionSettings];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment