2 +--------------------------------------------------------------------+
3 | CiviCRM version 4.4 |
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
28 * Temporarily change a global variable.
31 * $globals = CRM_Utils_GlobalStack::singleton();
32 * $globals->push(array(
41 * Note: for purposes of this class, we'll refer to the array passed into
44 class CRM_Utils_GlobalStack
{
46 * We don't have a container or dependency-injection, so use singleton instead
51 private static $_singleton = NULL;
53 private $backups = array();
56 * Get or set the single instance of CRM_Utils_GlobalStack
58 * @return CRM_Utils_GlobalStack
60 static public function singleton() {
61 if (self
::$_singleton === NULL) {
62 self
::$_singleton = new CRM_Utils_GlobalStack();
64 return self
::$_singleton;
67 public function push($newFrame) {
68 $this->backups
[] = $this->createBackup($newFrame);
69 $this->applyFrame($newFrame);
72 public function pop() {
73 $this->applyFrame(array_pop($this->backups
));
77 * @param array $new the new, incoming frame
80 public function createBackup($new) {
82 foreach ($new as $globalKey => $values) {
83 if (is_array($values)) {
84 foreach ($values as $key => $value) {
85 $frame[$globalKey][$key] = CRM_Utils_Array
::value($key, $GLOBALS[$globalKey]);
88 $frame[$globalKey] = CRM_Utils_Array
::value($globalKey, $GLOBALS);
94 public function applyFrame($newFrame) {
95 foreach ($newFrame as $globalKey => $values) {
96 if (is_array($values)) {
97 foreach ($values as $key => $value) {
98 $GLOBALS[$globalKey][$key] = $value;
101 $GLOBALS[$globalKey] = $values;