3 * This file is part of the GlobalState package.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace SebastianBergmann\GlobalState;
13 use ReflectionProperty;
16 * Restorer of snapshots of global state.
21 * Deletes function definitions that are not defined in a snapshot.
23 * @param Snapshot $snapshot
24 * @throws RuntimeException when the uopz_delete() function is not available
25 * @see https://github.com/krakjoe/uopz
27 public function restoreFunctions(Snapshot $snapshot)
29 if (!function_exists('uopz_delete')) {
30 throw new RuntimeException('The uopz_delete() function is required for this operation');
33 $functions = get_defined_functions();
35 foreach (array_diff($functions['user'], $snapshot->functions()) as $function) {
36 uopz_delete($function);
41 * Restores all global and super-global variables from a snapshot.
43 * @param Snapshot $snapshot
45 public function restoreGlobalVariables(Snapshot $snapshot)
47 $superGlobalArrays = $snapshot->superGlobalArrays();
49 foreach ($superGlobalArrays as $superGlobalArray) {
50 $this->restoreSuperGlobalArray($snapshot, $superGlobalArray);
53 $globalVariables = $snapshot->globalVariables();
55 foreach (array_keys($GLOBALS) as $key) {
56 if ($key != 'GLOBALS' &&
57 !in_array($key, $superGlobalArrays) &&
58 !$snapshot->blacklist()->isGlobalVariableBlacklisted($key)) {
59 if (isset($globalVariables[$key])) {
60 $GLOBALS[$key] = $globalVariables[$key];
62 unset($GLOBALS[$key]);
69 * Restores all static attributes in user-defined classes from this snapshot.
71 * @param Snapshot $snapshot
73 public function restoreStaticAttributes(Snapshot $snapshot)
75 $current = new Snapshot($snapshot->blacklist(), false, false, false, false, true, false, false, false, false);
76 $newClasses = array_diff($current->classes(), $snapshot->classes());
79 foreach ($snapshot->staticAttributes() as $className => $staticAttributes) {
80 foreach ($staticAttributes as $name => $value) {
81 $reflector = new ReflectionProperty($className, $name);
82 $reflector->setAccessible(true);
83 $reflector->setValue($value);
87 foreach ($newClasses as $className) {
88 $class = new \ReflectionClass($className);
89 $defaults = $class->getDefaultProperties();
91 foreach ($class->getProperties() as $attribute) {
92 if (!$attribute->isStatic()) {
96 $name = $attribute->getName();
98 if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
102 if (!isset($defaults[$name])) {
106 $attribute->setAccessible(true);
107 $attribute->setValue($defaults[$name]);
113 * Restores a super-global variable array from this snapshot.
115 * @param Snapshot $snapshot
116 * @param $superGlobalArray
118 private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
120 $superGlobalVariables = $snapshot->superGlobalVariables();
122 if (isset($GLOBALS[$superGlobalArray]) &&
123 is_array($GLOBALS[$superGlobalArray]) &&
124 isset($superGlobalVariables[$superGlobalArray])) {
127 $GLOBALS[$superGlobalArray],
128 $superGlobalVariables[$superGlobalArray]
132 foreach ($keys as $key) {
133 if (isset($superGlobalVariables[$superGlobalArray][$key])) {
134 $GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
136 unset($GLOBALS[$superGlobalArray][$key]);