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