Merge pull request #18324 from mattwire/payproc_configoptional
[civicrm-core.git] / Civi / Core / SettingsStack.php
1 <?php
2 namespace Civi\Core;
3
4 /**
5 * Class SettingsStack
6 *
7 * The settings stack allows you to temporarily change (then restore) settings. It's intended
8 * primarily for use in testing.
9 *
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.
13 *
14 * @package Civi\Core
15 */
16 class SettingsStack {
17
18 /**
19 * @var array
20 * Ex: $stack[0] == ['settingName', 'oldSettingValue'];
21 */
22 protected $stack = [];
23
24 /**
25 * Temporarily apply a setting.
26 *
27 * @param $settingValue
28 * @param $setting
29 */
30 public function push($setting, $settingValue) {
31 if (isset($GLOBALS['civicrm_setting']['domain'][$setting])) {
32 $this->stack[] = [$setting, $GLOBALS['civicrm_setting']['domain'][$setting]];
33 }
34 else {
35 $this->stack[] = [$setting, NULL];
36 }
37 $GLOBALS['civicrm_setting']['domain'][$setting] = $settingValue;
38 \Civi::service('settings_manager')->useMandatory();
39 }
40
41 /**
42 * Restore original settings.
43 */
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]);
49 }
50 else {
51 $GLOBALS['civicrm_setting']['domain'][$setting] = $value;
52 }
53 }
54 \Civi::service('settings_manager')->useMandatory();
55 }
56
57 }