Skip to content

Commit

Permalink
CRM_Utils_GlobalStack - Allow top-level vars
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jul 30, 2013
1 parent 85f9c0f commit 25c8f5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 12 additions & 4 deletions CRM/Utils/GlobalStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ public function pop() {
public function createBackup($new) {
$frame = array();
foreach ($new as $globalKey => $values) {
foreach ($values as $key => $value) {
$frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]);
if (is_array($values)) {
foreach ($values as $key => $value) {
$frame[$globalKey][$key] = CRM_Utils_Array::value($key, $GLOBALS[$globalKey]);
}
} else {
$frame[$globalKey] = CRM_Utils_Array::value($globalKey, $GLOBALS);
}
}
return $frame;
}

public function applyFrame($newFrame) {
foreach ($newFrame as $globalKey => $values) {
foreach ($values as $key => $value) {
$GLOBALS[$globalKey][$key] = $value;
if (is_array($values)) {
foreach ($values as $key => $value) {
$GLOBALS[$globalKey][$key] = $value;
}
} else {
$GLOBALS[$globalKey] = $values;
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion tests/phpunit/CRM/Utils/GlobalStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@
class CRM_Utils_GlobalStackTest extends CiviUnitTestCase {

public function testPushPop() {
global $FOO;
global $FOO, $EXTRA;

$FOO['bar'] = 1;
$FOO['whiz'] = 1;
$EXTRA = 1;

$this->assertEquals(1, $FOO['bar']);
$this->assertEquals(1, $FOO['whiz']);
$this->assertFalse(isset($FOO['bang']));
$this->assertEquals(1, $EXTRA);

CRM_Utils_GlobalStack::singleton()->push(array(
'FOO' => array(
'bar' => 2,
'bang' => 2,
),
'EXTRA' => 2,
));

$this->assertEquals(2, $FOO['bar']);
$this->assertEquals(1, $FOO['whiz']);
$this->assertEquals(2, $FOO['bang']);
$this->assertEquals(2, $EXTRA);

CRM_Utils_GlobalStack::singleton()->pop();

$this->assertEquals(1, $FOO['bar']);
$this->assertEquals(1, $FOO['whiz']);
$this->assertEquals(NULL, $FOO['bang']);
$this->assertEquals(1, $EXTRA);
}
}

0 comments on commit 25c8f5c

Please sign in to comment.