| @@ -36,119 +36,52 @@ | ||
| 36 | 36 | * This class is loosely based on the Symfony UniversalClassLoader. |
| 37 | 37 | * |
| 38 | 38 | * @author Fabien Potencier <[email protected]> |
| 39 | 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/ | |
| 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 | - | |
| 87 | - /** @var bool */ | |
| 88 | 56 | private $classMapAuthoritative = false; |
| 89 | - | |
| 90 | - /** | |
| 91 | - * @var array<string, bool> | |
| 92 | - */ | |
| 93 | 57 | private $missingClasses = array(); |
| 94 | - | |
| 95 | - /** @var string|null */ | |
| 96 | 58 | private $apcuPrefix; |
| 97 | 59 | |
| 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 | 60 | public function getPrefixes() |
| 116 | 61 | { |
| 117 | 62 | if (!empty($this->prefixesPsr0)) { |
| 118 | - return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); | |
| 63 | + return call_user_func_array('array_merge', $this->prefixesPsr0); | |
| 119 | 64 | } |
| 120 | 65 | |
| 121 | 66 | return array(); |
| 122 | 67 | } |
| 123 | 68 | |
| 124 | - /** | |
| 125 | - * @return array<string, list<string>> | |
| 126 | - */ | |
| 127 | 69 | public function getPrefixesPsr4() |
| 128 | 70 | { |
| 129 | 71 | return $this->prefixDirsPsr4; |
| 130 | 72 | } |
| 131 | 73 | |
| 132 | - /** | |
| 133 | - * @return list<string> | |
| 134 | - */ | |
| 135 | 74 | public function getFallbackDirs() |
| 136 | 75 | { |
| 137 | 76 | return $this->fallbackDirsPsr0; |
| 138 | 77 | } |
| 139 | 78 | |
| 140 | - /** | |
| 141 | - * @return list<string> | |
| 142 | - */ | |
| 143 | 79 | public function getFallbackDirsPsr4() |
| 144 | 80 | { |
| 145 | 81 | return $this->fallbackDirsPsr4; |
| 146 | 82 | } |
| 147 | 83 | |
| 148 | - /** | |
| 149 | - * @return array<string, string> Array of classname => path | |
| 150 | - */ | |
| 151 | 84 | public function getClassMap() |
| 152 | 85 | { |
| 153 | 86 | return $this->classMap; |
| 154 | 87 | } |
| @@ -153,11 +86,9 @@ | ||
| 153 | 86 | return $this->classMap; |
| 154 | 87 | } |
| 155 | 88 | |
| 156 | 89 | /** |
| 157 | - * @param array<string, string> $classMap Class to filename map | |
| 158 | - * | |
| 159 | - * @return void | |
| 90 | + * @param array $classMap Class to filename map | |
| 160 | 91 | */ |
| 161 | 92 | public function addClassMap(array $classMap) |
| 162 | 93 | { |
| 163 | 94 | if ($this->classMap) { |
| @@ -170,27 +101,24 @@ | ||
| 170 | 101 | /** |
| 171 | 102 | * Registers a set of PSR-0 directories for a given prefix, either |
| 172 | 103 | * appending or prepending to the ones previously set for this prefix. |
| 173 | 104 | * |
| 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 | |
| 105 | + * @param string $prefix The prefix | |
| 106 | + * @param array|string $paths The PSR-0 root directories | |
| 107 | + * @param bool $prepend Whether to prepend the directories | |
| 179 | 108 | */ |
| 180 | 109 | public function add($prefix, $paths, $prepend = false) |
| 181 | 110 | { |
| 182 | - $paths = (array) $paths; | |
| 183 | 111 | if (!$prefix) { |
| 184 | 112 | if ($prepend) { |
| 185 | 113 | $this->fallbackDirsPsr0 = array_merge( |
| 186 | - $paths, | |
| 114 | + (array) $paths, | |
| 187 | 115 | $this->fallbackDirsPsr0 |
| 188 | 116 | ); |
| 189 | 117 | } else { |
| 190 | 118 | $this->fallbackDirsPsr0 = array_merge( |
| 191 | 119 | $this->fallbackDirsPsr0, |
| 192 | - $paths | |
| 120 | + (array) $paths | |
| 193 | 121 | ); |
| 194 | 122 | } |
| 195 | 123 | |
| 196 | 124 | return; |
| @@ -197,21 +125,21 @@ | ||
| 197 | 125 | } |
| 198 | 126 | |
| 199 | 127 | $first = $prefix[0]; |
| 200 | 128 | if (!isset($this->prefixesPsr0[$first][$prefix])) { |
| 201 | - $this->prefixesPsr0[$first][$prefix] = $paths; | |
| 129 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; | |
| 202 | 130 | |
| 203 | 131 | return; |
| 204 | 132 | } |
| 205 | 133 | if ($prepend) { |
| 206 | 134 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 207 | - $paths, | |
| 135 | + (array) $paths, | |
| 208 | 136 | $this->prefixesPsr0[$first][$prefix] |
| 209 | 137 | ); |
| 210 | 138 | } else { |
| 211 | 139 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| 212 | 140 | $this->prefixesPsr0[$first][$prefix], |
| 213 | - $paths | |
| 141 | + (array) $paths | |
| 214 | 142 | ); |
| 215 | 143 | } |
| 216 | 144 | } |
| 217 | 145 | |
| @@ -218,30 +146,27 @@ | ||
| 218 | 146 | /** |
| 219 | 147 | * Registers a set of PSR-4 directories for a given namespace, either |
| 220 | 148 | * appending or prepending to the ones previously set for this namespace. |
| 221 | 149 | * |
| 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 | |
| 150 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 151 | + * @param array|string $paths The PSR-4 base directories | |
| 152 | + * @param bool $prepend Whether to prepend the directories | |
| 225 | 153 | * |
| 226 | 154 | * @throws \InvalidArgumentException |
| 227 | - * | |
| 228 | - * @return void | |
| 229 | 155 | */ |
| 230 | 156 | public function addPsr4($prefix, $paths, $prepend = false) |
| 231 | 157 | { |
| 232 | - $paths = (array) $paths; | |
| 233 | 158 | if (!$prefix) { |
| 234 | 159 | // Register directories for the root namespace. |
| 235 | 160 | if ($prepend) { |
| 236 | 161 | $this->fallbackDirsPsr4 = array_merge( |
| 237 | - $paths, | |
| 162 | + (array) $paths, | |
| 238 | 163 | $this->fallbackDirsPsr4 |
| 239 | 164 | ); |
| 240 | 165 | } else { |
| 241 | 166 | $this->fallbackDirsPsr4 = array_merge( |
| 242 | 167 | $this->fallbackDirsPsr4, |
| 243 | - $paths | |
| 168 | + (array) $paths | |
| 244 | 169 | ); |
| 245 | 170 | } |
| 246 | 171 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
| 247 | 172 | // Register directories for a new namespace. |
| @@ -249,13 +174,13 @@ | ||
| 249 | 174 | if ('\\' !== $prefix[$length - 1]) { |
| 250 | 175 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| 251 | 176 | } |
| 252 | 177 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| 253 | - $this->prefixDirsPsr4[$prefix] = $paths; | |
| 178 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; | |
| 254 | 179 | } elseif ($prepend) { |
| 255 | 180 | // Prepend directories for an already registered namespace. |
| 256 | 181 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 257 | - $paths, | |
| 182 | + (array) $paths, | |
| 258 | 183 | $this->prefixDirsPsr4[$prefix] |
| 259 | 184 | ); |
| 260 | 185 | } else { |
| 261 | 186 | // Append directories for an already registered namespace. |
| @@ -260,9 +185,9 @@ | ||
| 260 | 185 | } else { |
| 261 | 186 | // Append directories for an already registered namespace. |
| 262 | 187 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| 263 | 188 | $this->prefixDirsPsr4[$prefix], |
| 264 | - $paths | |
| 189 | + (array) $paths | |
| 265 | 190 | ); |
| 266 | 191 | } |
| 267 | 192 | } |
| 268 | 193 | |
| @@ -269,12 +194,10 @@ | ||
| 269 | 194 | /** |
| 270 | 195 | * Registers a set of PSR-0 directories for a given prefix, |
| 271 | 196 | * replacing any others previously set for this prefix. |
| 272 | 197 | * |
| 273 | - * @param string $prefix The prefix | |
| 274 | - * @param list<string>|string $paths The PSR-0 base directories | |
| 275 | - * | |
| 276 | - * @return void | |
| 198 | + * @param string $prefix The prefix | |
| 199 | + * @param array|string $paths The PSR-0 base directories | |
| 277 | 200 | */ |
| 278 | 201 | public function set($prefix, $paths) |
| 279 | 202 | { |
| 280 | 203 | if (!$prefix) { |
| @@ -287,14 +210,12 @@ | ||
| 287 | 210 | /** |
| 288 | 211 | * Registers a set of PSR-4 directories for a given namespace, |
| 289 | 212 | * replacing any others previously set for this namespace. |
| 290 | 213 | * |
| 291 | - * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 292 | - * @param list<string>|string $paths The PSR-4 base directories | |
| 214 | + * @param string $prefix The prefix/namespace, with trailing '\\' | |
| 215 | + * @param array|string $paths The PSR-4 base directories | |
| 293 | 216 | * |
| 294 | 217 | * @throws \InvalidArgumentException |
| 295 | - * | |
| 296 | - * @return void | |
| 297 | 218 | */ |
| 298 | 219 | public function setPsr4($prefix, $paths) |
| 299 | 220 | { |
| 300 | 221 | if (!$prefix) { |
| @@ -312,10 +233,8 @@ | ||
| 312 | 233 | /** |
| 313 | 234 | * Turns on searching the include path for class files. |
| 314 | 235 | * |
| 315 | 236 | * @param bool $useIncludePath |
| 316 | - * | |
| 317 | - * @return void | |
| 318 | 237 | */ |
| 319 | 238 | public function setUseIncludePath($useIncludePath) |
| 320 | 239 | { |
| 321 | 240 | $this->useIncludePath = $useIncludePath; |
| @@ -336,10 +255,8 @@ | ||
| 336 | 255 | * Turns off searching the prefix and fallback directories for classes |
| 337 | 256 | * that have not been registered with the class map. |
| 338 | 257 | * |
| 339 | 258 | * @param bool $classMapAuthoritative |
| 340 | - * | |
| 341 | - * @return void | |
| 342 | 259 | */ |
| 343 | 260 | public function setClassMapAuthoritative($classMapAuthoritative) |
| 344 | 261 | { |
| 345 | 262 | $this->classMapAuthoritative = $classMapAuthoritative; |
| @@ -358,14 +275,12 @@ | ||
| 358 | 275 | /** |
| 359 | 276 | * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
| 360 | 277 | * |
| 361 | 278 | * @param string|null $apcuPrefix |
| 362 | - * | |
| 363 | - * @return void | |
| 364 | 279 | */ |
| 365 | 280 | public function setApcuPrefix($apcuPrefix) |
| 366 | 281 | { |
| 367 | - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; | |
| 282 | + $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null; | |
| 368 | 283 | } |
| 369 | 284 | |
| 370 | 285 | /** |
| 371 | 286 | * The APCu prefix in use, or null if APCu caching is not enabled. |
| @@ -380,39 +295,20 @@ | ||
| 380 | 295 | /** |
| 381 | 296 | * Registers this instance as an autoloader. |
| 382 | 297 | * |
| 383 | 298 | * @param bool $prepend Whether to prepend the autoloader or not |
| 384 | - * | |
| 385 | - * @return void | |
| 386 | 299 | */ |
| 387 | 300 | public function register($prepend = false) |
| 388 | 301 | { |
| 389 | 302 | 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 | 303 | } |
| 402 | 304 | |
| 403 | 305 | /** |
| 404 | 306 | * Unregisters this instance as an autoloader. |
| 405 | - * | |
| 406 | - * @return void | |
| 407 | 307 | */ |
| 408 | 308 | public function unregister() |
| 409 | 309 | { |
| 410 | 310 | spl_autoload_unregister(array($this, 'loadClass')); |
| 411 | - | |
| 412 | - if (null !== $this->vendorDir) { | |
| 413 | - unset(self::$registeredLoaders[$this->vendorDir]); | |
| 414 | - } | |
| 415 | 311 | } |
| 416 | 312 | |
| 417 | 313 | /** |
| 418 | 314 | * Loads the given class or interface. |
| @@ -417,20 +313,17 @@ | ||
| 417 | 313 | /** |
| 418 | 314 | * Loads the given class or interface. |
| 419 | 315 | * |
| 420 | 316 | * @param string $class The name of the class |
| 421 | - * @return true|null True if loaded, null otherwise | |
| 317 | + * @return bool|null True if loaded, null otherwise | |
| 422 | 318 | */ |
| 423 | 319 | public function loadClass($class) |
| 424 | 320 | { |
| 425 | 321 | if ($file = $this->findFile($class)) { |
| 426 | - $includeFile = self::$includeFile; | |
| 427 | - $includeFile($file); | |
| 322 | + includeFile($file); | |
| 428 | 323 | |
| 429 | 324 | return true; |
| 430 | 325 | } |
| 431 | - | |
| 432 | - return null; | |
| 433 | 326 | } |
| 434 | 327 | |
| 435 | 328 | /** |
| 436 | 329 | * Finds the path to the file where the class is defined. |
| @@ -473,23 +366,8 @@ | ||
| 473 | 366 | |
| 474 | 367 | return $file; |
| 475 | 368 | } |
| 476 | 369 | |
| 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 | 370 | private function findFileWithExtension($class, $ext) |
| 493 | 371 | { |
| 494 | 372 | // PSR-4 lookup |
| 495 | 373 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| @@ -498,9 +376,9 @@ | ||
| 498 | 376 | if (isset($this->prefixLengthsPsr4[$first])) { |
| 499 | 377 | $subPath = $class; |
| 500 | 378 | while (false !== $lastPos = strrpos($subPath, '\\')) { |
| 501 | 379 | $subPath = substr($subPath, 0, $lastPos); |
| 502 | - $search = $subPath . '\\'; | |
| 380 | + $search = $subPath.'\\'; | |
| 503 | 381 | if (isset($this->prefixDirsPsr4[$search])) { |
| 504 | 382 | $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
| 505 | 383 | foreach ($this->prefixDirsPsr4[$search] as $dir) { |
| 506 | 384 | if (file_exists($file = $dir . $pathEnd)) { |
| @@ -553,27 +431,15 @@ | ||
| 553 | 431 | } |
| 554 | 432 | |
| 555 | 433 | return false; |
| 556 | 434 | } |
| 435 | +} | |
| 557 | 436 | |
| 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 | - } | |
| 437 | +/** | |
| 438 | + * Scope isolated include. | |
| 439 | + * | |
| 440 | + * Prevents access to $this/self from included files. | |
| 441 | + */ | |
| 442 | +function includeFile($file) | |
| 443 | +{ | |
| 444 | + include $file; | |
| 579 | 445 | } |