commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Utils / GlobalStack.php
1 <?php /*
2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.6 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
25 */
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
49 */
50 private static $_singleton = NULL;
51
52 private $backups = array();
53
54 /**
55 * Get or set the single instance of CRM_Utils_GlobalStack.
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
66 /**
67 * @param $newFrame
68 */
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 /**
79 * @param array $new
80 * The new, incoming frame.
81 * @return array
82 * frame
83 */
84 public function createBackup($new) {
85 $frame = array();
86 foreach ($new as $globalKey => $values) {
87 if (is_array($values)) {
88 foreach ($values as $key => $value) {
89 $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]);
90 }
91 }
92 else {
93 $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS);
94 }
95 }
96 return $frame;
97 }
98
99 /**
100 * @param $newFrame
101 */
102 public function applyFrame($newFrame) {
103 foreach ($newFrame as $globalKey => $values) {
104 if (is_array($values)) {
105 foreach ($values as $key => $value) {
106 $GLOBALS[$globalKey][$key] = $value;
107 }
108 }
109 else {
110 $GLOBALS[$globalKey] = $values;
111 }
112 }
113 }
114
115 }