| @@ -41,81 +41,32 @@ | ||
| 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 | - | |
| 48 | - /** @var ?string */ | |
| 49 | 45 | private $vendorDir; |
| 50 | 46 | |
| 51 | 47 | // PSR-4 |
| 52 | - /** | |
| 53 | - * @var array[] | |
| 54 | - * @psalm-var array<string, array<string, int>> | |
| 55 | - */ | |
| 56 | 48 | private $prefixLengthsPsr4 = array(); |
| 57 | - /** | |
| 58 | - * @var array[] | |
| 59 | - * @psalm-var array<string, array<int, string>> | |
| 60 | - */ | |
| 61 | 49 | private $prefixDirsPsr4 = array(); |
| 62 | - /** | |
| 63 | - * @var array[] | |
| 64 | - * @psalm-var array<string, string> | |
| 65 | - */ | |
| 66 | 50 | private $fallbackDirsPsr4 = array(); |
| 67 | 51 | |
| 68 | 52 | // PSR-0 |
| 69 | - /** | |
| 70 | - * @var array[] | |
| 71 | - * @psalm-var array<string, array<string, string[]>> | |
| 72 | - */ | |
| 73 | 53 | private $prefixesPsr0 = array(); |
| 74 | - /** | |
| 75 | - * @var array[] | |
| 76 | - * @psalm-var array<string, string> | |
| 77 | - */ | |
| 78 | 54 | private $fallbackDirsPsr0 = array(); |
| 79 | 55 | |
| 80 | - /** @var bool */ | |
| 81 | 56 | private $useIncludePath = false; |
| 82 | - | |
| 83 | - /** | |
| 84 | - * @var string[] | |
| 85 | - * @psalm-var array<string, string> | |
| 86 | - */ | |
| 87 | 57 | private $classMap = array(); |
| 88 | - | |
| 89 | - /** @var bool */ | |
| 90 | 58 | private $classMapAuthoritative = false; |
| 91 | - | |
| 92 | - /** | |
| 93 | - * @var bool[] | |
| 94 | - * @psalm-var array<string, bool> | |
| 95 | - */ | |
| 96 | 59 | private $missingClasses = array(); |
| 97 | - | |
| 98 | - /** @var ?string */ | |
| 99 | 60 | private $apcuPrefix; |
| 100 | 61 | |
| 101 | - /** | |
| 102 | - * @var self[] | |
| 103 | - */ | |
| 104 | 62 | private static $registeredLoaders = array(); |
| 105 | 63 | |
| 106 | - /** | |
| 107 | - * @param ?string $vendorDir | |
| 108 | - */ | |
| 109 | 64 | public function __construct($vendorDir = null) |
| 110 | 65 | { |
| 111 | 66 | $this->vendorDir = $vendorDir; |
| 112 | - self::initializeIncludeClosure(); | |
| 113 | 67 | } |
| 114 | 68 | |
| 115 | - /** | |
| 116 | - * @return string[] | |
| 117 | - */ | |
| 118 | 69 | public function getPrefixes() |
| 119 | 70 | { |
| 120 | 71 | if (!empty($this->prefixesPsr0)) { |
| 121 | 72 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
| @@ -123,39 +74,23 @@ | ||
| 123 | 74 | |
| 124 | 75 | return array(); |
| 125 | 76 | } |
| 126 | 77 | |
| 127 | - /** | |
| 128 | - * @return array[] | |
| 129 | - * @psalm-return array<string, array<int, string>> | |
| 130 | - */ | |
| 131 | 78 | public function getPrefixesPsr4() |
| 132 | 79 | { |
| 133 | 80 | return $this->prefixDirsPsr4; |
| 134 | 81 | } |
| 135 | 82 | |
| 136 | - /** | |
| 137 | - * @return array[] | |
| 138 | - * @psalm-return array<string, string> | |
| 139 | - */ | |
| 140 | 83 | public function getFallbackDirs() |
| 141 | 84 | { |
| 142 | 85 | return $this->fallbackDirsPsr0; |
| 143 | 86 | } |
| 144 | 87 | |
| 145 | - /** | |
| 146 | - * @return array[] | |
| 147 | - * @psalm-return array<string, string> | |
| 148 | - */ | |
| 149 | 88 | public function getFallbackDirsPsr4() |
| 150 | 89 | { |
| 151 | 90 | return $this->fallbackDirsPsr4; |
| 152 | 91 | } |
| 153 | 92 | |
| 154 | - /** | |
| 155 | - * @return string[] Array of classname => path | |
| 156 | - * @psalm-return array<string, string> | |
| 157 | - */ | |
| 158 | 93 | public function getClassMap() |
| 159 | 94 | { |
| 160 | 95 | return $this->classMap; |
| 161 | 96 | } |
| @@ -160,12 +95,9 @@ | ||
| 160 | 95 | return $this->classMap; |
| 161 | 96 | } |
| 162 | 97 | |
| 163 | 98 | /** |
| 164 | - * @param string[] $classMap Class to filename map | |
| 165 | - * @psalm-param array<string, string> $classMap | |
| 166 | - * | |
| 167 | - * @return void | |
| 99 | + * @param array $classMap Class to filename map | |
| 168 | 100 | */ |
| 169 | 101 | public function addClassMap(array $classMap) |
| 170 | 102 | { |
| 171 | 103 | if ($this->classMap) { |
| @@ -178,13 +110,11 @@ | ||
| 178 | 110 | /** |
| 179 | 111 | * Registers a set of PSR-0 directories for a given prefix, either |
| 180 | 112 | * appending or prepending to the ones previously set for this prefix. |
| 181 | 113 | * |
| 182 | - * @param string $prefix The prefix | |
| 183 | - * @param string[]|string $paths The PSR-0 root directories | |
| 184 | - * @param bool $prepend Whether to prepend the directories | |
| 185 | - * | |
| 186 | - * @return void | |
| 114 | + * @param string $prefix The prefix | |
| 115 | + * @param array|string $paths The PSR-0 root directories | |
| 116 | + * @param bool $prepend Whether to prepend the directories | |
| 187 | 117 | */ |
| 188 | 118 | public function add($prefix, $paths, $prepend = false) |
| 189 | 119 | { |
| 190 | 120 | if (!$prefix) { |
| @@ -225,15 +155,13 @@ | ||
| 225 | 155 | /** |
| 226 | 156 | * Registers a set of PSR-4 directories for a given namespace, either |
| 227 | 157 | * appending or prepending to the ones previously set for this namespace. |
| 228 | 158 | * |
| 229 | - * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 230 | - * @param string[]|string $paths The PSR-4 base directories | |
| 231 | - * @param bool $prepend Whether to prepend the directories | |
| 159 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 160 | + * @param array|string $paths The PSR-4 base directories | |
| 161 | + * @param bool $prepend Whether to prepend the directories | |
| 232 | 162 | * |
| 233 | 163 | * @throws \InvalidArgumentException |
| 234 | - * | |
| 235 | - * @return void | |
| 236 | 164 | */ |
| 237 | 165 | public function addPsr4($prefix, $paths, $prepend = false) |
| 238 | 166 | { |
| 239 | 167 | if (!$prefix) { |
| @@ -275,12 +203,10 @@ | ||
| 275 | 203 | /** |
| 276 | 204 | * Registers a set of PSR-0 directories for a given prefix, |
| 277 | 205 | * replacing any others previously set for this prefix. |
| 278 | 206 | * |
| 279 | - * @param string $prefix The prefix | |
| 280 | - * @param string[]|string $paths The PSR-0 base directories | |
| 281 | - * | |
| 282 | - * @return void | |
| 207 | + * @param string $prefix The prefix | |
| 208 | + * @param array|string $paths The PSR-0 base directories | |
| 283 | 209 | */ |
| 284 | 210 | public function set($prefix, $paths) |
| 285 | 211 | { |
| 286 | 212 | if (!$prefix) { |
| @@ -293,14 +219,12 @@ | ||
| 293 | 219 | /** |
| 294 | 220 | * Registers a set of PSR-4 directories for a given namespace, |
| 295 | 221 | * replacing any others previously set for this namespace. |
| 296 | 222 | * |
| 297 | - * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 298 | - * @param string[]|string $paths The PSR-4 base directories | |
| 223 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 224 | + * @param array|string $paths The PSR-4 base directories | |
| 299 | 225 | * |
| 300 | 226 | * @throws \InvalidArgumentException |
| 301 | - * | |
| 302 | - * @return void | |
| 303 | 227 | */ |
| 304 | 228 | public function setPsr4($prefix, $paths) |
| 305 | 229 | { |
| 306 | 230 | if (!$prefix) { |
| @@ -318,10 +242,8 @@ | ||
| 318 | 242 | /** |
| 319 | 243 | * Turns on searching the include path for class files. |
| 320 | 244 | * |
| 321 | 245 | * @param bool $useIncludePath |
| 322 | - * | |
| 323 | - * @return void | |
| 324 | 246 | */ |
| 325 | 247 | public function setUseIncludePath($useIncludePath) |
| 326 | 248 | { |
| 327 | 249 | $this->useIncludePath = $useIncludePath; |
| @@ -342,10 +264,8 @@ | ||
| 342 | 264 | * Turns off searching the prefix and fallback directories for classes |
| 343 | 265 | * that have not been registered with the class map. |
| 344 | 266 | * |
| 345 | 267 | * @param bool $classMapAuthoritative |
| 346 | - * | |
| 347 | - * @return void | |
| 348 | 268 | */ |
| 349 | 269 | public function setClassMapAuthoritative($classMapAuthoritative) |
| 350 | 270 | { |
| 351 | 271 | $this->classMapAuthoritative = $classMapAuthoritative; |
| @@ -364,10 +284,8 @@ | ||
| 364 | 284 | /** |
| 365 | 285 | * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
| 366 | 286 | * |
| 367 | 287 | * @param string|null $apcuPrefix |
| 368 | - * | |
| 369 | - * @return void | |
| 370 | 288 | */ |
| 371 | 289 | public function setApcuPrefix($apcuPrefix) |
| 372 | 290 | { |
| 373 | 291 | $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
| @@ -386,10 +304,8 @@ | ||
| 386 | 304 | /** |
| 387 | 305 | * Registers this instance as an autoloader. |
| 388 | 306 | * |
| 389 | 307 | * @param bool $prepend Whether to prepend the autoloader or not |
| 390 | - * | |
| 391 | - * @return void | |
| 392 | 308 | */ |
| 393 | 309 | public function register($prepend = false) |
| 394 | 310 | { |
| 395 | 311 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| @@ -407,10 +323,8 @@ | ||
| 407 | 323 | } |
| 408 | 324 | |
| 409 | 325 | /** |
| 410 | 326 | * Unregisters this instance as an autoloader. |
| 411 | - * | |
| 412 | - * @return void | |
| 413 | 327 | */ |
| 414 | 328 | public function unregister() |
| 415 | 329 | { |
| 416 | 330 | spl_autoload_unregister(array($this, 'loadClass')); |
| @@ -423,20 +337,17 @@ | ||
| 423 | 337 | /** |
| 424 | 338 | * Loads the given class or interface. |
| 425 | 339 | * |
| 426 | 340 | * @param string $class The name of the class |
| 427 | - * @return true|null True if loaded, null otherwise | |
| 341 | + * @return bool|null True if loaded, null otherwise | |
| 428 | 342 | */ |
| 429 | 343 | public function loadClass($class) |
| 430 | 344 | { |
| 431 | 345 | if ($file = $this->findFile($class)) { |
| 432 | - $includeFile = self::$includeFile; | |
| 433 | - $includeFile($file); | |
| 346 | + includeFile($file); | |
| 434 | 347 | |
| 435 | 348 | return true; |
| 436 | 349 | } |
| 437 | - | |
| 438 | - return null; | |
| 439 | 350 | } |
| 440 | 351 | |
| 441 | 352 | /** |
| 442 | 353 | * Finds the path to the file where the class is defined. |
| @@ -489,13 +400,8 @@ | ||
| 489 | 400 | { |
| 490 | 401 | return self::$registeredLoaders; |
| 491 | 402 | } |
| 492 | 403 | |
| 493 | - /** | |
| 494 | - * @param string $class | |
| 495 | - * @param string $ext | |
| 496 | - * @return string|false | |
| 497 | - */ | |
| 498 | 404 | private function findFileWithExtension($class, $ext) |
| 499 | 405 | { |
| 500 | 406 | // PSR-4 lookup |
| 501 | 407 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| @@ -559,27 +465,15 @@ | ||
| 559 | 465 | } |
| 560 | 466 | |
| 561 | 467 | return false; |
| 562 | 468 | } |
| 469 | +} | |
| 563 | 470 | |
| 564 | - /** | |
| 565 | - * @return void | |
| 566 | - */ | |
| 567 | - private static function initializeIncludeClosure() | |
| 568 | - { | |
| 569 | - if (self::$includeFile !== null) { | |
| 570 | - return; | |
| 571 | - } | |
| 572 | - | |
| 573 | - /** | |
| 574 | - * Scope isolated include. | |
| 575 | - * | |
| 576 | - * Prevents access to $this/self from included files. | |
| 577 | - * | |
| 578 | - * @param string $file | |
| 579 | - * @return void | |
| 580 | - */ | |
| 581 | - self::$includeFile = \Closure::bind(static function($file) { | |
| 582 | - include $file; | |
| 583 | - }, null, null); | |
| 584 | - } | |
| 471 | +/** | |
| 472 | + * Scope isolated include. | |
| 473 | + * | |
| 474 | + * Prevents access to $this/self from included files. | |
| 475 | + */ | |
| 476 | +function includeFile($file) | |
| 477 | +{ | |
| 478 | + include $file; | |
| 585 | 479 | } |