1<?php
2/*
3 * This file is part of the GlobalState package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\GlobalState;
12
13use ReflectionProperty;
14
15/**
16 * Restorer of snapshots of global state.
17 */
18class Restorer
19{
20    /**
21     * Deletes function definitions that are not defined in a snapshot.
22     *
23     * @param  Snapshot         $snapshot
24     * @throws RuntimeException when the uopz_delete() function is not available
25     * @see    https://github.com/krakjoe/uopz
26     */
27    public function restoreFunctions(Snapshot $snapshot)
28    {
29        if (!function_exists('uopz_delete')) {
30            throw new RuntimeException('The uopz_delete() function is required for this operation');
31        }
32
33        $functions = get_defined_functions();
34
35        foreach (array_diff($functions['user'], $snapshot->functions()) as $function) {
36            uopz_delete($function);
37        }
38    }
39
40    /**
41     * Restores all global and super-global variables from a snapshot.
42     *
43     * @param Snapshot $snapshot
44     */
45    public function restoreGlobalVariables(Snapshot $snapshot)
46    {
47        $superGlobalArrays = $snapshot->superGlobalArrays();
48
49        foreach ($superGlobalArrays as $superGlobalArray) {
50            $this->restoreSuperGlobalArray($snapshot, $superGlobalArray);
51        }
52
53        $globalVariables = $snapshot->globalVariables();
54
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];
61                } else {
62                    unset($GLOBALS[$key]);
63                }
64            }
65        }
66    }
67
68    /**
69     * Restores all static attributes in user-defined classes from this snapshot.
70     *
71     * @param Snapshot $snapshot
72     */
73    public function restoreStaticAttributes(Snapshot $snapshot)
74    {
75        $current    = new Snapshot($snapshot->blacklist(), false, false, false, false, true, false, false, false, false);
76        $newClasses = array_diff($current->classes(), $snapshot->classes());
77        unset($current);
78
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);
84            }
85        }
86
87        foreach ($newClasses as $className) {
88            $class    = new \ReflectionClass($className);
89            $defaults = $class->getDefaultProperties();
90
91            foreach ($class->getProperties() as $attribute) {
92                if (!$attribute->isStatic()) {
93                    continue;
94                }
95
96                $name = $attribute->getName();
97
98                if ($snapshot->blacklist()->isStaticAttributeBlacklisted($className, $name)) {
99                    continue;
100                }
101
102                if (!isset($defaults[$name])) {
103                    continue;
104                }
105
106                $attribute->setAccessible(true);
107                $attribute->setValue($defaults[$name]);
108            }
109        }
110    }
111
112    /**
113     * Restores a super-global variable array from this snapshot.
114     *
115     * @param Snapshot $snapshot
116     * @param $superGlobalArray
117     */
118    private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
119    {
120        $superGlobalVariables = $snapshot->superGlobalVariables();
121
122        if (isset($GLOBALS[$superGlobalArray]) &&
123            is_array($GLOBALS[$superGlobalArray]) &&
124            isset($superGlobalVariables[$superGlobalArray])) {
125            $keys = array_keys(
126                array_merge(
127                    $GLOBALS[$superGlobalArray],
128                    $superGlobalVariables[$superGlobalArray]
129                )
130            );
131
132            foreach ($keys as $key) {
133                if (isset($superGlobalVariables[$superGlobalArray][$key])) {
134                    $GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
135                } else {
136                    unset($GLOBALS[$superGlobalArray][$key]);
137                }
138            }
139        }
140    }
141}
142