Commit | Line | Data |
---|---|---|
6d4b9264 TO |
1 | <?php /* |
2 | +--------------------------------------------------------------------+ | |
39de6fd5 | 3 | | CiviCRM version 4.6 | |
6d4b9264 | 4 | +--------------------------------------------------------------------+ |
06b69b18 | 5 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 TO |
26 | |
27 | /** | |
28 | * Temporarily change a global variable. | |
29 | * | |
30 | * @code | |
31 | * $globals = CRM_Utils_GlobalStack::singleton(); | |
32 | * $globals->push(array( | |
33 | * '_GET' => array( | |
34 | * 'q' => 'some-value | |
35 | * ), | |
36 | * )); | |
37 | * ...do stuff... | |
38 | * $globals->pop(); | |
39 | * @endcode | |
40 | * | |
41 | * Note: for purposes of this class, we'll refer to the array passed into | |
42 | * push() as a frame. | |
43 | */ | |
44 | class CRM_Utils_GlobalStack { | |
45 | /** | |
46 | * We don't have a container or dependency-injection, so use singleton instead | |
47 | * | |
48 | * @var object | |
6d4b9264 TO |
49 | */ |
50 | private static $_singleton = NULL; | |
51 | ||
52 | private $backups = array(); | |
53 | ||
54 | /** | |
fe482240 | 55 | * Get or set the single instance of CRM_Utils_GlobalStack. |
6d4b9264 TO |
56 | * |
57 | * @return CRM_Utils_GlobalStack | |
58 | */ | |
59 | static public function singleton() { | |
60 | if (self::$_singleton === NULL) { | |
61 | self::$_singleton = new CRM_Utils_GlobalStack(); | |
62 | } | |
63 | return self::$_singleton; | |
64 | } | |
65 | ||
5bc392e6 EM |
66 | /** |
67 | * @param $newFrame | |
68 | */ | |
6d4b9264 TO |
69 | public function push($newFrame) { |
70 | $this->backups[] = $this->createBackup($newFrame); | |
71 | $this->applyFrame($newFrame); | |
72 | } | |
73 | ||
74 | public function pop() { | |
75 | $this->applyFrame(array_pop($this->backups)); | |
76 | } | |
77 | ||
78 | /** | |
77855840 TO |
79 | * @param array $new |
80 | * The new, incoming frame. | |
a6c01b45 CW |
81 | * @return array |
82 | * frame | |
6d4b9264 TO |
83 | */ |
84 | public function createBackup($new) { | |
85 | $frame = array(); | |
86 | foreach ($new as $globalKey => $values) { | |
25c8f5c6 TO |
87 | if (is_array($values)) { |
88 | foreach ($values as $key => $value) { | |
89 | $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]); | |
90 | } | |
0db6c3e1 TO |
91 | } |
92 | else { | |
25c8f5c6 | 93 | $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS); |
6d4b9264 TO |
94 | } |
95 | } | |
96 | return $frame; | |
97 | } | |
98 | ||
5bc392e6 EM |
99 | /** |
100 | * @param $newFrame | |
101 | */ | |
6d4b9264 TO |
102 | public function applyFrame($newFrame) { |
103 | foreach ($newFrame as $globalKey => $values) { | |
25c8f5c6 TO |
104 | if (is_array($values)) { |
105 | foreach ($values as $key => $value) { | |
106 | $GLOBALS[$globalKey][$key] = $value; | |
107 | } | |
0db6c3e1 TO |
108 | } |
109 | else { | |
25c8f5c6 | 110 | $GLOBALS[$globalKey] = $values; |
6d4b9264 TO |
111 | } |
112 | } | |
113 | } | |
96025800 | 114 | |
232624b1 | 115 | } |