bootstrap.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php declare(strict_types = 1);
  2. namespace PHPStan;
  3. use Composer\Autoload\ClassLoader;
  4. use function class_exists;
  5. use const PHP_VERSION_ID;
  6. final class PharAutoloader
  7. {
  8. /** @var ClassLoader */
  9. private static $composerAutoloader;
  10. /** @var bool */
  11. private static $polyfillsLoaded = false;
  12. final public static function loadClass(string $class): void {
  13. if (!extension_loaded('phar') || defined('__PHPSTAN_RUNNING__')) {
  14. return;
  15. }
  16. if (strpos($class, '_PHPStan_') === 0) {
  17. if (!in_array('phar', stream_get_wrappers(), true)) {
  18. throw new \Exception('Phar wrapper is not registered. Please review your php.ini settings.');
  19. }
  20. if (self::$composerAutoloader === null) {
  21. self::$composerAutoloader = require 'phar://' . __DIR__ . '/phpstan.phar/vendor/autoload.php';
  22. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/jetbrains/phpstorm-stubs/PhpStormStubsMap.php';
  23. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/react/async/src/functions_include.php';
  24. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/react/promise-timer/src/functions_include.php';
  25. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/react/promise/src/functions_include.php';
  26. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/ringcentral/psr7/src/functions_include.php';
  27. }
  28. self::$composerAutoloader->loadClass($class);
  29. return;
  30. }
  31. if (strpos($class, 'PHPStan\\') !== 0 || strpos($class, 'PHPStan\\PhpDocParser\\') === 0) {
  32. return;
  33. }
  34. if (!in_array('phar', stream_get_wrappers(), true)) {
  35. throw new \Exception('Phar wrapper is not registered. Please review your php.ini settings.');
  36. }
  37. if (!self::$polyfillsLoaded) {
  38. self::$polyfillsLoaded = true;
  39. if (
  40. PHP_VERSION_ID < 80000
  41. && empty($GLOBALS['__composer_autoload_files']['a4a119a56e50fbb293281d9a48007e0e'])
  42. && !class_exists(\Symfony\Polyfill\Php80\Php80::class, false)
  43. ) {
  44. $GLOBALS['__composer_autoload_files']['a4a119a56e50fbb293281d9a48007e0e'] = true;
  45. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php80/Php80.php';
  46. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php80/bootstrap.php';
  47. }
  48. if (
  49. empty($GLOBALS['__composer_autoload_files']['0e6d7bf4a5811bfa5cf40c5ccd6fae6a'])
  50. && !class_exists(\Symfony\Polyfill\Mbstring\Mbstring::class, false)
  51. ) {
  52. $GLOBALS['__composer_autoload_files']['0e6d7bf4a5811bfa5cf40c5ccd6fae6a'] = true;
  53. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-mbstring/Mbstring.php';
  54. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-mbstring/bootstrap.php';
  55. }
  56. if (
  57. empty($GLOBALS['__composer_autoload_files']['e69f7f6ee287b969198c3c9d6777bd38'])
  58. && !class_exists(\Symfony\Polyfill\Intl\Normalizer\Normalizer::class, false)
  59. ) {
  60. $GLOBALS['__composer_autoload_files']['e69f7f6ee287b969198c3c9d6777bd38'] = true;
  61. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-intl-normalizer/Normalizer.php';
  62. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-intl-normalizer/bootstrap.php';
  63. }
  64. if (
  65. PHP_VERSION_ID < 70300
  66. && empty($GLOBALS['__composer_autoload_files']['0d59ee240a4cd96ddbb4ff164fccea4d'])
  67. && !class_exists(\Symfony\Polyfill\Php73\Php73::class, false)
  68. ) {
  69. $GLOBALS['__composer_autoload_files']['0d59ee240a4cd96ddbb4ff164fccea4d'] = true;
  70. // already loaded by bootstrap inside the hrtime condition
  71. // require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php73/Php73.php';
  72. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php73/bootstrap.php';
  73. }
  74. if (
  75. PHP_VERSION_ID < 70400
  76. && empty($GLOBALS['__composer_autoload_files']['b686b8e46447868025a15ce5d0cb2634'])
  77. && !class_exists(\Symfony\Polyfill\Php74\Php74::class, false)
  78. ) {
  79. $GLOBALS['__composer_autoload_files']['b686b8e46447868025a15ce5d0cb2634'] = true;
  80. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php74/Php74.php';
  81. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php74/bootstrap.php';
  82. }
  83. if (
  84. !extension_loaded('intl')
  85. && empty($GLOBALS['__composer_autoload_files']['8825ede83f2f289127722d4e842cf7e8'])
  86. && !class_exists(\Symfony\Polyfill\Intl\Grapheme\Grapheme::class, false)
  87. ) {
  88. $GLOBALS['__composer_autoload_files']['8825ede83f2f289127722d4e842cf7e8'] = true;
  89. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-intl-grapheme/Grapheme.php';
  90. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-intl-grapheme/bootstrap.php';
  91. }
  92. if (
  93. PHP_VERSION_ID < 80100
  94. && empty ($GLOBALS['__composer_autoload_files']['23c18046f52bef3eea034657bafda50f'])
  95. && !class_exists(\Symfony\Polyfill\Php81\Php81::class, false)
  96. ) {
  97. $GLOBALS['__composer_autoload_files']['23c18046f52bef3eea034657bafda50f'] = true;
  98. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php81/Php81.php';
  99. require_once 'phar://' . __DIR__ . '/phpstan.phar/vendor/symfony/polyfill-php81/bootstrap.php';
  100. }
  101. }
  102. $filename = str_replace('\\', DIRECTORY_SEPARATOR, $class);
  103. if (strpos($class, 'PHPStan\\BetterReflection\\') === 0) {
  104. $filename = substr($filename, strlen('PHPStan\\BetterReflection\\'));
  105. $filepath = 'phar://' . __DIR__ . '/phpstan.phar/vendor/ondrejmirtes/better-reflection/src/' . $filename . '.php';
  106. } else {
  107. $filename = substr($filename, strlen('PHPStan\\'));
  108. $filepath = 'phar://' . __DIR__ . '/phpstan.phar/src/' . $filename . '.php';
  109. }
  110. if (!file_exists($filepath)) {
  111. return;
  112. }
  113. require $filepath;
  114. }
  115. }
  116. spl_autoload_register([PharAutoloader::class, 'loadClass']);