Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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
 
991 lars 12
use const PHP_MAJOR_VERSION;
13
use const PHP_MINOR_VERSION;
148 lars 14
use function array_keys;
15
use function array_reverse;
16
use function array_shift;
17
use function defined;
18
use function get_defined_constants;
19
use function get_included_files;
20
use function in_array;
21
use function ini_get_all;
22
use function is_array;
23
use function is_file;
24
use function is_scalar;
25
use function preg_match;
26
use function serialize;
27
use function sprintf;
28
use function strpos;
29
use function strtr;
30
use function substr;
31
use function var_export;
32
use Closure;
33
 
34
/**
35
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
36
 */
37
final class GlobalState
38
{
39
    /**
40
     * @var string[]
41
     */
42
    private const SUPER_GLOBAL_ARRAYS = [
43
        '_ENV',
44
        '_POST',
45
        '_GET',
46
        '_COOKIE',
47
        '_SERVER',
48
        '_FILES',
49
        '_REQUEST',
50
    ];
51
 
52
    /**
991 lars 53
     * @psalm-var array<string, array<string, true>>
54
     */
55
    private const DEPRECATED_INI_SETTINGS = [
56
        '7.3' => [
57
            'iconv.input_encoding'       => true,
58
            'iconv.output_encoding'      => true,
59
            'iconv.internal_encoding'    => true,
60
            'mbstring.func_overload'     => true,
61
            'mbstring.http_input'        => true,
62
            'mbstring.http_output'       => true,
63
            'mbstring.internal_encoding' => true,
64
            'string.strip_tags'          => true,
65
        ],
66
 
67
        '7.4' => [
68
            'iconv.input_encoding'       => true,
69
            'iconv.output_encoding'      => true,
70
            'iconv.internal_encoding'    => true,
71
            'mbstring.func_overload'     => true,
72
            'mbstring.http_input'        => true,
73
            'mbstring.http_output'       => true,
74
            'mbstring.internal_encoding' => true,
75
            'pdo_odbc.db2_instance_name' => true,
76
            'string.strip_tags'          => true,
77
        ],
78
 
79
        '8.0' => [
80
            'iconv.input_encoding'       => true,
81
            'iconv.output_encoding'      => true,
82
            'iconv.internal_encoding'    => true,
83
            'mbstring.http_input'        => true,
84
            'mbstring.http_output'       => true,
85
            'mbstring.internal_encoding' => true,
86
        ],
87
 
88
        '8.1' => [
89
            'auto_detect_line_endings'     => true,
90
            'filter.default'               => true,
91
            'iconv.input_encoding'         => true,
92
            'iconv.output_encoding'        => true,
93
            'iconv.internal_encoding'      => true,
94
            'mbstring.http_input'          => true,
95
            'mbstring.http_output'         => true,
96
            'mbstring.internal_encoding'   => true,
97
            'oci8.old_oci_close_semantics' => true,
98
        ],
99
 
100
        '8.2' => [
101
            'auto_detect_line_endings'     => true,
102
            'filter.default'               => true,
103
            'iconv.input_encoding'         => true,
104
            'iconv.output_encoding'        => true,
105
            'iconv.internal_encoding'      => true,
106
            'mbstring.http_input'          => true,
107
            'mbstring.http_output'         => true,
108
            'mbstring.internal_encoding'   => true,
109
            'oci8.old_oci_close_semantics' => true,
110
        ],
111
 
112
        '8.3' => [
113
            'auto_detect_line_endings'     => true,
114
            'filter.default'               => true,
115
            'iconv.input_encoding'         => true,
116
            'iconv.output_encoding'        => true,
117
            'iconv.internal_encoding'      => true,
118
            'mbstring.http_input'          => true,
119
            'mbstring.http_output'         => true,
120
            'mbstring.internal_encoding'   => true,
121
            'oci8.old_oci_close_semantics' => true,
122
        ],
123
    ];
124
 
125
    /**
148 lars 126
     * @throws Exception
127
     */
128
    public static function getIncludedFilesAsString(): string
129
    {
130
        return self::processIncludedFilesAsString(get_included_files());
131
    }
132
 
133
    /**
134
     * @param string[] $files
135
     *
136
     * @throws Exception
137
     */
138
    public static function processIncludedFilesAsString(array $files): string
139
    {
140
        $excludeList = new ExcludeList;
141
        $prefix      = false;
142
        $result      = '';
143
 
144
        if (defined('__PHPUNIT_PHAR__')) {
145
            $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/';
146
        }
147
 
148
        // Do not process bootstrap script
149
        array_shift($files);
150
 
151
        // If bootstrap script was a Composer bin proxy, skip the second entry as well
152
        if (substr(strtr($files[0], '\\', '/'), -24) === '/phpunit/phpunit/phpunit') {
153
            array_shift($files);
154
        }
155
 
156
        foreach (array_reverse($files) as $file) {
157
            if (!empty($GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST']) &&
158
                in_array($file, $GLOBALS['__PHPUNIT_ISOLATION_EXCLUDE_LIST'], true)) {
159
                continue;
160
            }
161
 
162
            if ($prefix !== false && strpos($file, $prefix) === 0) {
163
                continue;
164
            }
165
 
166
            // Skip virtual file system protocols
167
            if (preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file)) {
168
                continue;
169
            }
170
 
171
            if (!$excludeList->isExcluded($file) && is_file($file)) {
172
                $result = 'require_once \'' . $file . "';\n" . $result;
173
            }
174
        }
175
 
176
        return $result;
177
    }
178
 
179
    public static function getIniSettingsAsString(): string
180
    {
181
        $result = '';
182
 
183
        foreach (ini_get_all(null, false) as $key => $value) {
991 lars 184
            if (self::isIniSettingDeprecated($key)) {
185
                continue;
186
            }
187
 
148 lars 188
            $result .= sprintf(
189
                '@ini_set(%s, %s);' . "\n",
190
                self::exportVariable($key),
191
                self::exportVariable((string) $value)
192
            );
193
        }
194
 
195
        return $result;
196
    }
197
 
198
    public static function getConstantsAsString(): string
199
    {
200
        $constants = get_defined_constants(true);
201
        $result    = '';
202
 
203
        if (isset($constants['user'])) {
204
            foreach ($constants['user'] as $name => $value) {
205
                $result .= sprintf(
206
                    'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
207
                    $name,
208
                    $name,
209
                    self::exportVariable($value)
210
                );
211
            }
212
        }
213
 
214
        return $result;
215
    }
216
 
217
    public static function getGlobalsAsString(): string
218
    {
219
        $result = '';
220
 
221
        foreach (self::SUPER_GLOBAL_ARRAYS as $superGlobalArray) {
222
            if (isset($GLOBALS[$superGlobalArray]) && is_array($GLOBALS[$superGlobalArray])) {
223
                foreach (array_keys($GLOBALS[$superGlobalArray]) as $key) {
224
                    if ($GLOBALS[$superGlobalArray][$key] instanceof Closure) {
225
                        continue;
226
                    }
227
 
228
                    $result .= sprintf(
229
                        '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
230
                        $superGlobalArray,
231
                        $key,
232
                        self::exportVariable($GLOBALS[$superGlobalArray][$key])
233
                    );
234
                }
235
            }
236
        }
237
 
238
        $excludeList   = self::SUPER_GLOBAL_ARRAYS;
239
        $excludeList[] = 'GLOBALS';
240
 
241
        foreach (array_keys($GLOBALS) as $key) {
242
            if (!$GLOBALS[$key] instanceof Closure && !in_array($key, $excludeList, true)) {
243
                $result .= sprintf(
244
                    '$GLOBALS[\'%s\'] = %s;' . "\n",
245
                    $key,
246
                    self::exportVariable($GLOBALS[$key])
247
                );
248
            }
249
        }
250
 
251
        return $result;
252
    }
253
 
254
    private static function exportVariable($variable): string
255
    {
256
        if (is_scalar($variable) || $variable === null ||
257
            (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
258
            return var_export($variable, true);
259
        }
260
 
261
        return 'unserialize(' . var_export(serialize($variable), true) . ')';
262
    }
263
 
264
    private static function arrayOnlyContainsScalars(array $array): bool
265
    {
266
        $result = true;
267
 
268
        foreach ($array as $element) {
269
            if (is_array($element)) {
270
                $result = self::arrayOnlyContainsScalars($element);
271
            } elseif (!is_scalar($element) && $element !== null) {
272
                $result = false;
273
            }
274
 
275
            if (!$result) {
276
                break;
277
            }
278
        }
279
 
280
        return $result;
281
    }
991 lars 282
 
283
    private static function isIniSettingDeprecated(string $iniSetting): bool
284
    {
285
        return isset(self::DEPRECATED_INI_SETTINGS[PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION][$iniSetting]);
286
    }
148 lars 287
}