Yaffs site version 1.1
[yaffs-website] / vendor / phpunit / phpunit / src / Util / GlobalState.php
1 <?php
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
11 /**
12  * @since Class available since Release 3.4.0
13  */
14 class PHPUnit_Util_GlobalState
15 {
16     /**
17      * @var array
18      */
19     protected static $superGlobalArrays = array(
20       '_ENV',
21       '_POST',
22       '_GET',
23       '_COOKIE',
24       '_SERVER',
25       '_FILES',
26       '_REQUEST'
27     );
28
29     /**
30      * @var array
31      */
32     protected static $superGlobalArraysLong = array(
33       'HTTP_ENV_VARS',
34       'HTTP_POST_VARS',
35       'HTTP_GET_VARS',
36       'HTTP_COOKIE_VARS',
37       'HTTP_SERVER_VARS',
38       'HTTP_POST_FILES'
39     );
40
41     public static function getIncludedFilesAsString()
42     {
43         return static::processIncludedFilesAsString(get_included_files());
44     }
45
46     public static function processIncludedFilesAsString(array $files)
47     {
48         $blacklist = new PHPUnit_Util_Blacklist;
49         $prefix    = false;
50         $result    = '';
51
52         if (defined('__PHPUNIT_PHAR__')) {
53             $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/';
54         }
55
56         for ($i = count($files) - 1; $i > 0; $i--) {
57             $file = $files[$i];
58
59             if ($prefix !== false && strpos($file, $prefix) === 0) {
60                 continue;
61             }
62
63             // Skip virtual file system protocols
64             if (preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file)) {
65                 continue;
66             }
67
68             if (!$blacklist->isBlacklisted($file) && is_file($file)) {
69                 $result = 'require_once \'' . $file . "';\n" . $result;
70             }
71         }
72
73         return $result;
74     }
75
76     public static function getIniSettingsAsString()
77     {
78         $result      = '';
79         $iniSettings = ini_get_all(null, false);
80
81         foreach ($iniSettings as $key => $value) {
82             $result .= sprintf(
83                 '@ini_set(%s, %s);' . "\n",
84                 self::exportVariable($key),
85                 self::exportVariable($value)
86             );
87         }
88
89         return $result;
90     }
91
92     public static function getConstantsAsString()
93     {
94         $constants = get_defined_constants(true);
95         $result    = '';
96
97         if (isset($constants['user'])) {
98             foreach ($constants['user'] as $name => $value) {
99                 $result .= sprintf(
100                     'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
101                     $name,
102                     $name,
103                     self::exportVariable($value)
104                 );
105             }
106         }
107
108         return $result;
109     }
110
111     public static function getGlobalsAsString()
112     {
113         $result            = '';
114         $superGlobalArrays = self::getSuperGlobalArrays();
115
116         foreach ($superGlobalArrays as $superGlobalArray) {
117             if (isset($GLOBALS[$superGlobalArray]) &&
118                 is_array($GLOBALS[$superGlobalArray])) {
119                 foreach (array_keys($GLOBALS[$superGlobalArray]) as $key) {
120                     if ($GLOBALS[$superGlobalArray][$key] instanceof Closure) {
121                         continue;
122                     }
123
124                     $result .= sprintf(
125                         '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
126                         $superGlobalArray,
127                         $key,
128                         self::exportVariable($GLOBALS[$superGlobalArray][$key])
129                     );
130                 }
131             }
132         }
133
134         $blacklist   = $superGlobalArrays;
135         $blacklist[] = 'GLOBALS';
136
137         foreach (array_keys($GLOBALS) as $key) {
138             if (!in_array($key, $blacklist) && !$GLOBALS[$key] instanceof Closure) {
139                 $result .= sprintf(
140                     '$GLOBALS[\'%s\'] = %s;' . "\n",
141                     $key,
142                     self::exportVariable($GLOBALS[$key])
143                 );
144             }
145         }
146
147         return $result;
148     }
149
150     protected static function getSuperGlobalArrays()
151     {
152         if (ini_get('register_long_arrays') == '1') {
153             return array_merge(
154                 self::$superGlobalArrays,
155                 self::$superGlobalArraysLong
156             );
157         } else {
158             return self::$superGlobalArrays;
159         }
160     }
161
162     protected static function exportVariable($variable)
163     {
164         if (is_scalar($variable) || is_null($variable) ||
165            (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
166             return var_export($variable, true);
167         }
168
169         return 'unserialize(' .
170                 var_export(serialize($variable), true) .
171                 ')';
172     }
173
174     protected static function arrayOnlyContainsScalars(array $array)
175     {
176         $result = true;
177
178         foreach ($array as $element) {
179             if (is_array($element)) {
180                 $result = self::arrayOnlyContainsScalars($element);
181             } elseif (!is_scalar($element) && !is_null($element)) {
182                 $result = false;
183             }
184
185             if ($result === false) {
186                 break;
187             }
188         }
189
190         return $result;
191     }
192 }