7 * The settings stack allows you to temporarily change (then restore) settings. It's intended
8 * primarily for use in testing.
10 * Like the global `$civicrm_setting` variable, it works best with typical inert settings that
11 * do not trigger extra activation logic. A handful of settings (such as `enable_components`
12 * and ~5 others) should be avoided, but most settings should work.
20 * Ex: $stack[0] == ['settingName', 'oldSettingValue'];
22 protected $stack = [];
25 * Temporarily apply a setting.
27 * @param $settingValue
30 public function push($setting, $settingValue) {
31 if (isset($GLOBALS['civicrm_setting']['domain'][$setting])) {
32 $this->stack
[] = [$setting, $GLOBALS['civicrm_setting']['domain'][$setting]];
35 $this->stack
[] = [$setting, NULL];
37 $GLOBALS['civicrm_setting']['domain'][$setting] = $settingValue;
38 \Civi
::service('settings_manager')->useMandatory();
42 * Restore original settings.
44 public function popAll() {
45 while ($frame = array_pop($this->stack
)) {
46 list($setting, $value) = $frame;
47 if ($value === NULL) {
48 unset($GLOBALS['civicrm_setting']['domain'][$setting]);
51 $GLOBALS['civicrm_setting']['domain'][$setting] = $value;
54 \Civi
::service('settings_manager')->useMandatory();