5
* Copyright (c) 2001-2014, Sebastian Bergmann <sebastian@phpunit.de>.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
15
* * Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in
17
* the documentation and/or other materials provided with the
20
* * Neither the name of Sebastian Bergmann nor the names of his
21
* contributors may be used to endorse or promote products derived
22
* from this software without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
* POSSIBILITY OF SUCH DAMAGE.
37
* @author Sebastian Bergmann <sebastian@phpunit.de>
38
* @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
39
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
40
* @link http://www.github.com/sebastianbergmann/global-state
43
namespace SebastianBergmann\GlobalState;
45
use ReflectionProperty;
48
* Restorer of snapshots of global state.
50
* @author Sebastian Bergmann <sebastian@phpunit.de>
51
* @copyright 2001-2014 Sebastian Bergmann <sebastian@phpunit.de>
52
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
53
* @link http://www.github.com/sebastianbergmann/global-state
58
* Deletes function definitions that are not defined in a snapshot.
60
* @param Snapshot $snapshot
61
* @throws RuntimeException when the uopz_delete() function is not available
62
* @see https://github.com/krakjoe/uopz
64
public function restoreFunctions(Snapshot $snapshot)
66
if (!function_exists('uopz_delete')) {
67
throw new RuntimeException('The uopz_delete() function is required for this operation');
70
$functions = get_defined_functions();
72
foreach (array_diff($functions['user'], $snapshot->functions()) as $function) {
73
uopz_delete($function);
78
* Restores all global and super-global variables from a snapshot.
80
* @param Snapshot $snapshot
82
public function restoreGlobalVariables(Snapshot $snapshot)
84
$superGlobalArrays = $snapshot->superGlobalArrays();
86
foreach ($superGlobalArrays as $superGlobalArray) {
87
$this->restoreSuperGlobalArray($snapshot, $superGlobalArray);
90
$globalVariables = $snapshot->globalVariables();
92
foreach (array_keys($GLOBALS) as $key) {
93
if ($key != 'GLOBALS' &&
94
!in_array($key, $superGlobalArrays) &&
95
!$snapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
96
if (isset($globalVariables[$key])) {
97
$GLOBALS[$key] = $globalVariables[$key];
99
unset($GLOBALS[$key]);
106
* Restores all static attributes in user-defined classes from this snapshot.
108
* @param Snapshot $snapshot
110
public function restoreStaticAttributes(Snapshot $snapshot)
112
foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
113
foreach ($staticAttributes as $name => $value) {
114
$reflector = new ReflectionProperty($className, $name);
115
$reflector->setAccessible(true);
116
$reflector->setValue($value);
122
* Restores a super-global variable array from this snapshot.
124
* @param Snapshot $snapshot
125
* @param $superGlobalArray
127
private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
129
$superGlobalVariables = $snapshot->superGlobalVariables();
131
if (isset($GLOBALS[$superGlobalArray]) &&
132
is_array($GLOBALS[$superGlobalArray]) &&
133
isset($superGlobalVariables[$superGlobalArray])) {
136
$GLOBALS[$superGlobalArray],
137
$superGlobalVariables[$superGlobalArray]
141
foreach ($keys as $key) {
142
if (isset($superGlobalVariables[$superGlobalArray][$key])) {
143
$GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
145
unset($GLOBALS[$superGlobalArray][$key]);