2 +--------------------------------------------------------------------+
3 | Copyright CiviCRM LLC. All rights reserved. |
5 | This work is published under the GNU AGPLv3 license with some |
6 | permitted exceptions and without any warranty. For full license |
7 | and copyright information, see https://civicrm.org/licensing |
8 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
18 * Temporarily change a global variable.
21 * $globals = CRM_Utils_GlobalStack::singleton();
22 * $globals->push(array(
31 * Note: for purposes of this class, we'll refer to the array passed into
34 class CRM_Utils_GlobalStack
{
36 * We don't have a container or dependency-injection, so use singleton instead
40 private static $_singleton = NULL;
42 private $backups = [];
45 * Get or set the single instance of CRM_Utils_GlobalStack.
47 * @return CRM_Utils_GlobalStack
49 public static function singleton() {
50 if (self
::$_singleton === NULL) {
51 self
::$_singleton = new CRM_Utils_GlobalStack();
53 return self
::$_singleton;
59 public function push($newFrame) {
60 $this->backups
[] = $this->createBackup($newFrame);
61 $this->applyFrame($newFrame);
64 public function pop() {
65 $this->applyFrame(array_pop($this->backups
));
70 * The new, incoming frame.
74 public function createBackup($new) {
76 foreach ($new as $globalKey => $values) {
77 if (is_array($values)) {
78 foreach ($values as $key => $value) {
79 $frame[$globalKey][$key] = $GLOBALS[$globalKey][$key] ??
NULL;
83 $frame[$globalKey] = $GLOBALS[$globalKey] ??
NULL;
92 public function applyFrame($newFrame) {
93 foreach ($newFrame as $globalKey => $values) {
94 if (is_array($values)) {
95 foreach ($values as $key => $value) {
96 $GLOBALS[$globalKey][$key] = $value;
100 $GLOBALS[$globalKey] = $values;