Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
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
namespace PHPUnit\Util;
11
 
12
use function array_keys;
13
use function array_reverse;
14
use function array_shift;
15
use function defined;
16
use function get_defined_constants;
17
use function get_included_files;
18
use function in_array;
19
use function ini_get_all;
20
use function is_array;
21
use function is_file;
22
use function is_scalar;
23
use function preg_match;
24
use function serialize;
25
use function sprintf;
26
use function strpos;
27
use function strtr;
28
use function substr;
29
use function var_export;
30
use Closure;
31
 
32
/**
33
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
34
 */
35
final class GlobalState
36
{
37
    /**
38
     * @var string[]
39
     */
40
    private const SUPER_GLOBAL_ARRAYS = [
41
        '_ENV',
42
        '_POST',
43
        '_GET',
44
        '_COOKIE',
45
        '_SERVER',
46
        '_FILES',
47
        '_REQUEST',
48
    ];
49
 
50
    /**
51
     * @throws Exception
52
     */
53
    public static function getIncludedFilesAsString(): string
54
    {
55
        return self::processIncludedFilesAsString(get_included_files());
56
    }
57
 
58
    /**
59
     * @param string[] $files
60
     *
61
     * @throws Exception
62
     */
63
    public static function processIncludedFilesAsString(array $files): string
64
    {
65
        $excludeList = new ExcludeList;
66
        $prefix      = false;
67
        $result      = '';
68
 
69
        if (defined('__PHPUNIT_PHAR__')) {
70
            $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/';
71
        }
72
 
73
        // Do not process bootstrap script
74
        array_shift($files);
75
 
76
        // If bootstrap script was a Composer bin proxy, skip the second entry as well
77
        if (substr(strtr($files[0], '\\', '/'), -24) === '/phpunit/phpunit/phpunit') {
78
            array_shift($files);
79
        }
80
 
81
        foreach (array_reverse($files) as $file) {
82
            if (!empty($GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST']) &&
83
                in_array($file, $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'], true)) {
84
                continue;
85
            }
86
 
87
            if ($prefix !== false && strpos($file, $prefix) === 0) {
88
                continue;
89
            }
90
 
91
            // Skip virtual file system protocols
92
            if (preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file)) {
93
                continue;
94
            }
95
 
96
            if (!$excludeList->isExcluded($file) && is_file($file)) {
97
                $result = 'require_once \'' . $file . "';\n" . $result;
98
            }
99
        }
100
 
101
        return $result;
102
    }
103
 
104
    public static function getIniSettingsAsString(): string
105
    {
106
        $result = '';
107
 
108
        foreach (ini_get_all(null, false) as $key => $value) {
109
            $result .= sprintf(
110
                '@ini_set(%s, %s);' . "\n",
111
                self::exportVariable($key),
112
                self::exportVariable((string) $value)
113
            );
114
        }
115
 
116
        return $result;
117
    }
118
 
119
    public static function getConstantsAsString(): string
120
    {
121
        $constants = get_defined_constants(true);
122
        $result    = '';
123
 
124
        if (isset($constants['user'])) {
125
            foreach ($constants['user'] as $name => $value) {
126
                $result .= sprintf(
127
                    'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
128
                    $name,
129
                    $name,
130
                    self::exportVariable($value)
131
                );
132
            }
133
        }
134
 
135
        return $result;
136
    }
137
 
138
    public static function getGlobalsAsString(): string
139
    {
140
        $result = '';
141
 
142
        foreach (self::SUPER_GLOBAL_ARRAYS as $superGlobalArray) {
143
            if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
144
                foreach (array_keys($GLOBALS[$superGlobalArray]) as $key) {
145
                    if ($GLOBALS[$superGlobalArray][$key] instanceof Closure) {
146
                        continue;
147
                    }
148
 
149
                    $result .= sprintf(
150
                        '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
151
                        $superGlobalArray,
152
                        $key,
153
                        self::exportVariable($GLOBALS[$superGlobalArray][$key])
154
                    );
155
                }
156
            }
157
        }
158
 
159
        $excludeList   = self::SUPER_GLOBAL_ARRAYS;
160
        $excludeList[] = 'GLOBALS';
161
 
162
        foreach (array_keys($GLOBALS) as $key) {
163
            if (!$GLOBALS[$key] instanceof Closure && !in_array($key, $excludeList, true)) {
164
                $result .= sprintf(
165
                    '$GLOBALS[\'%s\'] = %s;' . "\n",
166
                    $key,
167
                    self::exportVariable($GLOBALS[$key])
168
                );
169
            }
170
        }
171
 
172
        return $result;
173
    }
174
 
175
    private static function exportVariable($variable): string
176
    {
177
        if (is_scalar($variable) || $variable === null ||
178
            (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
179
            return var_export($variable, true);
180
        }
181
 
182
        return 'unserialize(' . var_export(serialize($variable), true) . ')';
183
    }
184
 
185
    private static function arrayOnlyContainsScalars(array $array): bool
186
    {
187
        $result = true;
188
 
189
        foreach ($array as $element) {
190
            if (is_array($element)) {
191
                $result = self::arrayOnlyContainsScalars($element);
192
            } elseif (!is_scalar($element) && $element !== null) {
193
                $result = false;
194
            }
195
 
196
            if (!$result) {
197
                break;
198
            }
199
        }
200
 
201
        return $result;
202
    }
203
}