| @@ -36,126 +36,51 @@ | ||
| 36 | 36 | * This class is loosely based on the Symfony UniversalClassLoader. |
| 37 | 37 | * |
| 38 | 38 | * @author Fabien Potencier <fabien@symfony.com> |
| 39 | 39 | * @author Jordi Boggiano <j.boggiano@seld.be> |
| 40 | - * @see https://www.php-fig.org/psr/psr-0/ | |
| 41 | - * @see https://www.php-fig.org/psr/psr-4/ | |
| 40 | + * @see http://www.php-fig.org/psr/psr-0/ | |
| 41 | + * @see http://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 | - private $vendorDir; | |
| 50 | - | |
| 51 | 45 | // PSR-4 |
| 52 | - /** | |
| 53 | - * @var array[] | |
| 54 | - * @psalm-var array<string, array<string, int>> | |
| 55 | - */ | |
| 56 | 46 | private $prefixLengthsPsr4 = array(); |
| 57 | - /** | |
| 58 | - * @var array[] | |
| 59 | - * @psalm-var array<string, array<int, string>> | |
| 60 | - */ | |
| 61 | 47 | private $prefixDirsPsr4 = array(); |
| 62 | - /** | |
| 63 | - * @var array[] | |
| 64 | - * @psalm-var array<string, string> | |
| 65 | - */ | |
| 66 | 48 | private $fallbackDirsPsr4 = array(); |
| 67 | 49 | |
| 68 | 50 | // PSR-0 |
| 69 | - /** | |
| 70 | - * @var array[] | |
| 71 | - * @psalm-var array<string, array<string, string[]>> | |
| 72 | - */ | |
| 73 | 51 | private $prefixesPsr0 = array(); |
| 74 | - /** | |
| 75 | - * @var array[] | |
| 76 | - * @psalm-var array<string, string> | |
| 77 | - */ | |
| 78 | 52 | private $fallbackDirsPsr0 = array(); |
| 79 | 53 | |
| 80 | - /** @var bool */ | |
| 81 | 54 | private $useIncludePath = false; |
| 82 | - | |
| 83 | - /** | |
| 84 | - * @var string[] | |
| 85 | - * @psalm-var array<string, string> | |
| 86 | - */ | |
| 87 | 55 | private $classMap = array(); |
| 88 | 56 | |
| 89 | - /** @var bool */ | |
| 90 | 57 | private $classMapAuthoritative = false; |
| 91 | 58 | |
| 92 | - /** | |
| 93 | - * @var bool[] | |
| 94 | - * @psalm-var array<string, bool> | |
| 95 | - */ | |
| 96 | - private $missingClasses = array(); | |
| 97 | - | |
| 98 | - /** @var ?string */ | |
| 99 | - private $apcuPrefix; | |
| 100 | - | |
| 101 | - /** | |
| 102 | - * @var self[] | |
| 103 | - */ | |
| 104 | - private static $registeredLoaders = array(); | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * @param ?string $vendorDir | |
| 108 | - */ | |
| 109 | - public function __construct($vendorDir = null) | |
| 110 | - { | |
| 111 | - $this->vendorDir = $vendorDir; | |
| 112 | - self::initializeIncludeClosure(); | |
| 113 | - } | |
| 114 | - | |
| 115 | - /** | |
| 116 | - * @return string[] | |
| 117 | - */ | |
| 118 | 59 | public function getPrefixes() |
| 119 | 60 | { |
| 120 | 61 | if (!empty($this->prefixesPsr0)) { |
| 121 | - return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); | |
| 62 | + return call_user_func_array('array_merge', $this->prefixesPsr0); | |
| 122 | 63 | } |
| 123 | 64 | |
| 124 | 65 | return array(); |
| 125 | 66 | } |
| 126 | 67 | |
| 127 | - /** | |
| 128 | - * @return array[] | |
| 129 | - * @psalm-return array<string, array<int, string>> | |
| 130 | - */ | |
| 131 | 68 | public function getPrefixesPsr4() |
| 132 | 69 | { |
| 133 | 70 | return $this->prefixDirsPsr4; |
| 134 | 71 | } |
| 135 | 72 | |
| 136 | - /** | |
| 137 | - * @return array[] | |
| 138 | - * @psalm-return array<string, string> | |
| 139 | - */ | |
| 140 | 73 | public function getFallbackDirs() |
| 141 | 74 | { |
| 142 | 75 | return $this->fallbackDirsPsr0; |
| 143 | 76 | } |
| 144 | 77 | |
| 145 | - /** | |
| 146 | - * @return array[] | |
| 147 | - * @psalm-return array<string, string> | |
| 148 | - */ | |
| 149 | 78 | public function getFallbackDirsPsr4() |
| 150 | 79 | { |
| 151 | 80 | return $this->fallbackDirsPsr4; |
| 152 | 81 | } |
| 153 | 82 | |
| 154 | - /** | |
| 155 | - * @return string[] Array of classname => path | |
| 156 | - * @psalm-return array<string, string> | |
| 157 | - */ | |
| 158 | 83 | public function getClassMap() |
| 159 | 84 | { |
| 160 | 85 | return $this->classMap; |
| 161 | 86 | } |
| @@ -160,12 +85,9 @@ | ||
| 160 | 85 | return $this->classMap; |
| 161 | 86 | } |
| 162 | 87 | |
| 163 | 88 | /** |
| 164 | - * @param string[] $classMap Class to filename map | |
| 165 | - * @psalm-param array<string, string> $classMap | |
| 166 | - * | |
| 167 | - * @return void | |
| 89 | + * @param array $classMap Class to filename map | |
| 168 | 90 | */ |
| 169 | 91 | public function addClassMap(array $classMap) |
| 170 | 92 | { |
| 171 | 93 | if ($this->classMap) { |
| @@ -178,13 +100,11 @@ | ||
| 178 | 100 | /** |
| 179 | 101 | * Registers a set of PSR-0 directories for a given prefix, either |
| 180 | 102 | * appending or prepending to the ones previously set for this prefix. |
| 181 | 103 | * |
| 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 | |
| 104 | + * @param string $prefix The prefix | |
| 105 | + * @param array|string $paths The PSR-0 root directories | |
| 106 | + * @param bool $prepend Whether to prepend the directories | |
| 187 | 107 | */ |
| 188 | 108 | public function add($prefix, $paths, $prepend = false) |
| 189 | 109 | { |
| 190 | 110 | if (!$prefix) { |
| @@ -225,15 +145,13 @@ | ||
| 225 | 145 | /** |
| 226 | 146 | * Registers a set of PSR-4 directories for a given namespace, either |
| 227 | 147 | * appending or prepending to the ones previously set for this namespace. |
| 228 | 148 | * |
| 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 | |
| 149 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 150 | + * @param array|string $paths The PSR-4 base directories | |
| 151 | + * @param bool $prepend Whether to prepend the directories | |
| 232 | 152 | * |
| 233 | 153 | * @throws \InvalidArgumentException |
| 234 | - * | |
| 235 | - * @return void | |
| 236 | 154 | */ |
| 237 | 155 | public function addPsr4($prefix, $paths, $prepend = false) |
| 238 | 156 | { |
| 239 | 157 | if (!$prefix) { |
| @@ -275,12 +193,10 @@ | ||
| 275 | 193 | /** |
| 276 | 194 | * Registers a set of PSR-0 directories for a given prefix, |
| 277 | 195 | * replacing any others previously set for this prefix. |
| 278 | 196 | * |
| 279 | - * @param string $prefix The prefix | |
| 280 | - * @param string[]|string $paths The PSR-0 base directories | |
| 281 | - * | |
| 282 | - * @return void | |
| 197 | + * @param string $prefix The prefix | |
| 198 | + * @param array|string $paths The PSR-0 base directories | |
| 283 | 199 | */ |
| 284 | 200 | public function set($prefix, $paths) |
| 285 | 201 | { |
| 286 | 202 | if (!$prefix) { |
| @@ -293,14 +209,12 @@ | ||
| 293 | 209 | /** |
| 294 | 210 | * Registers a set of PSR-4 directories for a given namespace, |
| 295 | 211 | * replacing any others previously set for this namespace. |
| 296 | 212 | * |
| 297 | - * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 298 | - * @param string[]|string $paths The PSR-4 base directories | |
| 213 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 214 | + * @param array|string $paths The PSR-4 base directories | |
| 299 | 215 | * |
| 300 | 216 | * @throws \InvalidArgumentException |
| 301 | - * | |
| 302 | - * @return void | |
| 303 | 217 | */ |
| 304 | 218 | public function setPsr4($prefix, $paths) |
| 305 | 219 | { |
| 306 | 220 | if (!$prefix) { |
| @@ -318,10 +232,8 @@ | ||
| 318 | 232 | /** |
| 319 | 233 | * Turns on searching the include path for class files. |
| 320 | 234 | * |
| 321 | 235 | * @param bool $useIncludePath |
| 322 | - * | |
| 323 | - * @return void | |
| 324 | 236 | */ |
| 325 | 237 | public function setUseIncludePath($useIncludePath) |
| 326 | 238 | { |
| 327 | 239 | $this->useIncludePath = $useIncludePath; |
| @@ -342,10 +254,8 @@ | ||
| 342 | 254 | * Turns off searching the prefix and fallback directories for classes |
| 343 | 255 | * that have not been registered with the class map. |
| 344 | 256 | * |
| 345 | 257 | * @param bool $classMapAuthoritative |
| 346 | - * | |
| 347 | - * @return void | |
| 348 | 258 | */ |
| 349 | 259 | public function setClassMapAuthoritative($classMapAuthoritative) |
| 350 | 260 | { |
| 351 | 261 | $this->classMapAuthoritative = $classMapAuthoritative; |
| @@ -361,64 +271,23 @@ | ||
| 361 | 271 | return $this->classMapAuthoritative; |
| 362 | 272 | } |
| 363 | 273 | |
| 364 | 274 | /** |
| 365 | - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. | |
| 366 | - * | |
| 367 | - * @param string|null $apcuPrefix | |
| 368 | - * | |
| 369 | - * @return void | |
| 370 | - */ | |
| 371 | - public function setApcuPrefix($apcuPrefix) | |
| 372 | - { | |
| 373 | - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; | |
| 374 | - } | |
| 375 | - | |
| 376 | - /** | |
| 377 | - * The APCu prefix in use, or null if APCu caching is not enabled. | |
| 378 | - * | |
| 379 | - * @return string|null | |
| 380 | - */ | |
| 381 | - public function getApcuPrefix() | |
| 382 | - { | |
| 383 | - return $this->apcuPrefix; | |
| 384 | - } | |
| 385 | - | |
| 386 | - /** | |
| 387 | 275 | * Registers this instance as an autoloader. |
| 388 | 276 | * |
| 389 | 277 | * @param bool $prepend Whether to prepend the autoloader or not |
| 390 | - * | |
| 391 | - * @return void | |
| 392 | 278 | */ |
| 393 | 279 | public function register($prepend = false) |
| 394 | 280 | { |
| 395 | 281 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 396 | - | |
| 397 | - if (null === $this->vendorDir) { | |
| 398 | - return; | |
| 399 | - } | |
| 400 | - | |
| 401 | - if ($prepend) { | |
| 402 | - self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; | |
| 403 | - } else { | |
| 404 | - unset(self::$registeredLoaders[$this->vendorDir]); | |
| 405 | - self::$registeredLoaders[$this->vendorDir] = $this; | |
| 406 | - } | |
| 407 | 282 | } |
| 408 | 283 | |
| 409 | 284 | /** |
| 410 | 285 | * Unregisters this instance as an autoloader. |
| 411 | - * | |
| 412 | - * @return void | |
| 413 | 286 | */ |
| 414 | 287 | public function unregister() |
| 415 | 288 | { |
| 416 | 289 | spl_autoload_unregister(array($this, 'loadClass')); |
| 417 | - | |
| 418 | - if (null !== $this->vendorDir) { | |
| 419 | - unset(self::$registeredLoaders[$this->vendorDir]); | |
| 420 | - } | |
| 421 | 290 | } |
| 422 | 291 | |
| 423 | 292 | /** |
| 424 | 293 | * Loads the given class or interface. |
| @@ -423,20 +292,17 @@ | ||
| 423 | 292 | /** |
| 424 | 293 | * Loads the given class or interface. |
| 425 | 294 | * |
| 426 | 295 | * @param string $class The name of the class |
| 427 | - * @return true|null True if loaded, null otherwise | |
| 296 | + * @return bool|null True if loaded, null otherwise | |
| 428 | 297 | */ |
| 429 | 298 | public function loadClass($class) |
| 430 | 299 | { |
| 431 | 300 | if ($file = $this->findFile($class)) { |
| 432 | - $includeFile = self::$includeFile; | |
| 433 | - $includeFile($file); | |
| 301 | + includeFile($file); | |
| 434 | 302 | |
| 435 | 303 | return true; |
| 436 | 304 | } |
| 437 | - | |
| 438 | - return null; | |
| 439 | 305 | } |
| 440 | 306 | |
| 441 | 307 | /** |
| 442 | 308 | * Finds the path to the file where the class is defined. |
| @@ -446,56 +312,36 @@ | ||
| 446 | 312 | * @return string|false The path if found, false otherwise |
| 447 | 313 | */ |
| 448 | 314 | public function findFile($class) |
| 449 | 315 | { |
| 316 | + // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 | |
| 317 | + if ('\\' == $class[0]) { | |
| 318 | + $class = substr($class, 1); | |
| 319 | + } | |
| 320 | + | |
| 450 | 321 | // class map lookup |
| 451 | 322 | if (isset($this->classMap[$class])) { |
| 452 | 323 | return $this->classMap[$class]; |
| 453 | 324 | } |
| 454 | - if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { | |
| 325 | + if ($this->classMapAuthoritative) { | |
| 455 | 326 | return false; |
| 456 | 327 | } |
| 457 | - if (null !== $this->apcuPrefix) { | |
| 458 | - $file = apcu_fetch($this->apcuPrefix.$class, $hit); | |
| 459 | - if ($hit) { | |
| 460 | - return $file; | |
| 461 | - } | |
| 462 | - } | |
| 463 | 328 | |
| 464 | 329 | $file = $this->findFileWithExtension($class, '.php'); |
| 465 | 330 | |
| 466 | 331 | // Search for Hack files if we are running on HHVM |
| 467 | - if (false === $file && defined('HHVM_VERSION')) { | |
| 332 | + if ($file === null && defined('HHVM_VERSION')) { | |
| 468 | 333 | $file = $this->findFileWithExtension($class, '.hh'); |
| 469 | 334 | } |
| 470 | 335 | |
| 471 | - if (null !== $this->apcuPrefix) { | |
| 472 | - apcu_add($this->apcuPrefix.$class, $file); | |
| 473 | - } | |
| 474 | - | |
| 475 | - if (false === $file) { | |
| 336 | + if ($file === null) { | |
| 476 | 337 | // Remember that this class does not exist. |
| 477 | - $this->missingClasses[$class] = true; | |
| 338 | + return $this->classMap[$class] = false; | |
| 478 | 339 | } |
| 479 | 340 | |
| 480 | 341 | return $file; |
| 481 | 342 | } |
| 482 | 343 | |
| 483 | - /** | |
| 484 | - * Returns the currently registered loaders indexed by their corresponding vendor directories. | |
| 485 | - * | |
| 486 | - * @return self[] | |
| 487 | - */ | |
| 488 | - public static function getRegisteredLoaders() | |
| 489 | - { | |
| 490 | - return self::$registeredLoaders; | |
| 491 | - } | |
| 492 | - | |
| 493 | - /** | |
| 494 | - * @param string $class | |
| 495 | - * @param string $ext | |
| 496 | - * @return string|false | |
| 497 | - */ | |
| 498 | 344 | private function findFileWithExtension($class, $ext) |
| 499 | 345 | { |
| 500 | 346 | // PSR-4 lookup |
| 501 | 347 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| @@ -501,16 +347,12 @@ | ||
| 501 | 347 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| 502 | 348 | |
| 503 | 349 | $first = $class[0]; |
| 504 | 350 | if (isset($this->prefixLengthsPsr4[$first])) { |
| 505 | - $subPath = $class; | |
| 506 | - while (false !== $lastPos = strrpos($subPath, '\\')) { | |
| 507 | - $subPath = substr($subPath, 0, $lastPos); | |
| 508 | - $search = $subPath . '\\'; | |
| 509 | - if (isset($this->prefixDirsPsr4[$search])) { | |
| 510 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); | |
| 511 | - foreach ($this->prefixDirsPsr4[$search] as $dir) { | |
| 512 | - if (file_exists($file = $dir . $pathEnd)) { | |
| 351 | + foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { | |
| 352 | + if (0 === strpos($class, $prefix)) { | |
| 353 | + foreach ($this->prefixDirsPsr4[$prefix] as $dir) { | |
| 354 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { | |
| 513 | 355 | return $file; |
| 514 | 356 | } |
| 515 | 357 | } |
| 516 | 358 | } |
| @@ -556,30 +398,16 @@ | ||
| 556 | 398 | // PSR-0 include paths. |
| 557 | 399 | if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
| 558 | 400 | return $file; |
| 559 | 401 | } |
| 560 | - | |
| 561 | - return false; | |
| 562 | 402 | } |
| 403 | +} | |
| 563 | 404 | |
| 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 | - } | |
| 405 | +/** | |
| 406 | + * Scope isolated include. | |
| 407 | + * | |
| 408 | + * Prevents access to $this/self from included files. | |
| 409 | + */ | |
| 410 | +function includeFile($file) | |
| 411 | +{ | |
| 412 | + include $file; | |
| 585 | 413 | } |