2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.7 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2015 |
6 +--------------------------------------------------------------------+
7 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * @copyright CiviCRM LLC (c) 2004-2015
34 * Temporarily change a global variable.
37 * $globals = CRM_Utils_GlobalStack::singleton();
38 * $globals->push(array(
47 * Note: for purposes of this class, we'll refer to the array passed into
50 class CRM_Utils_GlobalStack
{
52 * We don't have a container or dependency-injection, so use singleton instead
56 private static $_singleton = NULL;
58 private $backups = array();
61 * Get or set the single instance of CRM_Utils_GlobalStack.
63 * @return CRM_Utils_GlobalStack
65 static public function singleton() {
66 if (self
::$_singleton === NULL) {
67 self
::$_singleton = new CRM_Utils_GlobalStack();
69 return self
::$_singleton;
75 public function push($newFrame) {
76 $this->backups
[] = $this->createBackup($newFrame);
77 $this->applyFrame($newFrame);
80 public function pop() {
81 $this->applyFrame(array_pop($this->backups
));
86 * The new, incoming frame.
90 public function createBackup($new) {
92 foreach ($new as $globalKey => $values) {
93 if (is_array($values)) {
94 foreach ($values as $key => $value) {
95 $frame[$globalKey][$key] = CRM_Utils_Array
::value($key, $GLOBALS[$globalKey]);
99 $frame[$globalKey] = CRM_Utils_Array
::value($globalKey, $GLOBALS);
108 public function applyFrame($newFrame) {
109 foreach ($newFrame as $globalKey => $values) {
110 if (is_array($values)) {
111 foreach ($values as $key => $value) {
112 $GLOBALS[$globalKey][$key] = $value;
116 $GLOBALS[$globalKey] = $values;