Merge pull request #3580 from monishdeb/CRM-14701
[civicrm-core.git] / CRM / Utils / GlobalStack.php
CommitLineData
6d4b9264
TO
1<?php /*
2 +--------------------------------------------------------------------+
232624b1 3 | CiviCRM version 4.4 |
6d4b9264
TO
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
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 */
44class CRM_Utils_GlobalStack {
45 /**
46 * We don't have a container or dependency-injection, so use singleton instead
47 *
48 * @var object
49 * @static
50 */
51 private static $_singleton = NULL;
52
53 private $backups = array();
54
55 /**
56 * Get or set the single instance of CRM_Utils_GlobalStack
57 *
58 * @return CRM_Utils_GlobalStack
59 */
60 static public function singleton() {
61 if (self::$_singleton === NULL) {
62 self::$_singleton = new CRM_Utils_GlobalStack();
63 }
64 return self::$_singleton;
65 }
66
67 public function push($newFrame) {
68 $this->backups[] = $this->createBackup($newFrame);
69 $this->applyFrame($newFrame);
70 }
71
72 public function pop() {
73 $this->applyFrame(array_pop($this->backups));
74 }
75
76 /**
77 * @param array $new the new, incoming frame
78 * @return array frame
79 */
80 public function createBackup($new) {
81 $frame = array();
82 foreach ($new as $globalKey => $values) {
25c8f5c6
TO
83 if (is_array($values)) {
84 foreach ($values as $key => $value) {
85 $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]);
86 }
87 } else {
88 $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS);
6d4b9264
TO
89 }
90 }
91 return $frame;
92 }
93
94 public function applyFrame($newFrame) {
95 foreach ($newFrame as $globalKey => $values) {
25c8f5c6
TO
96 if (is_array($values)) {
97 foreach ($values as $key => $value) {
98 $GLOBALS[$globalKey][$key] = $value;
99 }
100 } else {
101 $GLOBALS[$globalKey] = $values;
6d4b9264
TO
102 }
103 }
104 }
232624b1 105}