| @@ -1,34 +1,109 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +/* | |
| 4 | + * This file is part of Composer. | |
| 5 | + * | |
| 6 | + * (c) Nils Adermann <[email protected]> | |
| 7 | + * Jordi Boggiano <[email protected]> | |
| 8 | + * | |
| 9 | + * For the full copyright and license information, please view the LICENSE | |
| 10 | + * file that was distributed with this source code. | |
| 11 | + */ | |
| 3 | 12 | |
| 4 | 13 | namespace Composer\Autoload; |
| 5 | 14 | |
| 15 | +/** | |
| 16 | + * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. | |
| 17 | + * | |
| 18 | + * $loader = new \Composer\Autoload\ClassLoader(); | |
| 19 | + * | |
| 20 | + * // register classes with namespaces | |
| 21 | + * $loader->add('Symfony\Component', __DIR__.'/component'); | |
| 22 | + * $loader->add('Symfony', __DIR__.'/framework'); | |
| 23 | + * | |
| 24 | + * // activate the autoloader | |
| 25 | + * $loader->register(); | |
| 26 | + * | |
| 27 | + * // to enable searching the include path (eg. for PEAR packages) | |
| 28 | + * $loader->setUseIncludePath(true); | |
| 29 | + * | |
| 30 | + * In this example, if you try to use a class in the Symfony\Component | |
| 31 | + * namespace or one of its children (Symfony\Component\Console for instance), | |
| 32 | + * the autoloader will first look for the class under the component/ | |
| 33 | + * directory, and it will then fallback to the framework/ directory if not | |
| 34 | + * found before giving up. | |
| 35 | + * | |
| 36 | + * This class is loosely based on the Symfony UniversalClassLoader. | |
| 37 | + * | |
| 38 | + * @author Fabien Potencier <[email protected]> | |
| 39 | + * @author Jordi Boggiano <[email protected]> | |
| 40 | + * @see https://www.php-fig.org/psr/psr-0/ | |
| 41 | + * @see https://www.php-fig.org/psr/psr-4/ | |
| 42 | + */ | |
| 6 | 43 | class ClassLoader |
| 7 | 44 | { |
| 45 | + /** @var \Closure(string):void */ | |
| 8 | 46 | private static $includeFile; |
| 9 | 47 | |
| 48 | + /** @var string|null */ | |
| 10 | 49 | private $vendorDir; |
| 11 | 50 | |
| 51 | + // PSR-4 | |
| 52 | + /** | |
| 53 | + * @var array<string, array<string, int>> | |
| 54 | + */ | |
| 12 | 55 | private $prefixLengthsPsr4 = array(); |
| 56 | + /** | |
| 57 | + * @var array<string, list<string>> | |
| 58 | + */ | |
| 13 | 59 | private $prefixDirsPsr4 = array(); |
| 60 | + /** | |
| 61 | + * @var list<string> | |
| 62 | + */ | |
| 14 | 63 | private $fallbackDirsPsr4 = array(); |
| 15 | 64 | |
| 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 | + */ | |
| 16 | 73 | private $prefixesPsr0 = array(); |
| 74 | + /** | |
| 75 | + * @var list<string> | |
| 76 | + */ | |
| 17 | 77 | private $fallbackDirsPsr0 = array(); |
| 18 | 78 | |
| 79 | + /** @var bool */ | |
| 19 | 80 | private $useIncludePath = false; |
| 20 | 81 | |
| 82 | + /** | |
| 83 | + * @var array<string, string> | |
| 84 | + */ | |
| 21 | 85 | private $classMap = array(); |
| 22 | 86 | |
| 87 | + /** @var bool */ | |
| 23 | 88 | private $classMapAuthoritative = false; |
| 24 | 89 | |
| 90 | + /** | |
| 91 | + * @var array<string, bool> | |
| 92 | + */ | |
| 25 | 93 | private $missingClasses = array(); |
| 26 | 94 | |
| 95 | + /** @var string|null */ | |
| 27 | 96 | private $apcuPrefix; |
| 28 | 97 | |
| 98 | + /** | |
| 99 | + * @var array<string, self> | |
| 100 | + */ | |
| 29 | 101 | private static $registeredLoaders = array(); |
| 30 | 102 | |
| 103 | + /** | |
| 104 | + * @param string|null $vendorDir | |
| 105 | + */ | |
| 31 | 106 | public function __construct($vendorDir = null) |
| 32 | 107 | { |
| 33 | 108 | $this->vendorDir = $vendorDir; |
| 34 | 109 | self::initializeIncludeClosure(); |
| @@ -33,8 +108,11 @@ | ||
| 33 | 108 | $this->vendorDir = $vendorDir; |
| 34 | 109 | self::initializeIncludeClosure(); |
| 35 | 110 | } |
| 36 | 111 | |
| 112 | + /** | |
| 113 | + * @return array<string, list<string>> | |
| 114 | + */ | |
| 37 | 115 | public function getPrefixes() |
| 38 | 116 | { |
| 39 | 117 | if (!empty($this->prefixesPsr0)) { |
| 40 | 118 | return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
| @@ -42,28 +120,45 @@ | ||
| 42 | 120 | |
| 43 | 121 | return array(); |
| 44 | 122 | } |
| 45 | 123 | |
| 124 | + /** | |
| 125 | + * @return array<string, list<string>> | |
| 126 | + */ | |
| 46 | 127 | public function getPrefixesPsr4() |
| 47 | 128 | { |
| 48 | 129 | return $this->prefixDirsPsr4; |
| 49 | 130 | } |
| 50 | 131 | |
| 132 | + /** | |
| 133 | + * @return list<string> | |
| 134 | + */ | |
| 51 | 135 | public function getFallbackDirs() |
| 52 | 136 | { |
| 53 | 137 | return $this->fallbackDirsPsr0; |
| 54 | 138 | } |
| 55 | 139 | |
| 140 | + /** | |
| 141 | + * @return list<string> | |
| 142 | + */ | |
| 56 | 143 | public function getFallbackDirsPsr4() |
| 57 | 144 | { |
| 58 | 145 | return $this->fallbackDirsPsr4; |
| 59 | 146 | } |
| 60 | 147 | |
| 148 | + /** | |
| 149 | + * @return array<string, string> Array of classname => path | |
| 150 | + */ | |
| 61 | 151 | public function getClassMap() |
| 62 | 152 | { |
| 63 | 153 | return $this->classMap; |
| 64 | 154 | } |
| 65 | 155 | |
| 156 | + /** | |
| 157 | + * @param array<string, string> $classMap Class to filename map | |
| 158 | + * | |
| 159 | + * @return void | |
| 160 | + */ | |
| 66 | 161 | public function addClassMap(array $classMap) |
| 67 | 162 | { |
| 68 | 163 | if ($this->classMap) { |
| 69 | 164 | $this->classMap = array_merge($this->classMap, $classMap); |
| @@ -71,8 +166,18 @@ | ||
| 71 | 166 | $this->classMap = $classMap; |
| 72 | 167 | } |
| 73 | 168 | } |
| 74 | 169 | |
| 170 | + /** | |
| 171 | + * Registers a set of PSR-0 directories for a given prefix, either | |
| 172 | + * appending or prepending to the ones previously set for this prefix. | |
| 173 | + * | |
| 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 | |
| 179 | + */ | |
| 75 | 180 | public function add($prefix, $paths, $prepend = false) |
| 76 | 181 | { |
| 77 | 182 | $paths = (array) $paths; |
| 78 | 183 | if (!$prefix) { |
| @@ -109,12 +214,25 @@ | ||
| 109 | 214 | ); |
| 110 | 215 | } |
| 111 | 216 | } |
| 112 | 217 | |
| 218 | + /** | |
| 219 | + * Registers a set of PSR-4 directories for a given namespace, either | |
| 220 | + * appending or prepending to the ones previously set for this namespace. | |
| 221 | + * | |
| 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 | |
| 225 | + * | |
| 226 | + * @throws \InvalidArgumentException | |
| 227 | + * | |
| 228 | + * @return void | |
| 229 | + */ | |
| 113 | 230 | public function addPsr4($prefix, $paths, $prepend = false) |
| 114 | 231 | { |
| 115 | 232 | $paths = (array) $paths; |
| 116 | 233 | if (!$prefix) { |
| 234 | + // Register directories for the root namespace. | |
| 117 | 235 | if ($prepend) { |
| 118 | 236 | $this->fallbackDirsPsr4 = array_merge( |
| 119 | 237 | $paths, |
| 120 | 238 | $this->fallbackDirsPsr4 |
| @@ -125,8 +243,9 @@ | ||
| 125 | 243 | $paths |
| 126 | 244 | ); |
| 127 | 245 | } |
| 128 | 246 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
| 247 | + // Register directories for a new namespace. | |
| 129 | 248 | $length = strlen($prefix); |
| 130 | 249 | if ('\\' !== $prefix[$length - 1]) { |
| 131 | 250 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 132 | 251 | } |
| @@ -132,13 +251,15 @@ | ||
| 132 | 251 | } |
| 133 | 252 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 134 | 253 | $this->prefixDirsPsr4[$prefix] = $paths; |
| 135 | 254 | } elseif ($prepend) { |
| 255 | + // Prepend directories for an already registered namespace. | |
| 136 | 256 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 137 | 257 | $paths, |
| 138 | 258 | $this->prefixDirsPsr4[$prefix] |
| 139 | 259 | ); |
| 140 | 260 | } else { |
| 261 | + // Append directories for an already registered namespace. | |
| 141 | 262 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 142 | 263 | $this->prefixDirsPsr4[$prefix], |
| 143 | 264 | $paths |
| 144 | 265 | ); |
| @@ -144,8 +265,17 @@ | ||
| 144 | 265 | ); |
| 145 | 266 | } |
| 146 | 267 | } |
| 147 | 268 | |
| 269 | + /** | |
| 270 | + * Registers a set of PSR-0 directories for a given prefix, | |
| 271 | + * replacing any others previously set for this prefix. | |
| 272 | + * | |
| 273 | + * @param string $prefix The prefix | |
| 274 | + * @param list<string>|string $paths The PSR-0 base directories | |
| 275 | + * | |
| 276 | + * @return void | |
| 277 | + */ | |
| 148 | 278 | public function set($prefix, $paths) |
| 149 | 279 | { |
| 150 | 280 | if (!$prefix) { |
| 151 | 281 | $this->fallbackDirsPsr0 = (array) $paths; |
| @@ -153,8 +283,19 @@ | ||
| 153 | 283 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
| 154 | 284 | } |
| 155 | 285 | } |
| 156 | 286 | |
| 287 | + /** | |
| 288 | + * Registers a set of PSR-4 directories for a given namespace, | |
| 289 | + * replacing any others previously set for this namespace. | |
| 290 | + * | |
| 291 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 292 | + * @param list<string>|string $paths The PSR-4 base directories | |
| 293 | + * | |
| 294 | + * @throws \InvalidArgumentException | |
| 295 | + * | |
| 296 | + * @return void | |
| 297 | + */ | |
| 157 | 298 | public function setPsr4($prefix, $paths) |
| 158 | 299 | { |
| 159 | 300 | if (!$prefix) { |
| 160 | 301 | $this->fallbackDirsPsr4 = (array) $paths; |
| @@ -167,38 +308,83 @@ | ||
| 167 | 308 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| 168 | 309 | } |
| 169 | 310 | } |
| 170 | 311 | |
| 312 | + /** | |
| 313 | + * Turns on searching the include path for class files. | |
| 314 | + * | |
| 315 | + * @param bool $useIncludePath | |
| 316 | + * | |
| 317 | + * @return void | |
| 318 | + */ | |
| 171 | 319 | public function setUseIncludePath($useIncludePath) |
| 172 | 320 | { |
| 173 | 321 | $this->useIncludePath = $useIncludePath; |
| 174 | 322 | } |
| 175 | 323 | |
| 324 | + /** | |
| 325 | + * Can be used to check if the autoloader uses the include path to check | |
| 326 | + * for classes. | |
| 327 | + * | |
| 328 | + * @return bool | |
| 329 | + */ | |
| 176 | 330 | public function getUseIncludePath() |
| 177 | 331 | { |
| 178 | 332 | return $this->useIncludePath; |
| 179 | 333 | } |
| 180 | 334 | |
| 335 | + /** | |
| 336 | + * Turns off searching the prefix and fallback directories for classes | |
| 337 | + * that have not been registered with the class map. | |
| 338 | + * | |
| 339 | + * @param bool $classMapAuthoritative | |
| 340 | + * | |
| 341 | + * @return void | |
| 342 | + */ | |
| 181 | 343 | public function setClassMapAuthoritative($classMapAuthoritative) |
| 182 | 344 | { |
| 183 | 345 | $this->classMapAuthoritative = $classMapAuthoritative; |
| 184 | 346 | } |
| 185 | 347 | |
| 348 | + /** | |
| 349 | + * Should class lookup fail if not found in the current class map? | |
| 350 | + * | |
| 351 | + * @return bool | |
| 352 | + */ | |
| 186 | 353 | public function isClassMapAuthoritative() |
| 187 | 354 | { |
| 188 | 355 | return $this->classMapAuthoritative; |
| 189 | 356 | } |
| 190 | 357 | |
| 358 | + /** | |
| 359 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. | |
| 360 | + * | |
| 361 | + * @param string|null $apcuPrefix | |
| 362 | + * | |
| 363 | + * @return void | |
| 364 | + */ | |
| 191 | 365 | public function setApcuPrefix($apcuPrefix) |
| 192 | 366 | { |
| 193 | 367 | $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
| 194 | 368 | } |
| 195 | 369 | |
| 370 | + /** | |
| 371 | + * The APCu prefix in use, or null if APCu caching is not enabled. | |
| 372 | + * | |
| 373 | + * @return string|null | |
| 374 | + */ | |
| 196 | 375 | public function getApcuPrefix() |
| 197 | 376 | { |
| 198 | 377 | return $this->apcuPrefix; |
| 199 | 378 | } |
| 200 | 379 | |
| 380 | + /** | |
| 381 | + * Registers this instance as an autoloader. | |
| 382 | + * | |
| 383 | + * @param bool $prepend Whether to prepend the autoloader or not | |
| 384 | + * | |
| 385 | + * @return void | |
| 386 | + */ | |
| 201 | 387 | public function register($prepend = false) |
| 202 | 388 | { |
| 203 | 389 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 204 | 390 | |
| @@ -213,8 +399,13 @@ | ||
| 213 | 399 | self::$registeredLoaders[$this->vendorDir] = $this; |
| 214 | 400 | } |
| 215 | 401 | } |
| 216 | 402 | |
| 403 | + /** | |
| 404 | + * Unregisters this instance as an autoloader. | |
| 405 | + * | |
| 406 | + * @return void | |
| 407 | + */ | |
| 217 | 408 | public function unregister() |
| 218 | 409 | { |
| 219 | 410 | spl_autoload_unregister(array($this, 'loadClass')); |
| 220 | 411 | |
| @@ -222,8 +413,14 @@ | ||
| 222 | 413 | unset(self::$registeredLoaders[$this->vendorDir]); |
| 223 | 414 | } |
| 224 | 415 | } |
| 225 | 416 | |
| 417 | + /** | |
| 418 | + * Loads the given class or interface. | |
| 419 | + * | |
| 420 | + * @param string $class The name of the class | |
| 421 | + * @return true|null True if loaded, null otherwise | |
| 422 | + */ | |
| 226 | 423 | public function loadClass($class) |
| 227 | 424 | { |
| 228 | 425 | if ($file = $this->findFile($class)) { |
| 229 | 426 | $includeFile = self::$includeFile; |
| @@ -234,10 +431,18 @@ | ||
| 234 | 431 | |
| 235 | 432 | return null; |
| 236 | 433 | } |
| 237 | 434 | |
| 435 | + /** | |
| 436 | + * Finds the path to the file where the class is defined. | |
| 437 | + * | |
| 438 | + * @param string $class The name of the class | |
| 439 | + * | |
| 440 | + * @return string|false The path if found, false otherwise | |
| 441 | + */ | |
| 238 | 442 | public function findFile($class) |
| 239 | 443 | { |
| 444 | + // class map lookup | |
| 240 | 445 | if (isset($this->classMap[$class])) { |
| 241 | 446 | return $this->classMap[$class]; |
| 242 | 447 | } |
| 243 | 448 | if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
| @@ -251,8 +456,9 @@ | ||
| 251 | 456 | } |
| 252 | 457 | |
| 253 | 458 | $file = $this->findFileWithExtension($class, '.php'); |
| 254 | 459 | |
| 460 | + // Search for Hack files if we are running on HHVM | |
| 255 | 461 | if (false === $file && defined('HHVM_VERSION')) { |
| 256 | 462 | $file = $this->findFileWithExtension($class, '.hh'); |
| 257 | 463 | } |
| 258 | 464 | |
| @@ -260,8 +466,9 @@ | ||
| 260 | 466 | apcu_add($this->apcuPrefix.$class, $file); |
| 261 | 467 | } |
| 262 | 468 | |
| 263 | 469 | if (false === $file) { |
| 470 | + // Remember that this class does not exist. | |
| 264 | 471 | $this->missingClasses[$class] = true; |
| 265 | 472 | } |
| 266 | 473 | |
| 267 | 474 | return $file; |
| @@ -266,15 +473,26 @@ | ||
| 266 | 473 | |
| 267 | 474 | return $file; |
| 268 | 475 | } |
| 269 | 476 | |
| 477 | + /** | |
| 478 | + * Returns the currently registered loaders keyed by their corresponding vendor directories. | |
| 479 | + * | |
| 480 | + * @return array<string, self> | |
| 481 | + */ | |
| 270 | 482 | public static function getRegisteredLoaders() |
| 271 | 483 | { |
| 272 | 484 | return self::$registeredLoaders; |
| 273 | 485 | } |
| 274 | 486 | |
| 487 | + /** | |
| 488 | + * @param string $class | |
| 489 | + * @param string $ext | |
| 490 | + * @return string|false | |
| 491 | + */ | |
| 275 | 492 | private function findFileWithExtension($class, $ext) |
| 276 | 493 | { |
| 494 | + // PSR-4 lookup | |
| 277 | 495 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| 278 | 496 | |
| 279 | 497 | $first = $class[0]; |
| 280 | 498 | if (isset($this->prefixLengthsPsr4[$first])) { |
| @@ -292,8 +510,9 @@ | ||
| 292 | 510 | } |
| 293 | 511 | } |
| 294 | 512 | } |
| 295 | 513 | |
| 514 | + // PSR-4 fallback dirs | |
| 296 | 515 | foreach ($this->fallbackDirsPsr4 as $dir) { |
| 297 | 516 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
| 298 | 517 | return $file; |
| 299 | 518 | } |
| @@ -298,12 +517,15 @@ | ||
| 298 | 517 | return $file; |
| 299 | 518 | } |
| 300 | 519 | } |
| 301 | 520 | |
| 521 | + // PSR-0 lookup | |
| 302 | 522 | if (false !== $pos = strrpos($class, '\\')) { |
| 523 | + // namespaced class name | |
| 303 | 524 | $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
| 304 | 525 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
| 305 | 526 | } else { |
| 527 | + // PEAR-like class name | |
| 306 | 528 | $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
| 307 | 529 | } |
| 308 | 530 | |
| 309 | 531 | if (isset($this->prefixesPsr0[$first])) { |
| @@ -317,8 +539,9 @@ | ||
| 317 | 539 | } |
| 318 | 540 | } |
| 319 | 541 | } |
| 320 | 542 | |
| 543 | + // PSR-0 fallback dirs | |
| 321 | 544 | foreach ($this->fallbackDirsPsr0 as $dir) { |
| 322 | 545 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| 323 | 546 | return $file; |
| 324 | 547 | } |
| @@ -323,8 +546,9 @@ | ||
| 323 | 546 | return $file; |
| 324 | 547 | } |
| 325 | 548 | } |
| 326 | 549 | |
| 550 | + // PSR-0 include paths. | |
| 327 | 551 | if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
| 328 | 552 | return $file; |
| 329 | 553 | } |
| 330 | 554 | |
| @@ -330,8 +554,11 @@ | ||
| 330 | 554 | |
| 331 | 555 | return false; |
| 332 | 556 | } |
| 333 | 557 | |
| 558 | + /** | |
| 559 | + * @return void | |
| 560 | + */ | |
| 334 | 561 | private static function initializeIncludeClosure() |
| 335 | 562 | { |
| 336 | 563 | if (self::$includeFile !== null) { |
| 337 | 564 | return; |
| @@ -336,8 +563,16 @@ | ||
| 336 | 563 | if (self::$includeFile !== null) { |
| 337 | 564 | return; |
| 338 | 565 | } |
| 339 | 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 | + */ | |
| 340 | 575 | self::$includeFile = \Closure::bind(static function($file) { |
| 341 | 576 | include $file; |
| 342 | 577 | }, null, null); |
| 343 | 578 | } |