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