| 1 | <?php /* |
| 2 | +--------------------------------------------------------------------+ |
| 3 | | CiviCRM version 5 | |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC (c) 2004-2019 | |
| 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 | * |
| 29 | * @package CRM |
| 30 | * @copyright CiviCRM LLC (c) 2004-2019 |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * Temporarily change a global variable. |
| 35 | * |
| 36 | * @code |
| 37 | * $globals = CRM_Utils_GlobalStack::singleton(); |
| 38 | * $globals->push(array( |
| 39 | * '_GET' => array( |
| 40 | * 'q' => 'some-value |
| 41 | * ), |
| 42 | * )); |
| 43 | * ...do stuff... |
| 44 | * $globals->pop(); |
| 45 | * @endcode |
| 46 | * |
| 47 | * Note: for purposes of this class, we'll refer to the array passed into |
| 48 | * push() as a frame. |
| 49 | */ |
| 50 | class CRM_Utils_GlobalStack { |
| 51 | /** |
| 52 | * We don't have a container or dependency-injection, so use singleton instead |
| 53 | * |
| 54 | * @var object |
| 55 | */ |
| 56 | private static $_singleton = NULL; |
| 57 | |
| 58 | private $backups = []; |
| 59 | |
| 60 | /** |
| 61 | * Get or set the single instance of CRM_Utils_GlobalStack. |
| 62 | * |
| 63 | * @return CRM_Utils_GlobalStack |
| 64 | */ |
| 65 | public static function singleton() { |
| 66 | if (self::$_singleton === NULL) { |
| 67 | self::$_singleton = new CRM_Utils_GlobalStack(); |
| 68 | } |
| 69 | return self::$_singleton; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param $newFrame |
| 74 | */ |
| 75 | public function push($newFrame) { |
| 76 | $this->backups[] = $this->createBackup($newFrame); |
| 77 | $this->applyFrame($newFrame); |
| 78 | } |
| 79 | |
| 80 | public function pop() { |
| 81 | $this->applyFrame(array_pop($this->backups)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @param array $new |
| 86 | * The new, incoming frame. |
| 87 | * @return array |
| 88 | * frame |
| 89 | */ |
| 90 | public function createBackup($new) { |
| 91 | $frame = []; |
| 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]); |
| 96 | } |
| 97 | } |
| 98 | else { |
| 99 | $frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS); |
| 100 | } |
| 101 | } |
| 102 | return $frame; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @param $newFrame |
| 107 | */ |
| 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; |
| 113 | } |
| 114 | } |
| 115 | else { |
| 116 | $GLOBALS[$globalKey] = $values; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | } |