| @@ -41,8 +41,11 @@ | ||
| 41 | 41 | * @see https://www.php-fig.org/psr/psr-4/ |
| 42 | 42 | */ |
| 43 | 43 | class ClassLoader |
| 44 | 44 | { |
| 45 | + /** @var \Closure(string):void */ | |
| 46 | + private static $includeFile; | |
| 47 | + | |
| 45 | 48 | /** @var ?string */ |
| 46 | 49 | private $vendorDir; |
| 47 | 50 | |
| 48 | 51 | // PSR-4 |
| @@ -105,8 +108,9 @@ | ||
| 105 | 108 | */ |
| 106 | 109 | public function __construct($vendorDir = null) |
| 107 | 110 | { |
| 108 | 111 | $this->vendorDir = $vendorDir; |
| 112 | + self::initializeIncludeClosure(); | |
| 109 | 113 | } |
| 110 | 114 | |
| 111 | 115 | /** |
| 112 | 116 | * @return string[] |
| @@ -148,9 +152,9 @@ | ||
| 148 | 152 | } |
| 149 | 153 | |
| 150 | 154 | /** |
| 151 | 155 | * @return string[] Array of classname => path |
| 152 | - * @psalm-var array<string, string> | |
| 156 | + * @psalm-return array<string, string> | |
| 153 | 157 | */ |
| 154 | 158 | public function getClassMap() |
| 155 | 159 | { |
| 156 | 160 | return $this->classMap; |
| @@ -424,9 +428,9 @@ | ||
| 424 | 428 | */ |
| 425 | 429 | public function loadClass($class) |
| 426 | 430 | { |
| 427 | 431 | if ($file = $this->findFile($class)) { |
| 428 | - includeFile($file); | |
| 432 | + (self::$includeFile)($file); | |
| 429 | 433 | |
| 430 | 434 | return true; |
| 431 | 435 | } |
| 432 | 436 | |
| @@ -554,19 +558,24 @@ | ||
| 554 | 558 | } |
| 555 | 559 | |
| 556 | 560 | return false; |
| 557 | 561 | } |
| 558 | -} | |
| 559 | 562 | |
| 560 | -/** | |
| 561 | - * Scope isolated include. | |
| 562 | - * | |
| 563 | - * Prevents access to $this/self from included files. | |
| 564 | - * | |
| 565 | - * @param string $file | |
| 566 | - * @return void | |
| 567 | - * @private | |
| 568 | - */ | |
| 569 | -function includeFile($file) | |
| 570 | -{ | |
| 571 | - include $file; | |
| 563 | + private static function initializeIncludeClosure(): void | |
| 564 | + { | |
| 565 | + if (self::$includeFile !== null) { | |
| 566 | + return; | |
| 567 | + } | |
| 568 | + | |
| 569 | + /** | |
| 570 | + * Scope isolated include. | |
| 571 | + * | |
| 572 | + * Prevents access to $this/self from included files. | |
| 573 | + * | |
| 574 | + * @param string $file | |
| 575 | + * @return void | |
| 576 | + */ | |
| 577 | + self::$includeFile = static function($file) { | |
| 578 | + include $file; | |
| 579 | + }; | |
| 580 | + } | |
| 572 | 581 | } |