/laravel_tintuc/vendor/phpunit/phpunit/src/Util/GlobalState.php

https://gitlab.com/nmhieucoder/laravel_tintuc · PHP · 195 lines · 161 code · 10 blank · 24 comment · 7 complexity · 9ff710317fc6b912fd67cfb5e4ed2fcd MD5 · raw file

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