Merge pull request #19116 from eileenmcnaughton/pay_edit
[civicrm-core.git] / Civi / Core / SettingsStack.php
CommitLineData
534f8af1
TO
1<?php
2namespace 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 */
16class SettingsStack {
17
18 /**
19 * @var array
20 * Ex: $stack[0] == ['settingName', 'oldSettingValue'];
21 */
c64f69d9 22 protected $stack = [];
534f8af1
TO
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])) {
c64f69d9 32 $this->stack[] = [$setting, $GLOBALS['civicrm_setting']['domain'][$setting]];
534f8af1
TO
33 }
34 else {
c64f69d9 35 $this->stack[] = [$setting, NULL];
534f8af1
TO
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}