~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/sebastian/global-state/src/Restorer.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * GlobalState
 
4
 *
 
5
 * Copyright (c) 2001-2014, Sebastian Bergmann <sebastian@phpunit.de>.
 
6
 * All rights reserved.
 
7
 *
 
8
 * Redistribution and use in source and binary forms, with or without
 
9
 * modification, are permitted provided that the following conditions
 
10
 * are met:
 
11
 *
 
12
 *   * Redistributions of source code must retain the above copyright
 
13
 *     notice, this list of conditions and the following disclaimer.
 
14
 *
 
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
 
18
 *     distribution.
 
19
 *
 
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.
 
23
 *
 
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.
 
36
 *
 
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
 
41
 */
 
42
 
 
43
namespace SebastianBergmann\GlobalState;
 
44
 
 
45
use ReflectionProperty;
 
46
 
 
47
/**
 
48
 * Restorer of snapshots of global state.
 
49
 *
 
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
 
54
 */
 
55
class Restorer
 
56
{
 
57
    /**
 
58
     * Deletes function definitions that are not defined in a snapshot.
 
59
     *
 
60
     * @param  Snapshot $snapshot
 
61
     * @throws RuntimeException when the uopz_delete() function is not available
 
62
     * @see    https://github.com/krakjoe/uopz
 
63
     */
 
64
    public function restoreFunctions(Snapshot $snapshot)
 
65
    {
 
66
        if (!function_exists('uopz_delete')) {
 
67
            throw new RuntimeException('The uopz_delete() function is required for this operation');
 
68
        }
 
69
 
 
70
        $functions = get_defined_functions();
 
71
 
 
72
        foreach (array_diff($functions['user'], $snapshot->functions()) as $function) {
 
73
            uopz_delete($function);
 
74
        }
 
75
    }
 
76
 
 
77
    /**
 
78
     * Restores all global and super-global variables from a snapshot.
 
79
     *
 
80
     * @param Snapshot $snapshot
 
81
     */
 
82
    public function restoreGlobalVariables(Snapshot $snapshot)
 
83
    {
 
84
        $superGlobalArrays = $snapshot->superGlobalArrays();
 
85
 
 
86
        foreach ($superGlobalArrays as $superGlobalArray) {
 
87
            $this->restoreSuperGlobalArray($snapshot, $superGlobalArray);
 
88
        }
 
89
 
 
90
        $globalVariables = $snapshot->globalVariables();
 
91
 
 
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];
 
98
                } else {
 
99
                    unset($GLOBALS[$key]);
 
100
                }
 
101
            }
 
102
        }
 
103
    }
 
104
 
 
105
    /**
 
106
     * Restores all static attributes in user-defined classes from this snapshot.
 
107
     *
 
108
     * @param Snapshot $snapshot
 
109
     */
 
110
    public function restoreStaticAttributes(Snapshot $snapshot)
 
111
    {
 
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);
 
117
            }
 
118
        }
 
119
    }
 
120
 
 
121
    /**
 
122
     * Restores a super-global variable array from this snapshot.
 
123
     *
 
124
     * @param Snapshot $snapshot
 
125
     * @param $superGlobalArray
 
126
     */
 
127
    private function restoreSuperGlobalArray(Snapshot $snapshot, $superGlobalArray)
 
128
    {
 
129
        $superGlobalVariables = $snapshot->superGlobalVariables();
 
130
 
 
131
        if (isset($GLOBALS[$superGlobalArray]) &&
 
132
            is_array($GLOBALS[$superGlobalArray]) &&
 
133
            isset($superGlobalVariables[$superGlobalArray])) {
 
134
            $keys = array_keys(
 
135
                array_merge(
 
136
                    $GLOBALS[$superGlobalArray],
 
137
                    $superGlobalVariables[$superGlobalArray]
 
138
                )
 
139
            );
 
140
 
 
141
            foreach ($keys as $key) {
 
142
                if (isset($superGlobalVariables[$superGlobalArray][$key])) {
 
143
                    $GLOBALS[$superGlobalArray][$key] = $superGlobalVariables[$superGlobalArray][$key];
 
144
                } else {
 
145
                    unset($GLOBALS[$superGlobalArray][$key]);
 
146
                }
 
147
            }
 
148
        }
 
149
    }
 
150
}