From f84c861734c54bd4a2d54f61fb8f08f605be391d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 26 Apr 2023 20:26:49 -0400 Subject: [PATCH] Component/Extensions - Remove unnecessary cache clearing and prevent recursion Ensure the cached settings are updated _before_ post_change hooks are called for settings. All the cache clears being performed by on_change hooks were redundant with what happens when extensions are enabled/disabled so I removed it. Also moved the extension install hook to use postInstall which should be more stable, and added checks in onToggleComponents to prevent recursion. --- CRM/Core/BAO/ConfigSetting.php | 1 + CRM/Core/Component.php | 9 --------- CRM/Extension/Upgrader/Component.php | 2 +- Civi/Core/SettingsBag.php | 5 +++-- api/v3/examples/Setting/GetFields.ex.php | 2 -- settings/Core.setting.php | 2 -- 6 files changed, 5 insertions(+), 16 deletions(-) diff --git a/CRM/Core/BAO/ConfigSetting.php b/CRM/Core/BAO/ConfigSetting.php index 00698964cf..7d7b01fbab 100644 --- a/CRM/Core/BAO/ConfigSetting.php +++ b/CRM/Core/BAO/ConfigSetting.php @@ -362,6 +362,7 @@ class CRM_Core_BAO_ConfigSetting { * @param array $enabledComponents */ public static function setEnabledComponents($enabledComponents) { + // The post_change trigger on this setting will sync component extensions, which will also flush caches Civi::settings()->set('enable_components', array_values($enabledComponents)); } diff --git a/CRM/Core/Component.php b/CRM/Core/Component.php index ac801e1d19..e2b1c15f57 100644 --- a/CRM/Core/Component.php +++ b/CRM/Core/Component.php @@ -115,15 +115,6 @@ class CRM_Core_Component { return self::_info($force); } - /** - * Triggered by on_change callback of the 'enable_components' setting. - */ - public static function flushEnabledComponents() { - unset(Civi::$statics[__CLASS__]); - CRM_Core_BAO_Navigation::resetNavigation(); - Civi::cache('metadata')->clear(); - } - /** * @param bool $translated * diff --git a/CRM/Extension/Upgrader/Component.php b/CRM/Extension/Upgrader/Component.php index 7f58f10f2d..d4e3b07e3e 100644 --- a/CRM/Extension/Upgrader/Component.php +++ b/CRM/Extension/Upgrader/Component.php @@ -9,7 +9,7 @@ use CRM_AfformAdmin_ExtensionUtil as E; */ class CRM_Extension_Upgrader_Component extends CRM_Extension_Upgrader_Base { - public function install() { + public function postInstall() { CRM_Core_BAO_ConfigSetting::enableComponent($this->getComponentName()); } diff --git a/Civi/Core/SettingsBag.php b/Civi/Core/SettingsBag.php index 38feb8cb5f..948ea2b963 100644 --- a/Civi/Core/SettingsBag.php +++ b/Civi/Core/SettingsBag.php @@ -248,8 +248,6 @@ class SettingsBag { return $this; } $this->setDb($key, $value); - $this->values[$key] = $value; - $this->combined = NULL; return $this; } @@ -427,6 +425,9 @@ class SettingsBag { \CRM_Core_DAO::executeQuery(\CRM_Utils_SQL_Insert::dao($dao)->toSQL()); } + $this->values[$name] = $value; + $this->combined = NULL; + // Call 'post_change' listeners after the value has been saved. // Unlike 'on_change', this will only fire if the oldValue and newValue are not equivalent (using == comparison) if ($value != $oldValue && !empty($metadata['post_change'])) { diff --git a/api/v3/examples/Setting/GetFields.ex.php b/api/v3/examples/Setting/GetFields.ex.php index 51f9a44bef..803d0898f4 100644 --- a/api/v3/examples/Setting/GetFields.ex.php +++ b/api/v3/examples/Setting/GetFields.ex.php @@ -1216,8 +1216,6 @@ function setting_getfields_expectedresult() { 'help_text' => '', 'on_change' => [ '0' => 'CRM_Case_Info::onToggleComponents', - '1' => 'CRM_Core_Component::flushEnabledComponents', - '2' => 'call://resources/resetCacheCode', ], 'pseudoconstant' => [ 'callback' => 'CRM_Core_SelectValues::getComponentSelectValues', diff --git a/settings/Core.setting.php b/settings/Core.setting.php index 0ab58857cb..fa74835171 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -798,8 +798,6 @@ return [ 'help_text' => NULL, 'on_change' => [ 'CRM_Case_Info::onToggleComponents', - 'CRM_Core_Component::flushEnabledComponents', - 'call://resources/resetCacheCode', ], 'post_change' => [ 'CRM_Core_Component::onToggleComponents', -- 2.25.1