Commit | Line | Data |
---|---|---|
6d4b9264 TO |
1 | <?php /* |
2 | +--------------------------------------------------------------------+ | |
fee14197 | 3 | | CiviCRM version 5 | |
6d4b9264 | 4 | +--------------------------------------------------------------------+ |
6b83d5bd | 5 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6d4b9264 TO |
6 | +--------------------------------------------------------------------+ |
7 | | This file is a part of CiviCRM. | | |
8 | | | | |
9 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
10 | | under the terms of the GNU Affero General Public License | | |
11 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
12 | | | | |
13 | | CiviCRM is distributed in the hope that it will be useful, but | | |
14 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
16 | | See the GNU Affero General Public License for more details. | | |
17 | | | | |
18 | | You should have received a copy of the GNU Affero General Public | | |
19 | | License and the CiviCRM Licensing Exception along | | |
20 | | with this program; if not, contact CiviCRM LLC | | |
21 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
22 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
23 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
24 | +--------------------------------------------------------------------+ | |
d25dd0ee | 25 | */ |
6d4b9264 | 26 | |
50bfb460 SB |
27 | /** |
28 | * | |
29 | * @package CRM | |
6b83d5bd | 30 | * @copyright CiviCRM LLC (c) 2004-2019 |
50bfb460 SB |
31 | */ |
32 | ||
6d4b9264 TO |
33 | /** |
34 | * Temporarily change a global variable. | |
35 | * | |
36 | * @code | |
37 | * $globals = CRM_Utils_GlobalStack::singleton(); | |
38 | * $globals->push(array( | |
39 | * '_GET' => array( | |
40 | * 'q' => 'some-value | |
41 | * ), | |
42 | * )); | |
43 | * ...do stuff... | |
44 | * $globals->pop(); | |
45 | * @endcode | |
46 | * | |
47 | * Note: for purposes of this class, we'll refer to the array passed into | |
48 | * push() as a frame. | |
49 | */ | |
50 | class CRM_Utils_GlobalStack { | |
51 | /** | |
52 | * We don't have a container or dependency-injection, so use singleton instead | |
53 | * | |
54 | * @var object | |
6d4b9264 TO |
55 | */ |
56 | private static $_singleton = NULL; | |
57 | ||
be2fb01f | 58 | private $backups = []; |
6d4b9264 TO |
59 | |
60 | /** | |
fe482240 | 61 | * Get or set the single instance of CRM_Utils_GlobalStack. |
6d4b9264 TO |
62 | * |
63 | * @return CRM_Utils_GlobalStack | |
64 | */ | |
6714d8d2 | 65 | public static function singleton() { |
6d4b9264 TO |
66 | if (self::$_singleton === NULL) { |
67 | self::$_singleton = new CRM_Utils_GlobalStack(); | |
68 | } | |
69 | return self::$_singleton; | |
70 | } | |
71 | ||
5bc392e6 EM |
72 | /** |
73 | * @param $newFrame | |
74 | */ | |
6d4b9264 TO |
75 | public function push($newFrame) { |
76 | $this->backups[] = $this->createBackup($newFrame); | |
77 | $this->applyFrame($newFrame); | |
78 | } | |
79 | ||
80 | public function pop() { | |
81 | $this->applyFrame(array_pop($this->backups)); | |
82 | } | |
83 | ||
84 | /** | |
77855840 TO |
85 | * @param array $new |
86 | * The new, incoming frame. | |
a6c01b45 CW |
87 | * @return array |
88 | * frame | |
6d4b9264 TO |
89 | */ |
90 | public function createBackup($new) { | |
be2fb01f | 91 | $frame = []; |
6d4b9264 | 92 | foreach ($new as $globalKey => $values) { |
25c8f5c6 TO |
93 | if (is_array($values)) { |
94 | foreach ($values as $key => $value) { | |
95 | $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]); | |
96 | } | |
0db6c3e1 TO |
97 | } |
98 | else { | |
25c8f5c6 | 99 | $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS); |
6d4b9264 TO |
100 | } |
101 | } | |
102 | return $frame; | |
103 | } | |
104 | ||
5bc392e6 EM |
105 | /** |
106 | * @param $newFrame | |
107 | */ | |
6d4b9264 TO |
108 | public function applyFrame($newFrame) { |
109 | foreach ($newFrame as $globalKey => $values) { | |
25c8f5c6 TO |
110 | if (is_array($values)) { |
111 | foreach ($values as $key => $value) { | |
112 | $GLOBALS[$globalKey][$key] = $value; | |
113 | } | |
0db6c3e1 TO |
114 | } |
115 | else { | |
25c8f5c6 | 116 | $GLOBALS[$globalKey] = $values; |
6d4b9264 TO |
117 | } |
118 | } | |
119 | } | |
96025800 | 120 | |
232624b1 | 121 | } |