| @@ -41,78 +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|null */ | |
| 49 | 45 | private $vendorDir; |
| 50 | 46 | |
| 51 | 47 | // PSR-4 |
| 52 | - /** | |
| 53 | - * @var array<string, array<string, int>> | |
| 54 | - */ | |
| 55 | 48 | private $prefixLengthsPsr4 = array(); |
| 56 | - /** | |
| 57 | - * @var array<string, list<string>> | |
| 58 | - */ | |
| 59 | 49 | private $prefixDirsPsr4 = array(); |
| 60 | - /** | |
| 61 | - * @var list<string> | |
| 62 | - */ | |
| 63 | 50 | private $fallbackDirsPsr4 = array(); |
| 64 | 51 | |
| 65 | 52 | // PSR-0 |
| 66 | - /** | |
| 67 | - * List of PSR-0 prefixes | |
| 68 | - * | |
| 69 | - * Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2'))) | |
| 70 | - * | |
| 71 | - * @var array<string, array<string, list<string>>> | |
| 72 | - */ | |
| 73 | 53 | private $prefixesPsr0 = array(); |
| 74 | - /** | |
| 75 | - * @var list<string> | |
| 76 | - */ | |
| 77 | 54 | private $fallbackDirsPsr0 = array(); |
| 78 | 55 | |
| 79 | - /** @var bool */ | |
| 80 | 56 | private $useIncludePath = false; |
| 81 | - | |
| 82 | - /** | |
| 83 | - * @var array<string, string> | |
| 84 | - */ | |
| 85 | 57 | private $classMap = array(); |
| 86 | - | |
| 87 | - /** @var bool */ | |
| 88 | 58 | private $classMapAuthoritative = false; |
| 89 | - | |
| 90 | - /** | |
| 91 | - * @var array<string, bool> | |
| 92 | - */ | |
| 93 | 59 | private $missingClasses = array(); |
| 94 | - | |
| 95 | - /** @var string|null */ | |
| 96 | 60 | private $apcuPrefix; |
| 97 | 61 | |
| 98 | - /** | |
| 99 | - * @var array<string, self> | |
| 100 | - */ | |
| 101 | 62 | private static $registeredLoaders = array(); |
| 102 | 63 | |
| 103 | - /** | |
| 104 | - * @param string|null $vendorDir | |
| 105 | - */ | |
| 106 | 64 | public function __construct($vendorDir = null) |
| 107 | 65 | { |
| 108 | 66 | $this->vendorDir = $vendorDir; |
| 109 | - self::initializeIncludeClosure(); | |
| 110 | 67 | } |
| 111 | 68 | |
| 112 | - /** | |
| 113 | - * @return array<string, list<string>> | |
| 114 | - */ | |
| 115 | 69 | public function getPrefixes() |
| 116 | 70 | { |
| 117 | 71 | if (!empty($this->prefixesPsr0)) { |
| 118 | 72 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
| @@ -120,35 +74,23 @@ | ||
| 120 | 74 | |
| 121 | 75 | return array(); |
| 122 | 76 | } |
| 123 | 77 | |
| 124 | - /** | |
| 125 | - * @return array<string, list<string>> | |
| 126 | - */ | |
| 127 | 78 | public function getPrefixesPsr4() |
| 128 | 79 | { |
| 129 | 80 | return $this->prefixDirsPsr4; |
| 130 | 81 | } |
| 131 | 82 | |
| 132 | - /** | |
| 133 | - * @return list<string> | |
| 134 | - */ | |
| 135 | 83 | public function getFallbackDirs() |
| 136 | 84 | { |
| 137 | 85 | return $this->fallbackDirsPsr0; |
| 138 | 86 | } |
| 139 | 87 | |
| 140 | - /** | |
| 141 | - * @return list<string> | |
| 142 | - */ | |
| 143 | 88 | public function getFallbackDirsPsr4() |
| 144 | 89 | { |
| 145 | 90 | return $this->fallbackDirsPsr4; |
| 146 | 91 | } |
| 147 | 92 | |
| 148 | - /** | |
| 149 | - * @return array<string, string> Array of classname => path | |
| 150 | - */ | |
| 151 | 93 | public function getClassMap() |
| 152 | 94 | { |
| 153 | 95 | return $this->classMap; |
| 154 | 96 | } |
| @@ -153,11 +95,9 @@ | ||
| 153 | 95 | return $this->classMap; |
| 154 | 96 | } |
| 155 | 97 | |
| 156 | 98 | /** |
| 157 | - * @param array<string, string> $classMap Class to filename map | |
| 158 | - * | |
| 159 | - * @return void | |
| 99 | + * @param array $classMap Class to filename map | |
| 160 | 100 | */ |
| 161 | 101 | public function addClassMap(array $classMap) |
| 162 | 102 | { |
| 163 | 103 | if ($this->classMap) { |
| @@ -170,27 +110,24 @@ | ||
| 170 | 110 | /** |
| 171 | 111 | * Registers a set of PSR-0 directories for a given prefix, either |
| 172 | 112 | * appending or prepending to the ones previously set for this prefix. |
| 173 | 113 | * |
| 174 | - * @param string $prefix The prefix | |
| 175 | - * @param list<string>|string $paths The PSR-0 root directories | |
| 176 | - * @param bool $prepend Whether to prepend the directories | |
| 177 | - * | |
| 178 | - * @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 | |
| 179 | 117 | */ |
| 180 | 118 | public function add($prefix, $paths, $prepend = false) |
| 181 | 119 | { |
| 182 | - $paths = (array) $paths; | |
| 183 | 120 | if (!$prefix) { |
| 184 | 121 | if ($prepend) { |
| 185 | 122 | $this->fallbackDirsPsr0 = array_merge( |
| 186 | - $paths, | |
| 123 | + (array) $paths, | |
| 187 | 124 | $this->fallbackDirsPsr0 |
| 188 | 125 | ); |
| 189 | 126 | } else { |
| 190 | 127 | $this->fallbackDirsPsr0 = array_merge( |
| 191 | 128 | $this->fallbackDirsPsr0, |
| 192 | - $paths | |
| 129 | + (array) $paths | |
| 193 | 130 | ); |
| 194 | 131 | } |
| 195 | 132 | |
| 196 | 133 | return; |
| @@ -197,21 +134,21 @@ | ||
| 197 | 134 | } |
| 198 | 135 | |
| 199 | 136 | $first = $prefix[0]; |
| 200 | 137 | if (!isset($this->prefixesPsr0[$first][$prefix])) { |
| 201 | - $this->prefixesPsr0[$first][$prefix] = $paths; | |
| 138 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; | |
| 202 | 139 | |
| 203 | 140 | return; |
| 204 | 141 | } |
| 205 | 142 | if ($prepend) { |
| 206 | 143 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 207 | - $paths, | |
| 144 | + (array) $paths, | |
| 208 | 145 | $this->prefixesPsr0[$first][$prefix] |
| 209 | 146 | ); |
| 210 | 147 | } else { |
| 211 | 148 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 212 | 149 | $this->prefixesPsr0[$first][$prefix], |
| 213 | - $paths | |
| 150 | + (array) $paths | |
| 214 | 151 | ); |
| 215 | 152 | } |
| 216 | 153 | } |
| 217 | 154 | |
| @@ -218,30 +155,27 @@ | ||
| 218 | 155 | /** |
| 219 | 156 | * Registers a set of PSR-4 directories for a given namespace, either |
| 220 | 157 | * appending or prepending to the ones previously set for this namespace. |
| 221 | 158 | * |
| 222 | - * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 223 | - * @param list<string>|string $paths The PSR-4 base directories | |
| 224 | - * @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 | |
| 225 | 162 | * |
| 226 | 163 | * @throws \InvalidArgumentException |
| 227 | - * | |
| 228 | - * @return void | |
| 229 | 164 | */ |
| 230 | 165 | public function addPsr4($prefix, $paths, $prepend = false) |
| 231 | 166 | { |
| 232 | - $paths = (array) $paths; | |
| 233 | 167 | if (!$prefix) { |
| 234 | 168 | // Register directories for the root namespace. |
| 235 | 169 | if ($prepend) { |
| 236 | 170 | $this->fallbackDirsPsr4 = array_merge( |
| 237 | - $paths, | |
| 171 | + (array) $paths, | |
| 238 | 172 | $this->fallbackDirsPsr4 |
| 239 | 173 | ); |
| 240 | 174 | } else { |
| 241 | 175 | $this->fallbackDirsPsr4 = array_merge( |
| 242 | 176 | $this->fallbackDirsPsr4, |
| 243 | - $paths | |
| 177 | + (array) $paths | |
| 244 | 178 | ); |
| 245 | 179 | } |
| 246 | 180 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
| 247 | 181 | // Register directories for a new namespace. |
| @@ -249,13 +183,13 @@ | ||
| 249 | 183 | if ('\\' !== $prefix[$length - 1]) { |
| 250 | 184 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 251 | 185 | } |
| 252 | 186 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 253 | - $this->prefixDirsPsr4[$prefix] = $paths; | |
| 187 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; | |
| 254 | 188 | } elseif ($prepend) { |
| 255 | 189 | // Prepend directories for an already registered namespace. |
| 256 | 190 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 257 | - $paths, | |
| 191 | + (array) $paths, | |
| 258 | 192 | $this->prefixDirsPsr4[$prefix] |
| 259 | 193 | ); |
| 260 | 194 | } else { |
| 261 | 195 | // Append directories for an already registered namespace. |
| @@ -260,9 +194,9 @@ | ||
| 260 | 194 | } else { |
| 261 | 195 | // Append directories for an already registered namespace. |
| 262 | 196 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 263 | 197 | $this->prefixDirsPsr4[$prefix], |
| 264 | - $paths | |
| 198 | + (array) $paths | |
| 265 | 199 | ); |
| 266 | 200 | } |
| 267 | 201 | } |
| 268 | 202 | |
| @@ -269,12 +203,10 @@ | ||
| 269 | 203 | /** |
| 270 | 204 | * Registers a set of PSR-0 directories for a given prefix, |
| 271 | 205 | * replacing any others previously set for this prefix. |
| 272 | 206 | * |
| 273 | - * @param string $prefix The prefix | |
| 274 | - * @param list<string>|string $paths The PSR-0 base directories | |
| 275 | - * | |
| 276 | - * @return void | |
| 207 | + * @param string $prefix The prefix | |
| 208 | + * @param array|string $paths The PSR-0 base directories | |
| 277 | 209 | */ |
| 278 | 210 | public function set($prefix, $paths) |
| 279 | 211 | { |
| 280 | 212 | if (!$prefix) { |
| @@ -287,14 +219,12 @@ | ||
| 287 | 219 | /** |
| 288 | 220 | * Registers a set of PSR-4 directories for a given namespace, |
| 289 | 221 | * replacing any others previously set for this namespace. |
| 290 | 222 | * |
| 291 | - * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 292 | - * @param list<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 | |
| 293 | 225 | * |
| 294 | 226 | * @throws \InvalidArgumentException |
| 295 | - * | |
| 296 | - * @return void | |
| 297 | 227 | */ |
| 298 | 228 | public function setPsr4($prefix, $paths) |
| 299 | 229 | { |
| 300 | 230 | if (!$prefix) { |
| @@ -312,10 +242,8 @@ | ||
| 312 | 242 | /** |
| 313 | 243 | * Turns on searching the include path for class files. |
| 314 | 244 | * |
| 315 | 245 | * @param bool $useIncludePath |
| 316 | - * | |
| 317 | - * @return void | |
| 318 | 246 | */ |
| 319 | 247 | public function setUseIncludePath($useIncludePath) |
| 320 | 248 | { |
| 321 | 249 | $this->useIncludePath = $useIncludePath; |
| @@ -336,10 +264,8 @@ | ||
| 336 | 264 | * Turns off searching the prefix and fallback directories for classes |
| 337 | 265 | * that have not been registered with the class map. |
| 338 | 266 | * |
| 339 | 267 | * @param bool $classMapAuthoritative |
| 340 | - * | |
| 341 | - * @return void | |
| 342 | 268 | */ |
| 343 | 269 | public function setClassMapAuthoritative($classMapAuthoritative) |
| 344 | 270 | { |
| 345 | 271 | $this->classMapAuthoritative = $classMapAuthoritative; |
| @@ -358,10 +284,8 @@ | ||
| 358 | 284 | /** |
| 359 | 285 | * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
| 360 | 286 | * |
| 361 | 287 | * @param string|null $apcuPrefix |
| 362 | - * | |
| 363 | - * @return void | |
| 364 | 288 | */ |
| 365 | 289 | public function setApcuPrefix($apcuPrefix) |
| 366 | 290 | { |
| 367 | 291 | $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
| @@ -380,10 +304,8 @@ | ||
| 380 | 304 | /** |
| 381 | 305 | * Registers this instance as an autoloader. |
| 382 | 306 | * |
| 383 | 307 | * @param bool $prepend Whether to prepend the autoloader or not |
| 384 | - * | |
| 385 | - * @return void | |
| 386 | 308 | */ |
| 387 | 309 | public function register($prepend = false) |
| 388 | 310 | { |
| 389 | 311 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| @@ -401,10 +323,8 @@ | ||
| 401 | 323 | } |
| 402 | 324 | |
| 403 | 325 | /** |
| 404 | 326 | * Unregisters this instance as an autoloader. |
| 405 | - * | |
| 406 | - * @return void | |
| 407 | 327 | */ |
| 408 | 328 | public function unregister() |
| 409 | 329 | { |
| 410 | 330 | spl_autoload_unregister(array($this, 'loadClass')); |
| @@ -417,20 +337,17 @@ | ||
| 417 | 337 | /** |
| 418 | 338 | * Loads the given class or interface. |
| 419 | 339 | * |
| 420 | 340 | * @param string $class The name of the class |
| 421 | - * @return true|null True if loaded, null otherwise | |
| 341 | + * @return bool|null True if loaded, null otherwise | |
| 422 | 342 | */ |
| 423 | 343 | public function loadClass($class) |
| 424 | 344 | { |
| 425 | 345 | if ($file = $this->findFile($class)) { |
| 426 | - $includeFile = self::$includeFile; | |
| 427 | - $includeFile($file); | |
| 346 | + includeFile($file); | |
| 428 | 347 | |
| 429 | 348 | return true; |
| 430 | 349 | } |
| 431 | - | |
| 432 | - return null; | |
| 433 | 350 | } |
| 434 | 351 | |
| 435 | 352 | /** |
| 436 | 353 | * Finds the path to the file where the class is defined. |
| @@ -474,11 +391,11 @@ | ||
| 474 | 391 | return $file; |
| 475 | 392 | } |
| 476 | 393 | |
| 477 | 394 | /** |
| 478 | - * Returns the currently registered loaders keyed by their corresponding vendor directories. | |
| 395 | + * Returns the currently registered loaders indexed by their corresponding vendor directories. | |
| 479 | 396 | * |
| 480 | - * @return array<string, self> | |
| 397 | + * @return self[] | |
| 481 | 398 | */ |
| 482 | 399 | public static function getRegisteredLoaders() |
| 483 | 400 | { |
| 484 | 401 | return self::$registeredLoaders; |
| @@ -483,13 +400,8 @@ | ||
| 483 | 400 | { |
| 484 | 401 | return self::$registeredLoaders; |
| 485 | 402 | } |
| 486 | 403 | |
| 487 | - /** | |
| 488 | - * @param string $class | |
| 489 | - * @param string $ext | |
| 490 | - * @return string|false | |
| 491 | - */ | |
| 492 | 404 | private function findFileWithExtension($class, $ext) |
| 493 | 405 | { |
| 494 | 406 | // PSR-4 lookup |
| 495 | 407 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| @@ -553,27 +465,15 @@ | ||
| 553 | 465 | } |
| 554 | 466 | |
| 555 | 467 | return false; |
| 556 | 468 | } |
| 469 | +} | |
| 557 | 470 | |
| 558 | - /** | |
| 559 | - * @return void | |
| 560 | - */ | |
| 561 | - private static function initializeIncludeClosure() | |
| 562 | - { | |
| 563 | - if (self::$includeFile !== null) { | |
| 564 | - return; | |
| 565 | - } | |
| 566 | - | |
| 567 | - /** | |
| 568 | - * Scope isolated include. | |
| 569 | - * | |
| 570 | - * Prevents access to $this/self from included files. | |
| 571 | - * | |
| 572 | - * @param string $file | |
| 573 | - * @return void | |
| 574 | - */ | |
| 575 | - self::$includeFile = \Closure::bind(static function($file) { | |
| 576 | - include $file; | |
| 577 | - }, null, null); | |
| 578 | - } | |
| 471 | +/** | |
| 472 | + * Scope isolated include. | |
| 473 | + * | |
| 474 | + * Prevents access to $this/self from included files. | |
| 475 | + */ | |
| 476 | +function includeFile($file) | |
| 477 | +{ | |
| 478 | + include $file; | |
| 579 | 479 | } |