-
-
Notifications
You must be signed in to change notification settings - Fork 825
/
Copy pathSettingsStack.php
57 lines (52 loc) · 1.48 KB
/
SettingsStack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Civi\Core;
/**
* Class SettingsStack
*
* The settings stack allows you to temporarily change (then restore) settings. It's intended
* primarily for use in testing.
*
* Like the global `$civicrm_setting` variable, it works best with typical inert settings that
* do not trigger extra activation logic. A handful of settings (such as `enable_components`
* and ~5 others) should be avoided, but most settings should work.
*
* @package Civi\Core
*/
class SettingsStack {
/**
* @var array
* Ex: $stack[0] == ['settingName', 'oldSettingValue'];
*/
protected $stack = [];
/**
* Temporarily apply a setting.
*
* @param $settingValue
* @param $setting
*/
public function push($setting, $settingValue) {
if (isset($GLOBALS['civicrm_setting']['domain'][$setting])) {
$this->stack[] = [$setting, $GLOBALS['civicrm_setting']['domain'][$setting]];
}
else {
$this->stack[] = [$setting, NULL];
}
$GLOBALS['civicrm_setting']['domain'][$setting] = $settingValue;
\Civi::service('settings_manager')->useMandatory();
}
/**
* Restore original settings.
*/
public function popAll() {
while ($frame = array_pop($this->stack)) {
[$setting, $value] = $frame;
if ($value === NULL) {
unset($GLOBALS['civicrm_setting']['domain'][$setting]);
}
else {
$GLOBALS['civicrm_setting']['domain'][$setting] = $value;
}
}
\Civi::service('settings_manager')->useMandatory();
}
}