Commit | Line | Data |
---|---|---|
534f8af1 TO |
1 | <?php |
2 | namespace Civi\Core; | |
3 | ||
4 | /** | |
5 | * Class SettingsStack | |
6 | * | |
8f37e8e3 TO |
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` | |
2fb1b2af | 12 | * and ~5 others) should be avoided, but most settings should work. |
534f8af1 TO |
13 | * |
14 | * @package Civi\Core | |
15 | */ | |
16 | class SettingsStack { | |
17 | ||
18 | /** | |
19 | * @var array | |
20 | * Ex: $stack[0] == ['settingName', 'oldSettingValue']; | |
21 | */ | |
22 | protected $stack = array(); | |
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[] = array($setting, $GLOBALS['civicrm_setting']['domain'][$setting]); | |
33 | } | |
34 | else { | |
35 | $this->stack[] = array($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 | } |