| @@ -36,13 +36,15 @@ | ||
| 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 http://www.php-fig.org/psr/psr-0/ | |
| 41 | - * @see http://www.php-fig.org/psr/psr-4/ | |
| 40 | + * @see https://www.php-fig.org/psr/psr-0/ | |
| 41 | + * @see https://www.php-fig.org/psr/psr-4/ | |
| 42 | 42 | */ |
| 43 | 43 | class ClassLoader |
| 44 | 44 | { |
| 45 | + private $vendorDir; | |
| 46 | + | |
| 45 | 47 | // PSR-4 |
| 46 | 48 | private $prefixLengthsPsr4 = array(); |
| 47 | 49 | private $prefixDirsPsr4 = array(); |
| 48 | 50 | private $fallbackDirsPsr4 = array(); |
| @@ -52,15 +54,23 @@ | ||
| 52 | 54 | private $fallbackDirsPsr0 = array(); |
| 53 | 55 | |
| 54 | 56 | private $useIncludePath = false; |
| 55 | 57 | private $classMap = array(); |
| 58 | + private $classMapAuthoritative = false; | |
| 59 | + private $missingClasses = array(); | |
| 60 | + private $apcuPrefix; | |
| 56 | 61 | |
| 57 | - private $classMapAuthoritative = false; | |
| 62 | + private static $registeredLoaders = array(); | |
| 58 | 63 | |
| 64 | + public function __construct($vendorDir = null) | |
| 65 | + { | |
| 66 | + $this->vendorDir = $vendorDir; | |
| 67 | + } | |
| 68 | + | |
| 59 | 69 | public function getPrefixes() |
| 60 | 70 | { |
| 61 | 71 | if (!empty($this->prefixesPsr0)) { |
| 62 | - return call_user_func_array('array_merge', $this->prefixesPsr0); | |
| 72 | + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); | |
| 63 | 73 | } |
| 64 | 74 | |
| 65 | 75 | return array(); |
| 66 | 76 | } |
| @@ -271,8 +281,28 @@ | ||
| 271 | 281 | return $this->classMapAuthoritative; |
| 272 | 282 | } |
| 273 | 283 | |
| 274 | 284 | /** |
| 285 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. | |
| 286 | + * | |
| 287 | + * @param string|null $apcuPrefix | |
| 288 | + */ | |
| 289 | + public function setApcuPrefix($apcuPrefix) | |
| 290 | + { | |
| 291 | + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; | |
| 292 | + } | |
| 293 | + | |
| 294 | + /** | |
| 295 | + * The APCu prefix in use, or null if APCu caching is not enabled. | |
| 296 | + * | |
| 297 | + * @return string|null | |
| 298 | + */ | |
| 299 | + public function getApcuPrefix() | |
| 300 | + { | |
| 301 | + return $this->apcuPrefix; | |
| 302 | + } | |
| 303 | + | |
| 304 | + /** | |
| 275 | 305 | * Registers this instance as an autoloader. |
| 276 | 306 | * |
| 277 | 307 | * @param bool $prepend Whether to prepend the autoloader or not |
| 278 | 308 | */ |
| @@ -278,8 +308,19 @@ | ||
| 278 | 308 | */ |
| 279 | 309 | public function register($prepend = false) |
| 280 | 310 | { |
| 281 | 311 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 312 | + | |
| 313 | + if (null === $this->vendorDir) { | |
| 314 | + return; | |
| 315 | + } | |
| 316 | + | |
| 317 | + if ($prepend) { | |
| 318 | + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; | |
| 319 | + } else { | |
| 320 | + unset(self::$registeredLoaders[$this->vendorDir]); | |
| 321 | + self::$registeredLoaders[$this->vendorDir] = $this; | |
| 322 | + } | |
| 282 | 323 | } |
| 283 | 324 | |
| 284 | 325 | /** |
| 285 | 326 | * Unregisters this instance as an autoloader. |
| @@ -286,8 +327,12 @@ | ||
| 286 | 327 | */ |
| 287 | 328 | public function unregister() |
| 288 | 329 | { |
| 289 | 330 | spl_autoload_unregister(array($this, 'loadClass')); |
| 331 | + | |
| 332 | + if (null !== $this->vendorDir) { | |
| 333 | + unset(self::$registeredLoaders[$this->vendorDir]); | |
| 334 | + } | |
| 290 | 335 | } |
| 291 | 336 | |
| 292 | 337 | /** |
| 293 | 338 | * Loads the given class or interface. |
| @@ -292,9 +337,9 @@ | ||
| 292 | 337 | /** |
| 293 | 338 | * Loads the given class or interface. |
| 294 | 339 | * |
| 295 | 340 | * @param string $class The name of the class |
| 296 | - * @return bool|null True if loaded, null otherwise | |
| 341 | + * @return true|null True if loaded, null otherwise | |
| 297 | 342 | */ |
| 298 | 343 | public function loadClass($class) |
| 299 | 344 | { |
| 300 | 345 | if ($file = $this->findFile($class)) { |
| @@ -301,8 +346,10 @@ | ||
| 301 | 346 | includeFile($file); |
| 302 | 347 | |
| 303 | 348 | return true; |
| 304 | 349 | } |
| 350 | + | |
| 351 | + return null; | |
| 305 | 352 | } |
| 306 | 353 | |
| 307 | 354 | /** |
| 308 | 355 | * Finds the path to the file where the class is defined. |
| @@ -312,36 +359,51 @@ | ||
| 312 | 359 | * @return string|false The path if found, false otherwise |
| 313 | 360 | */ |
| 314 | 361 | public function findFile($class) |
| 315 | 362 | { |
| 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 | - | |
| 321 | 363 | // class map lookup |
| 322 | 364 | if (isset($this->classMap[$class])) { |
| 323 | 365 | return $this->classMap[$class]; |
| 324 | 366 | } |
| 325 | - if ($this->classMapAuthoritative) { | |
| 367 | + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { | |
| 326 | 368 | return false; |
| 327 | 369 | } |
| 370 | + if (null !== $this->apcuPrefix) { | |
| 371 | + $file = apcu_fetch($this->apcuPrefix.$class, $hit); | |
| 372 | + if ($hit) { | |
| 373 | + return $file; | |
| 374 | + } | |
| 375 | + } | |
| 328 | 376 | |
| 329 | 377 | $file = $this->findFileWithExtension($class, '.php'); |
| 330 | 378 | |
| 331 | 379 | // Search for Hack files if we are running on HHVM |
| 332 | - if ($file === null && defined('HHVM_VERSION')) { | |
| 380 | + if (false === $file && defined('HHVM_VERSION')) { | |
| 333 | 381 | $file = $this->findFileWithExtension($class, '.hh'); |
| 334 | 382 | } |
| 335 | 383 | |
| 336 | - if ($file === null) { | |
| 384 | + if (null !== $this->apcuPrefix) { | |
| 385 | + apcu_add($this->apcuPrefix.$class, $file); | |
| 386 | + } | |
| 387 | + | |
| 388 | + if (false === $file) { | |
| 337 | 389 | // Remember that this class does not exist. |
| 338 | - return $this->classMap[$class] = false; | |
| 390 | + $this->missingClasses[$class] = true; | |
| 339 | 391 | } |
| 340 | 392 | |
| 341 | 393 | return $file; |
| 342 | 394 | } |
| 343 | 395 | |
| 396 | + /** | |
| 397 | + * Returns the currently registered loaders indexed by their corresponding vendor directories. | |
| 398 | + * | |
| 399 | + * @return self[] | |
| 400 | + */ | |
| 401 | + public static function getRegisteredLoaders() | |
| 402 | + { | |
| 403 | + return self::$registeredLoaders; | |
| 404 | + } | |
| 405 | + | |
| 344 | 406 | private function findFileWithExtension($class, $ext) |
| 345 | 407 | { |
| 346 | 408 | // PSR-4 lookup |
| 347 | 409 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| @@ -347,12 +409,16 @@ | ||
| 347 | 409 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| 348 | 410 | |
| 349 | 411 | $first = $class[0]; |
| 350 | 412 | if (isset($this->prefixLengthsPsr4[$first])) { |
| 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))) { | |
| 413 | + $subPath = $class; | |
| 414 | + while (false !== $lastPos = strrpos($subPath, '\\')) { | |
| 415 | + $subPath = substr($subPath, 0, $lastPos); | |
| 416 | + $search = $subPath . '\\'; | |
| 417 | + if (isset($this->prefixDirsPsr4[$search])) { | |
| 418 | + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); | |
| 419 | + foreach ($this->prefixDirsPsr4[$search] as $dir) { | |
| 420 | + if (file_exists($file = $dir . $pathEnd)) { | |
| 355 | 421 | return $file; |
| 356 | 422 | } |
| 357 | 423 | } |
| 358 | 424 | } |
| @@ -398,8 +464,10 @@ | ||
| 398 | 464 | // PSR-0 include paths. |
| 399 | 465 | if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
| 400 | 466 | return $file; |
| 401 | 467 | } |
| 468 | + | |
| 469 | + return false; | |
| 402 | 470 | } |
| 403 | 471 | } |
| 404 | 472 | |
| 405 | 473 | /** |