1605f8e8dSAndreas Gohr<?php 2605f8e8dSAndreas Gohr 3605f8e8dSAndreas Gohr/* 4605f8e8dSAndreas Gohr * This file is part of Composer. 5605f8e8dSAndreas Gohr * 6605f8e8dSAndreas Gohr * (c) Nils Adermann <naderman@naderman.de> 7605f8e8dSAndreas Gohr * Jordi Boggiano <j.boggiano@seld.be> 8605f8e8dSAndreas Gohr * 9605f8e8dSAndreas Gohr * For the full copyright and license information, please view the LICENSE 10605f8e8dSAndreas Gohr * file that was distributed with this source code. 11605f8e8dSAndreas Gohr */ 12605f8e8dSAndreas Gohr 13605f8e8dSAndreas Gohrnamespace Composer\Autoload; 14605f8e8dSAndreas Gohr 15605f8e8dSAndreas Gohr/** 167a33d2f8SNiklas Keller * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. 17605f8e8dSAndreas Gohr * 18605f8e8dSAndreas Gohr * $loader = new \Composer\Autoload\ClassLoader(); 19605f8e8dSAndreas Gohr * 20605f8e8dSAndreas Gohr * // register classes with namespaces 21605f8e8dSAndreas Gohr * $loader->add('Symfony\Component', __DIR__.'/component'); 22605f8e8dSAndreas Gohr * $loader->add('Symfony', __DIR__.'/framework'); 23605f8e8dSAndreas Gohr * 24605f8e8dSAndreas Gohr * // activate the autoloader 25605f8e8dSAndreas Gohr * $loader->register(); 26605f8e8dSAndreas Gohr * 27605f8e8dSAndreas Gohr * // to enable searching the include path (eg. for PEAR packages) 28605f8e8dSAndreas Gohr * $loader->setUseIncludePath(true); 29605f8e8dSAndreas Gohr * 30605f8e8dSAndreas Gohr * In this example, if you try to use a class in the Symfony\Component 31605f8e8dSAndreas Gohr * namespace or one of its children (Symfony\Component\Console for instance), 32605f8e8dSAndreas Gohr * the autoloader will first look for the class under the component/ 33605f8e8dSAndreas Gohr * directory, and it will then fallback to the framework/ directory if not 34605f8e8dSAndreas Gohr * found before giving up. 35605f8e8dSAndreas Gohr * 36605f8e8dSAndreas Gohr * This class is loosely based on the Symfony UniversalClassLoader. 37605f8e8dSAndreas Gohr * 38605f8e8dSAndreas Gohr * @author Fabien Potencier <fabien@symfony.com> 39605f8e8dSAndreas Gohr * @author Jordi Boggiano <j.boggiano@seld.be> 406cb05674SAndreas Gohr * @see https://www.php-fig.org/psr/psr-0/ 416cb05674SAndreas Gohr * @see https://www.php-fig.org/psr/psr-4/ 42605f8e8dSAndreas Gohr */ 43605f8e8dSAndreas Gohrclass ClassLoader 44605f8e8dSAndreas Gohr{ 4528e9760aSAndreas Gohr /** @var \Closure(string):void */ 4628e9760aSAndreas Gohr private static $includeFile; 4728e9760aSAndreas Gohr 48*2cadabe7SAndreas Gohr /** @var string|null */ 496cb05674SAndreas Gohr private $vendorDir; 506cb05674SAndreas Gohr 51605f8e8dSAndreas Gohr // PSR-4 52d3233986SAndreas Gohr /** 53*2cadabe7SAndreas Gohr * @var array<string, array<string, int>> 54d3233986SAndreas Gohr */ 55605f8e8dSAndreas Gohr private $prefixLengthsPsr4 = array(); 56d3233986SAndreas Gohr /** 57*2cadabe7SAndreas Gohr * @var array<string, list<string>> 58d3233986SAndreas Gohr */ 59605f8e8dSAndreas Gohr private $prefixDirsPsr4 = array(); 60d3233986SAndreas Gohr /** 61*2cadabe7SAndreas Gohr * @var list<string> 62d3233986SAndreas Gohr */ 63605f8e8dSAndreas Gohr private $fallbackDirsPsr4 = array(); 64605f8e8dSAndreas Gohr 65605f8e8dSAndreas Gohr // PSR-0 66d3233986SAndreas Gohr /** 67*2cadabe7SAndreas Gohr * List of PSR-0 prefixes 68*2cadabe7SAndreas Gohr * 69*2cadabe7SAndreas Gohr * Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2'))) 70*2cadabe7SAndreas Gohr * 71*2cadabe7SAndreas Gohr * @var array<string, array<string, list<string>>> 72d3233986SAndreas Gohr */ 73605f8e8dSAndreas Gohr private $prefixesPsr0 = array(); 74d3233986SAndreas Gohr /** 75*2cadabe7SAndreas Gohr * @var list<string> 76d3233986SAndreas Gohr */ 77605f8e8dSAndreas Gohr private $fallbackDirsPsr0 = array(); 78605f8e8dSAndreas Gohr 79d3233986SAndreas Gohr /** @var bool */ 80605f8e8dSAndreas Gohr private $useIncludePath = false; 81d3233986SAndreas Gohr 82d3233986SAndreas Gohr /** 83*2cadabe7SAndreas Gohr * @var array<string, string> 84d3233986SAndreas Gohr */ 85605f8e8dSAndreas Gohr private $classMap = array(); 86d3233986SAndreas Gohr 87d3233986SAndreas Gohr /** @var bool */ 88605f8e8dSAndreas Gohr private $classMapAuthoritative = false; 89d3233986SAndreas Gohr 90d3233986SAndreas Gohr /** 91*2cadabe7SAndreas Gohr * @var array<string, bool> 92d3233986SAndreas Gohr */ 937a33d2f8SNiklas Keller private $missingClasses = array(); 94d3233986SAndreas Gohr 95*2cadabe7SAndreas Gohr /** @var string|null */ 96e0dd796dSAndreas Gohr private $apcuPrefix; 97605f8e8dSAndreas Gohr 98d3233986SAndreas Gohr /** 99*2cadabe7SAndreas Gohr * @var array<string, self> 100d3233986SAndreas Gohr */ 1016cb05674SAndreas Gohr private static $registeredLoaders = array(); 1026cb05674SAndreas Gohr 103d3233986SAndreas Gohr /** 104*2cadabe7SAndreas Gohr * @param string|null $vendorDir 105d3233986SAndreas Gohr */ 1066cb05674SAndreas Gohr public function __construct($vendorDir = null) 1076cb05674SAndreas Gohr { 1086cb05674SAndreas Gohr $this->vendorDir = $vendorDir; 10928e9760aSAndreas Gohr self::initializeIncludeClosure(); 1106cb05674SAndreas Gohr } 1116cb05674SAndreas Gohr 112d3233986SAndreas Gohr /** 113*2cadabe7SAndreas Gohr * @return array<string, list<string>> 114d3233986SAndreas Gohr */ 115605f8e8dSAndreas Gohr public function getPrefixes() 116605f8e8dSAndreas Gohr { 117605f8e8dSAndreas Gohr if (!empty($this->prefixesPsr0)) { 118a3bfbb3cSAndreas Gohr return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); 119605f8e8dSAndreas Gohr } 120605f8e8dSAndreas Gohr 121605f8e8dSAndreas Gohr return array(); 122605f8e8dSAndreas Gohr } 123605f8e8dSAndreas Gohr 124d3233986SAndreas Gohr /** 125*2cadabe7SAndreas Gohr * @return array<string, list<string>> 126d3233986SAndreas Gohr */ 127605f8e8dSAndreas Gohr public function getPrefixesPsr4() 128605f8e8dSAndreas Gohr { 129605f8e8dSAndreas Gohr return $this->prefixDirsPsr4; 130605f8e8dSAndreas Gohr } 131605f8e8dSAndreas Gohr 132d3233986SAndreas Gohr /** 133*2cadabe7SAndreas Gohr * @return list<string> 134d3233986SAndreas Gohr */ 135605f8e8dSAndreas Gohr public function getFallbackDirs() 136605f8e8dSAndreas Gohr { 137605f8e8dSAndreas Gohr return $this->fallbackDirsPsr0; 138605f8e8dSAndreas Gohr } 139605f8e8dSAndreas Gohr 140d3233986SAndreas Gohr /** 141*2cadabe7SAndreas Gohr * @return list<string> 142d3233986SAndreas Gohr */ 143605f8e8dSAndreas Gohr public function getFallbackDirsPsr4() 144605f8e8dSAndreas Gohr { 145605f8e8dSAndreas Gohr return $this->fallbackDirsPsr4; 146605f8e8dSAndreas Gohr } 147605f8e8dSAndreas Gohr 148d3233986SAndreas Gohr /** 149*2cadabe7SAndreas Gohr * @return array<string, string> Array of classname => path 150d3233986SAndreas Gohr */ 151605f8e8dSAndreas Gohr public function getClassMap() 152605f8e8dSAndreas Gohr { 153605f8e8dSAndreas Gohr return $this->classMap; 154605f8e8dSAndreas Gohr } 155605f8e8dSAndreas Gohr 156605f8e8dSAndreas Gohr /** 157*2cadabe7SAndreas Gohr * @param array<string, string> $classMap Class to filename map 158d3233986SAndreas Gohr * 159d3233986SAndreas Gohr * @return void 160605f8e8dSAndreas Gohr */ 161605f8e8dSAndreas Gohr public function addClassMap(array $classMap) 162605f8e8dSAndreas Gohr { 163605f8e8dSAndreas Gohr if ($this->classMap) { 164605f8e8dSAndreas Gohr $this->classMap = array_merge($this->classMap, $classMap); 165605f8e8dSAndreas Gohr } else { 166605f8e8dSAndreas Gohr $this->classMap = $classMap; 167605f8e8dSAndreas Gohr } 168605f8e8dSAndreas Gohr } 169605f8e8dSAndreas Gohr 170605f8e8dSAndreas Gohr /** 171605f8e8dSAndreas Gohr * Registers a set of PSR-0 directories for a given prefix, either 172605f8e8dSAndreas Gohr * appending or prepending to the ones previously set for this prefix. 173605f8e8dSAndreas Gohr * 174605f8e8dSAndreas Gohr * @param string $prefix The prefix 175*2cadabe7SAndreas Gohr * @param list<string>|string $paths The PSR-0 root directories 176605f8e8dSAndreas Gohr * @param bool $prepend Whether to prepend the directories 177d3233986SAndreas Gohr * 178d3233986SAndreas Gohr * @return void 179605f8e8dSAndreas Gohr */ 180605f8e8dSAndreas Gohr public function add($prefix, $paths, $prepend = false) 181605f8e8dSAndreas Gohr { 182*2cadabe7SAndreas Gohr $paths = (array) $paths; 183605f8e8dSAndreas Gohr if (!$prefix) { 184605f8e8dSAndreas Gohr if ($prepend) { 185605f8e8dSAndreas Gohr $this->fallbackDirsPsr0 = array_merge( 186*2cadabe7SAndreas Gohr $paths, 187605f8e8dSAndreas Gohr $this->fallbackDirsPsr0 188605f8e8dSAndreas Gohr ); 189605f8e8dSAndreas Gohr } else { 190605f8e8dSAndreas Gohr $this->fallbackDirsPsr0 = array_merge( 191605f8e8dSAndreas Gohr $this->fallbackDirsPsr0, 192*2cadabe7SAndreas Gohr $paths 193605f8e8dSAndreas Gohr ); 194605f8e8dSAndreas Gohr } 195605f8e8dSAndreas Gohr 196605f8e8dSAndreas Gohr return; 197605f8e8dSAndreas Gohr } 198605f8e8dSAndreas Gohr 199605f8e8dSAndreas Gohr $first = $prefix[0]; 200605f8e8dSAndreas Gohr if (!isset($this->prefixesPsr0[$first][$prefix])) { 201*2cadabe7SAndreas Gohr $this->prefixesPsr0[$first][$prefix] = $paths; 202605f8e8dSAndreas Gohr 203605f8e8dSAndreas Gohr return; 204605f8e8dSAndreas Gohr } 205605f8e8dSAndreas Gohr if ($prepend) { 206605f8e8dSAndreas Gohr $this->prefixesPsr0[$first][$prefix] = array_merge( 207*2cadabe7SAndreas Gohr $paths, 208605f8e8dSAndreas Gohr $this->prefixesPsr0[$first][$prefix] 209605f8e8dSAndreas Gohr ); 210605f8e8dSAndreas Gohr } else { 211605f8e8dSAndreas Gohr $this->prefixesPsr0[$first][$prefix] = array_merge( 212605f8e8dSAndreas Gohr $this->prefixesPsr0[$first][$prefix], 213*2cadabe7SAndreas Gohr $paths 214605f8e8dSAndreas Gohr ); 215605f8e8dSAndreas Gohr } 216605f8e8dSAndreas Gohr } 217605f8e8dSAndreas Gohr 218605f8e8dSAndreas Gohr /** 219605f8e8dSAndreas Gohr * Registers a set of PSR-4 directories for a given namespace, either 220605f8e8dSAndreas Gohr * appending or prepending to the ones previously set for this namespace. 221605f8e8dSAndreas Gohr * 222605f8e8dSAndreas Gohr * @param string $prefix The prefix/namespace, with trailing '\\' 223*2cadabe7SAndreas Gohr * @param list<string>|string $paths The PSR-4 base directories 224605f8e8dSAndreas Gohr * @param bool $prepend Whether to prepend the directories 225605f8e8dSAndreas Gohr * 226605f8e8dSAndreas Gohr * @throws \InvalidArgumentException 227d3233986SAndreas Gohr * 228d3233986SAndreas Gohr * @return void 229605f8e8dSAndreas Gohr */ 230605f8e8dSAndreas Gohr public function addPsr4($prefix, $paths, $prepend = false) 231605f8e8dSAndreas Gohr { 232*2cadabe7SAndreas Gohr $paths = (array) $paths; 233605f8e8dSAndreas Gohr if (!$prefix) { 234605f8e8dSAndreas Gohr // Register directories for the root namespace. 235605f8e8dSAndreas Gohr if ($prepend) { 236605f8e8dSAndreas Gohr $this->fallbackDirsPsr4 = array_merge( 237*2cadabe7SAndreas Gohr $paths, 238605f8e8dSAndreas Gohr $this->fallbackDirsPsr4 239605f8e8dSAndreas Gohr ); 240605f8e8dSAndreas Gohr } else { 241605f8e8dSAndreas Gohr $this->fallbackDirsPsr4 = array_merge( 242605f8e8dSAndreas Gohr $this->fallbackDirsPsr4, 243*2cadabe7SAndreas Gohr $paths 244605f8e8dSAndreas Gohr ); 245605f8e8dSAndreas Gohr } 246605f8e8dSAndreas Gohr } elseif (!isset($this->prefixDirsPsr4[$prefix])) { 247605f8e8dSAndreas Gohr // Register directories for a new namespace. 248605f8e8dSAndreas Gohr $length = strlen($prefix); 249605f8e8dSAndreas Gohr if ('\\' !== $prefix[$length - 1]) { 250605f8e8dSAndreas Gohr throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); 251605f8e8dSAndreas Gohr } 252605f8e8dSAndreas Gohr $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; 253*2cadabe7SAndreas Gohr $this->prefixDirsPsr4[$prefix] = $paths; 254605f8e8dSAndreas Gohr } elseif ($prepend) { 255605f8e8dSAndreas Gohr // Prepend directories for an already registered namespace. 256605f8e8dSAndreas Gohr $this->prefixDirsPsr4[$prefix] = array_merge( 257*2cadabe7SAndreas Gohr $paths, 258605f8e8dSAndreas Gohr $this->prefixDirsPsr4[$prefix] 259605f8e8dSAndreas Gohr ); 260605f8e8dSAndreas Gohr } else { 261605f8e8dSAndreas Gohr // Append directories for an already registered namespace. 262605f8e8dSAndreas Gohr $this->prefixDirsPsr4[$prefix] = array_merge( 263605f8e8dSAndreas Gohr $this->prefixDirsPsr4[$prefix], 264*2cadabe7SAndreas Gohr $paths 265605f8e8dSAndreas Gohr ); 266605f8e8dSAndreas Gohr } 267605f8e8dSAndreas Gohr } 268605f8e8dSAndreas Gohr 269605f8e8dSAndreas Gohr /** 270605f8e8dSAndreas Gohr * Registers a set of PSR-0 directories for a given prefix, 271605f8e8dSAndreas Gohr * replacing any others previously set for this prefix. 272605f8e8dSAndreas Gohr * 273605f8e8dSAndreas Gohr * @param string $prefix The prefix 274*2cadabe7SAndreas Gohr * @param list<string>|string $paths The PSR-0 base directories 275d3233986SAndreas Gohr * 276d3233986SAndreas Gohr * @return void 277605f8e8dSAndreas Gohr */ 278605f8e8dSAndreas Gohr public function set($prefix, $paths) 279605f8e8dSAndreas Gohr { 280605f8e8dSAndreas Gohr if (!$prefix) { 281605f8e8dSAndreas Gohr $this->fallbackDirsPsr0 = (array) $paths; 282605f8e8dSAndreas Gohr } else { 283605f8e8dSAndreas Gohr $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; 284605f8e8dSAndreas Gohr } 285605f8e8dSAndreas Gohr } 286605f8e8dSAndreas Gohr 287605f8e8dSAndreas Gohr /** 288605f8e8dSAndreas Gohr * Registers a set of PSR-4 directories for a given namespace, 289605f8e8dSAndreas Gohr * replacing any others previously set for this namespace. 290605f8e8dSAndreas Gohr * 291605f8e8dSAndreas Gohr * @param string $prefix The prefix/namespace, with trailing '\\' 292*2cadabe7SAndreas Gohr * @param list<string>|string $paths The PSR-4 base directories 293605f8e8dSAndreas Gohr * 294605f8e8dSAndreas Gohr * @throws \InvalidArgumentException 295d3233986SAndreas Gohr * 296d3233986SAndreas Gohr * @return void 297605f8e8dSAndreas Gohr */ 298605f8e8dSAndreas Gohr public function setPsr4($prefix, $paths) 299605f8e8dSAndreas Gohr { 300605f8e8dSAndreas Gohr if (!$prefix) { 301605f8e8dSAndreas Gohr $this->fallbackDirsPsr4 = (array) $paths; 302605f8e8dSAndreas Gohr } else { 303605f8e8dSAndreas Gohr $length = strlen($prefix); 304605f8e8dSAndreas Gohr if ('\\' !== $prefix[$length - 1]) { 305605f8e8dSAndreas Gohr throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); 306605f8e8dSAndreas Gohr } 307605f8e8dSAndreas Gohr $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; 308605f8e8dSAndreas Gohr $this->prefixDirsPsr4[$prefix] = (array) $paths; 309605f8e8dSAndreas Gohr } 310605f8e8dSAndreas Gohr } 311605f8e8dSAndreas Gohr 312605f8e8dSAndreas Gohr /** 313605f8e8dSAndreas Gohr * Turns on searching the include path for class files. 314605f8e8dSAndreas Gohr * 315605f8e8dSAndreas Gohr * @param bool $useIncludePath 316d3233986SAndreas Gohr * 317d3233986SAndreas Gohr * @return void 318605f8e8dSAndreas Gohr */ 319605f8e8dSAndreas Gohr public function setUseIncludePath($useIncludePath) 320605f8e8dSAndreas Gohr { 321605f8e8dSAndreas Gohr $this->useIncludePath = $useIncludePath; 322605f8e8dSAndreas Gohr } 323605f8e8dSAndreas Gohr 324605f8e8dSAndreas Gohr /** 325605f8e8dSAndreas Gohr * Can be used to check if the autoloader uses the include path to check 326605f8e8dSAndreas Gohr * for classes. 327605f8e8dSAndreas Gohr * 328605f8e8dSAndreas Gohr * @return bool 329605f8e8dSAndreas Gohr */ 330605f8e8dSAndreas Gohr public function getUseIncludePath() 331605f8e8dSAndreas Gohr { 332605f8e8dSAndreas Gohr return $this->useIncludePath; 333605f8e8dSAndreas Gohr } 334605f8e8dSAndreas Gohr 335605f8e8dSAndreas Gohr /** 336605f8e8dSAndreas Gohr * Turns off searching the prefix and fallback directories for classes 337605f8e8dSAndreas Gohr * that have not been registered with the class map. 338605f8e8dSAndreas Gohr * 339605f8e8dSAndreas Gohr * @param bool $classMapAuthoritative 340d3233986SAndreas Gohr * 341d3233986SAndreas Gohr * @return void 342605f8e8dSAndreas Gohr */ 343605f8e8dSAndreas Gohr public function setClassMapAuthoritative($classMapAuthoritative) 344605f8e8dSAndreas Gohr { 345605f8e8dSAndreas Gohr $this->classMapAuthoritative = $classMapAuthoritative; 346605f8e8dSAndreas Gohr } 347605f8e8dSAndreas Gohr 348605f8e8dSAndreas Gohr /** 349605f8e8dSAndreas Gohr * Should class lookup fail if not found in the current class map? 350605f8e8dSAndreas Gohr * 351605f8e8dSAndreas Gohr * @return bool 352605f8e8dSAndreas Gohr */ 353605f8e8dSAndreas Gohr public function isClassMapAuthoritative() 354605f8e8dSAndreas Gohr { 355605f8e8dSAndreas Gohr return $this->classMapAuthoritative; 356605f8e8dSAndreas Gohr } 357605f8e8dSAndreas Gohr 358605f8e8dSAndreas Gohr /** 359e0dd796dSAndreas Gohr * APCu prefix to use to cache found/not-found classes, if the extension is enabled. 360e0dd796dSAndreas Gohr * 361e0dd796dSAndreas Gohr * @param string|null $apcuPrefix 362d3233986SAndreas Gohr * 363d3233986SAndreas Gohr * @return void 364e0dd796dSAndreas Gohr */ 365e0dd796dSAndreas Gohr public function setApcuPrefix($apcuPrefix) 366e0dd796dSAndreas Gohr { 367e43cd7e1SAndreas Gohr $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; 368e0dd796dSAndreas Gohr } 369e0dd796dSAndreas Gohr 370e0dd796dSAndreas Gohr /** 371e0dd796dSAndreas Gohr * The APCu prefix in use, or null if APCu caching is not enabled. 372e0dd796dSAndreas Gohr * 373e0dd796dSAndreas Gohr * @return string|null 374e0dd796dSAndreas Gohr */ 375e0dd796dSAndreas Gohr public function getApcuPrefix() 376e0dd796dSAndreas Gohr { 377e0dd796dSAndreas Gohr return $this->apcuPrefix; 378e0dd796dSAndreas Gohr } 379e0dd796dSAndreas Gohr 380e0dd796dSAndreas Gohr /** 381605f8e8dSAndreas Gohr * Registers this instance as an autoloader. 382605f8e8dSAndreas Gohr * 383605f8e8dSAndreas Gohr * @param bool $prepend Whether to prepend the autoloader or not 384d3233986SAndreas Gohr * 385d3233986SAndreas Gohr * @return void 386605f8e8dSAndreas Gohr */ 387605f8e8dSAndreas Gohr public function register($prepend = false) 388605f8e8dSAndreas Gohr { 389605f8e8dSAndreas Gohr spl_autoload_register(array($this, 'loadClass'), true, $prepend); 3906cb05674SAndreas Gohr 3916cb05674SAndreas Gohr if (null === $this->vendorDir) { 3926cb05674SAndreas Gohr return; 3936cb05674SAndreas Gohr } 3946cb05674SAndreas Gohr 3956cb05674SAndreas Gohr if ($prepend) { 3966cb05674SAndreas Gohr self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; 3976cb05674SAndreas Gohr } else { 3986cb05674SAndreas Gohr unset(self::$registeredLoaders[$this->vendorDir]); 3996cb05674SAndreas Gohr self::$registeredLoaders[$this->vendorDir] = $this; 4006cb05674SAndreas Gohr } 401605f8e8dSAndreas Gohr } 402605f8e8dSAndreas Gohr 403605f8e8dSAndreas Gohr /** 404605f8e8dSAndreas Gohr * Unregisters this instance as an autoloader. 405d3233986SAndreas Gohr * 406d3233986SAndreas Gohr * @return void 407605f8e8dSAndreas Gohr */ 408605f8e8dSAndreas Gohr public function unregister() 409605f8e8dSAndreas Gohr { 410605f8e8dSAndreas Gohr spl_autoload_unregister(array($this, 'loadClass')); 4116cb05674SAndreas Gohr 4126cb05674SAndreas Gohr if (null !== $this->vendorDir) { 4136cb05674SAndreas Gohr unset(self::$registeredLoaders[$this->vendorDir]); 4146cb05674SAndreas Gohr } 415605f8e8dSAndreas Gohr } 416605f8e8dSAndreas Gohr 417605f8e8dSAndreas Gohr /** 418605f8e8dSAndreas Gohr * Loads the given class or interface. 419605f8e8dSAndreas Gohr * 420605f8e8dSAndreas Gohr * @param string $class The name of the class 421d3233986SAndreas Gohr * @return true|null True if loaded, null otherwise 422605f8e8dSAndreas Gohr */ 423605f8e8dSAndreas Gohr public function loadClass($class) 424605f8e8dSAndreas Gohr { 425605f8e8dSAndreas Gohr if ($file = $this->findFile($class)) { 42628e9760aSAndreas Gohr $includeFile = self::$includeFile; 42728e9760aSAndreas Gohr $includeFile($file); 428605f8e8dSAndreas Gohr 429605f8e8dSAndreas Gohr return true; 430605f8e8dSAndreas Gohr } 431d3233986SAndreas Gohr 432d3233986SAndreas Gohr return null; 433605f8e8dSAndreas Gohr } 434605f8e8dSAndreas Gohr 435605f8e8dSAndreas Gohr /** 436605f8e8dSAndreas Gohr * Finds the path to the file where the class is defined. 437605f8e8dSAndreas Gohr * 438605f8e8dSAndreas Gohr * @param string $class The name of the class 439605f8e8dSAndreas Gohr * 440605f8e8dSAndreas Gohr * @return string|false The path if found, false otherwise 441605f8e8dSAndreas Gohr */ 442605f8e8dSAndreas Gohr public function findFile($class) 443605f8e8dSAndreas Gohr { 444605f8e8dSAndreas Gohr // class map lookup 445605f8e8dSAndreas Gohr if (isset($this->classMap[$class])) { 446605f8e8dSAndreas Gohr return $this->classMap[$class]; 447605f8e8dSAndreas Gohr } 4487a33d2f8SNiklas Keller if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { 449605f8e8dSAndreas Gohr return false; 450605f8e8dSAndreas Gohr } 451e0dd796dSAndreas Gohr if (null !== $this->apcuPrefix) { 452e0dd796dSAndreas Gohr $file = apcu_fetch($this->apcuPrefix.$class, $hit); 453e0dd796dSAndreas Gohr if ($hit) { 454e0dd796dSAndreas Gohr return $file; 455e0dd796dSAndreas Gohr } 456e0dd796dSAndreas Gohr } 457605f8e8dSAndreas Gohr 458605f8e8dSAndreas Gohr $file = $this->findFileWithExtension($class, '.php'); 459605f8e8dSAndreas Gohr 460605f8e8dSAndreas Gohr // Search for Hack files if we are running on HHVM 4617a33d2f8SNiklas Keller if (false === $file && defined('HHVM_VERSION')) { 462605f8e8dSAndreas Gohr $file = $this->findFileWithExtension($class, '.hh'); 463605f8e8dSAndreas Gohr } 464605f8e8dSAndreas Gohr 465e0dd796dSAndreas Gohr if (null !== $this->apcuPrefix) { 466e0dd796dSAndreas Gohr apcu_add($this->apcuPrefix.$class, $file); 467e0dd796dSAndreas Gohr } 468e0dd796dSAndreas Gohr 4697a33d2f8SNiklas Keller if (false === $file) { 470605f8e8dSAndreas Gohr // Remember that this class does not exist. 4717a33d2f8SNiklas Keller $this->missingClasses[$class] = true; 472605f8e8dSAndreas Gohr } 473605f8e8dSAndreas Gohr 474605f8e8dSAndreas Gohr return $file; 475605f8e8dSAndreas Gohr } 476605f8e8dSAndreas Gohr 4776cb05674SAndreas Gohr /** 478*2cadabe7SAndreas Gohr * Returns the currently registered loaders keyed by their corresponding vendor directories. 4796cb05674SAndreas Gohr * 480*2cadabe7SAndreas Gohr * @return array<string, self> 4816cb05674SAndreas Gohr */ 4826cb05674SAndreas Gohr public static function getRegisteredLoaders() 4836cb05674SAndreas Gohr { 4846cb05674SAndreas Gohr return self::$registeredLoaders; 4856cb05674SAndreas Gohr } 4866cb05674SAndreas Gohr 487d3233986SAndreas Gohr /** 488d3233986SAndreas Gohr * @param string $class 489d3233986SAndreas Gohr * @param string $ext 490d3233986SAndreas Gohr * @return string|false 491d3233986SAndreas Gohr */ 492605f8e8dSAndreas Gohr private function findFileWithExtension($class, $ext) 493605f8e8dSAndreas Gohr { 494605f8e8dSAndreas Gohr // PSR-4 lookup 495605f8e8dSAndreas Gohr $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; 496605f8e8dSAndreas Gohr 497605f8e8dSAndreas Gohr $first = $class[0]; 498605f8e8dSAndreas Gohr if (isset($this->prefixLengthsPsr4[$first])) { 499e0dd796dSAndreas Gohr $subPath = $class; 500e0dd796dSAndreas Gohr while (false !== $lastPos = strrpos($subPath, '\\')) { 501e0dd796dSAndreas Gohr $subPath = substr($subPath, 0, $lastPos); 502e0dd796dSAndreas Gohr $search = $subPath . '\\'; 503e0dd796dSAndreas Gohr if (isset($this->prefixDirsPsr4[$search])) { 504e43cd7e1SAndreas Gohr $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); 505e0dd796dSAndreas Gohr foreach ($this->prefixDirsPsr4[$search] as $dir) { 506e43cd7e1SAndreas Gohr if (file_exists($file = $dir . $pathEnd)) { 507605f8e8dSAndreas Gohr return $file; 508605f8e8dSAndreas Gohr } 509605f8e8dSAndreas Gohr } 510605f8e8dSAndreas Gohr } 511605f8e8dSAndreas Gohr } 512605f8e8dSAndreas Gohr } 513605f8e8dSAndreas Gohr 514605f8e8dSAndreas Gohr // PSR-4 fallback dirs 515605f8e8dSAndreas Gohr foreach ($this->fallbackDirsPsr4 as $dir) { 5167a33d2f8SNiklas Keller if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { 517605f8e8dSAndreas Gohr return $file; 518605f8e8dSAndreas Gohr } 519605f8e8dSAndreas Gohr } 520605f8e8dSAndreas Gohr 521605f8e8dSAndreas Gohr // PSR-0 lookup 522605f8e8dSAndreas Gohr if (false !== $pos = strrpos($class, '\\')) { 523605f8e8dSAndreas Gohr // namespaced class name 524605f8e8dSAndreas Gohr $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) 525605f8e8dSAndreas Gohr . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); 526605f8e8dSAndreas Gohr } else { 527605f8e8dSAndreas Gohr // PEAR-like class name 528605f8e8dSAndreas Gohr $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; 529605f8e8dSAndreas Gohr } 530605f8e8dSAndreas Gohr 531605f8e8dSAndreas Gohr if (isset($this->prefixesPsr0[$first])) { 532605f8e8dSAndreas Gohr foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { 533605f8e8dSAndreas Gohr if (0 === strpos($class, $prefix)) { 534605f8e8dSAndreas Gohr foreach ($dirs as $dir) { 5357a33d2f8SNiklas Keller if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { 536605f8e8dSAndreas Gohr return $file; 537605f8e8dSAndreas Gohr } 538605f8e8dSAndreas Gohr } 539605f8e8dSAndreas Gohr } 540605f8e8dSAndreas Gohr } 541605f8e8dSAndreas Gohr } 542605f8e8dSAndreas Gohr 543605f8e8dSAndreas Gohr // PSR-0 fallback dirs 544605f8e8dSAndreas Gohr foreach ($this->fallbackDirsPsr0 as $dir) { 5457a33d2f8SNiklas Keller if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { 546605f8e8dSAndreas Gohr return $file; 547605f8e8dSAndreas Gohr } 548605f8e8dSAndreas Gohr } 549605f8e8dSAndreas Gohr 550605f8e8dSAndreas Gohr // PSR-0 include paths. 551605f8e8dSAndreas Gohr if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { 552605f8e8dSAndreas Gohr return $file; 553605f8e8dSAndreas Gohr } 5547a33d2f8SNiklas Keller 5557a33d2f8SNiklas Keller return false; 556605f8e8dSAndreas Gohr } 55728e9760aSAndreas Gohr 55828e9760aSAndreas Gohr /** 55928e9760aSAndreas Gohr * @return void 56028e9760aSAndreas Gohr */ 56128e9760aSAndreas Gohr private static function initializeIncludeClosure() 56228e9760aSAndreas Gohr { 56328e9760aSAndreas Gohr if (self::$includeFile !== null) { 56428e9760aSAndreas Gohr return; 565605f8e8dSAndreas Gohr } 566605f8e8dSAndreas Gohr 567605f8e8dSAndreas Gohr /** 568605f8e8dSAndreas Gohr * Scope isolated include. 569605f8e8dSAndreas Gohr * 570605f8e8dSAndreas Gohr * Prevents access to $this/self from included files. 571d3233986SAndreas Gohr * 572d3233986SAndreas Gohr * @param string $file 573d3233986SAndreas Gohr * @return void 574605f8e8dSAndreas Gohr */ 57528e9760aSAndreas Gohr self::$includeFile = \Closure::bind(static function($file) { 576605f8e8dSAndreas Gohr include $file; 57728e9760aSAndreas Gohr }, null, null); 57828e9760aSAndreas Gohr } 579605f8e8dSAndreas Gohr} 580