Cleanup phpdoc comments
[civicrm-core.git] / CRM / Utils / GlobalStack.php
CommitLineData
6d4b9264
TO
1<?php /*
2 +--------------------------------------------------------------------+
06b69b18 3 | CiviCRM version 4.5 |
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 +--------------------------------------------------------------------+
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
5bc392e6
EM
67 /**
68 * @param $newFrame
69 */
6d4b9264
TO
70 public function push($newFrame) {
71 $this->backups[] = $this->createBackup($newFrame);
72 $this->applyFrame($newFrame);
73 }
74
75 public function pop() {
76 $this->applyFrame(array_pop($this->backups));
77 }
78
79 /**
80 * @param array $new the new, incoming frame
81 * @return array frame
82 */
83 public function createBackup($new) {
84 $frame = array();
85 foreach ($new as $globalKey => $values) {
25c8f5c6
TO
86 if (is_array($values)) {
87 foreach ($values as $key => $value) {
88 $frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]);
89 }
90 } else {
91 $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS);
6d4b9264
TO
92 }
93 }
94 return $frame;
95 }
96
5bc392e6
EM
97 /**
98 * @param $newFrame
99 */
6d4b9264
TO
100 public function applyFrame($newFrame) {
101 foreach ($newFrame as $globalKey => $values) {
25c8f5c6
TO
102 if (is_array($values)) {
103 foreach ($values as $key => $value) {
104 $GLOBALS[$globalKey][$key] = $value;
105 }
106 } else {
107 $GLOBALS[$globalKey] = $values;
6d4b9264
TO
108 }
109 }
110 }
232624b1 111}