3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * @since Class available since Release 3.4.0
14 class PHPUnit_Util_GlobalState
19 protected static $superGlobalArrays = array(
32 protected static $superGlobalArraysLong = array(
41 public static function getIncludedFilesAsString()
43 return static::processIncludedFilesAsString(get_included_files());
46 public static function processIncludedFilesAsString(array $files)
48 $blacklist = new PHPUnit_Util_Blacklist;
52 if (defined('__PHPUNIT_PHAR__')) {
53 $prefix = 'phar://' . __PHPUNIT_PHAR__ . '/';
56 for ($i = count($files) - 1; $i > 0; $i--) {
59 if ($prefix !== false && strpos($file, $prefix) === 0) {
63 // Skip virtual file system protocols
64 if (preg_match('/^(vfs|phpvfs[a-z0-9]+):/', $file)) {
68 if (!$blacklist->isBlacklisted($file) && is_file($file)) {
69 $result = 'require_once \'' . $file . "';\n" . $result;
76 public static function getIniSettingsAsString()
79 $iniSettings = ini_get_all(null, false);
81 foreach ($iniSettings as $key => $value) {
83 '@ini_set(%s, %s);' . "\n",
84 self::exportVariable($key),
85 self::exportVariable($value)
92 public static function getConstantsAsString()
94 $constants = get_defined_constants(true);
97 if (isset($constants['user'])) {
98 foreach ($constants['user'] as $name => $value) {
100 'if (!defined(\'%s\')) define(\'%s\', %s);' . "\n",
103 self::exportVariable($value)
111 public static function getGlobalsAsString()
114 $superGlobalArrays = self::getSuperGlobalArrays();
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) {
125 '$GLOBALS[\'%s\'][\'%s\'] = %s;' . "\n",
128 self::exportVariable($GLOBALS[$superGlobalArray][$key])
134 $blacklist = $superGlobalArrays;
135 $blacklist[] = 'GLOBALS';
137 foreach (array_keys($GLOBALS) as $key) {
138 if (!in_array($key, $blacklist) && !$GLOBALS[$key] instanceof Closure) {
140 '$GLOBALS[\'%s\'] = %s;' . "\n",
142 self::exportVariable($GLOBALS[$key])
150 protected static function getSuperGlobalArrays()
152 if (ini_get('register_long_arrays') == '1') {
154 self::$superGlobalArrays,
155 self::$superGlobalArraysLong
158 return self::$superGlobalArrays;
162 protected static function exportVariable($variable)
164 if (is_scalar($variable) || is_null($variable) ||
165 (is_array($variable) && self::arrayOnlyContainsScalars($variable))) {
166 return var_export($variable, true);
169 return 'unserialize(' .
170 var_export(serialize($variable), true) .
174 protected static function arrayOnlyContainsScalars(array $array)
178 foreach ($array as $element) {
179 if (is_array($element)) {
180 $result = self::arrayOnlyContainsScalars($element);
181 } elseif (!is_scalar($element) && !is_null($element)) {
185 if ($result === false) {