PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 3.7.2
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v3.7.2
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
← All changes | vendor/composer/ClassLoader.php +146 -19 2.4.2 → 3.7.2 View file →
@@ -36,52 +36,122 @@
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 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 + /** @var ?string */
46 + private $vendorDir;
47 +
45 48 // PSR-4
49 + /**
50 + * @var array[]
51 + * @psalm-var array<string, array<string, int>>
52 + */
46 53 private $prefixLengthsPsr4 = array();
54 + /**
55 + * @var array[]
56 + * @psalm-var array<string, array<int, string>>
57 + */
47 58 private $prefixDirsPsr4 = array();
59 + /**
60 + * @var array[]
61 + * @psalm-var array<string, string>
62 + */
48 63 private $fallbackDirsPsr4 = array();
49 64
50 65 // PSR-0
66 + /**
67 + * @var array[]
68 + * @psalm-var array<string, array<string, string[]>>
69 + */
51 70 private $prefixesPsr0 = array();
71 + /**
72 + * @var array[]
73 + * @psalm-var array<string, string>
74 + */
52 75 private $fallbackDirsPsr0 = array();
53 76
77 + /** @var bool */
54 78 private $useIncludePath = false;
79 +
80 + /**
81 + * @var string[]
82 + * @psalm-var array<string, string>
83 + */
55 84 private $classMap = array();
85 +
86 + /** @var bool */
56 87 private $classMapAuthoritative = false;
88 +
89 + /**
90 + * @var bool[]
91 + * @psalm-var array<string, bool>
92 + */
57 93 private $missingClasses = array();
94 +
95 + /** @var ?string */
58 96 private $apcuPrefix;
59 97
98 + /**
99 + * @var self[]
100 + */
101 + private static $registeredLoaders = array();
102 +
103 + /**
104 + * @param ?string $vendorDir
105 + */
106 + public function __construct($vendorDir = null)
107 + {
108 + $this->vendorDir = $vendorDir;
109 + }
110 +
111 + /**
112 + * @return string[]
113 + */
60 114 public function getPrefixes()
61 115 {
62 116 if (!empty($this->prefixesPsr0)) {
63 - return call_user_func_array('array_merge', $this->prefixesPsr0);
117 + return call_user_func_array('array_merge', array_values($this->prefixesPsr0));
64 118 }
65 119
66 120 return array();
67 121 }
68 122
123 + /**
124 + * @return array[]
125 + * @psalm-return array<string, array<int, string>>
126 + */
69 127 public function getPrefixesPsr4()
70 128 {
71 129 return $this->prefixDirsPsr4;
72 130 }
73 131
132 + /**
133 + * @return array[]
134 + * @psalm-return array<string, string>
135 + */
74 136 public function getFallbackDirs()
75 137 {
76 138 return $this->fallbackDirsPsr0;
77 139 }
78 140
141 + /**
142 + * @return array[]
143 + * @psalm-return array<string, string>
144 + */
79 145 public function getFallbackDirsPsr4()
80 146 {
81 147 return $this->fallbackDirsPsr4;
82 148 }
83 149
150 + /**
151 + * @return string[] Array of classname => path
152 + * @psalm-return array<string, string>
153 + */
84 154 public function getClassMap()
85 155 {
86 156 return $this->classMap;
87 157 }
@@ -86,9 +156,12 @@
86 156 return $this->classMap;
87 157 }
88 158
89 159 /**
90 - * @param array $classMap Class to filename map
160 + * @param string[] $classMap Class to filename map
161 + * @psalm-param array<string, string> $classMap
162 + *
163 + * @return void
91 164 */
92 165 public function addClassMap(array $classMap)
93 166 {
94 167 if ($this->classMap) {
@@ -101,11 +174,13 @@
101 174 /**
102 175 * Registers a set of PSR-0 directories for a given prefix, either
103 176 * appending or prepending to the ones previously set for this prefix.
104 177 *
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
178 + * @param string $prefix The prefix
179 + * @param string[]|string $paths The PSR-0 root directories
180 + * @param bool $prepend Whether to prepend the directories
181 + *
182 + * @return void
108 183 */
109 184 public function add($prefix, $paths, $prepend = false)
110 185 {
111 186 if (!$prefix) {
@@ -146,13 +221,15 @@
146 221 /**
147 222 * Registers a set of PSR-4 directories for a given namespace, either
148 223 * appending or prepending to the ones previously set for this namespace.
149 224 *
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 + * @param string $prefix The prefix/namespace, with trailing '\\'
226 + * @param string[]|string $paths The PSR-4 base directories
227 + * @param bool $prepend Whether to prepend the directories
153 228 *
154 229 * @throws \InvalidArgumentException
230 + *
231 + * @return void
155 232 */
156 233 public function addPsr4($prefix, $paths, $prepend = false)
157 234 {
158 235 if (!$prefix) {
@@ -194,10 +271,12 @@
194 271 /**
195 272 * Registers a set of PSR-0 directories for a given prefix,
196 273 * replacing any others previously set for this prefix.
197 274 *
198 - * @param string $prefix The prefix
199 - * @param array|string $paths The PSR-0 base directories
275 + * @param string $prefix The prefix
276 + * @param string[]|string $paths The PSR-0 base directories
277 + *
278 + * @return void
200 279 */
201 280 public function set($prefix, $paths)
202 281 {
203 282 if (!$prefix) {
@@ -210,12 +289,14 @@
210 289 /**
211 290 * Registers a set of PSR-4 directories for a given namespace,
212 291 * replacing any others previously set for this namespace.
213 292 *
214 - * @param string $prefix The prefix/namespace, with trailing '\\'
215 - * @param array|string $paths The PSR-4 base directories
293 + * @param string $prefix The prefix/namespace, with trailing '\\'
294 + * @param string[]|string $paths The PSR-4 base directories
216 295 *
217 296 * @throws \InvalidArgumentException
297 + *
298 + * @return void
218 299 */
219 300 public function setPsr4($prefix, $paths)
220 301 {
221 302 if (!$prefix) {
@@ -233,8 +314,10 @@
233 314 /**
234 315 * Turns on searching the include path for class files.
235 316 *
236 317 * @param bool $useIncludePath
318 + *
319 + * @return void
237 320 */
238 321 public function setUseIncludePath($useIncludePath)
239 322 {
240 323 $this->useIncludePath = $useIncludePath;
@@ -255,8 +338,10 @@
255 338 * Turns off searching the prefix and fallback directories for classes
256 339 * that have not been registered with the class map.
257 340 *
258 341 * @param bool $classMapAuthoritative
342 + *
343 + * @return void
259 344 */
260 345 public function setClassMapAuthoritative($classMapAuthoritative)
261 346 {
262 347 $this->classMapAuthoritative = $classMapAuthoritative;
@@ -275,12 +360,14 @@
275 360 /**
276 361 * APCu prefix to use to cache found/not-found classes, if the extension is enabled.
277 362 *
278 363 * @param string|null $apcuPrefix
364 + *
365 + * @return void
279 366 */
280 367 public function setApcuPrefix($apcuPrefix)
281 368 {
282 - $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
369 + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
283 370 }
284 371
285 372 /**
286 373 * The APCu prefix in use, or null if APCu caching is not enabled.
@@ -295,20 +382,39 @@
295 382 /**
296 383 * Registers this instance as an autoloader.
297 384 *
298 385 * @param bool $prepend Whether to prepend the autoloader or not
386 + *
387 + * @return void
299 388 */
300 389 public function register($prepend = false)
301 390 {
302 391 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
392 +
393 + if (null === $this->vendorDir) {
394 + return;
395 + }
396 +
397 + if ($prepend) {
398 + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders;
399 + } else {
400 + unset(self::$registeredLoaders[$this->vendorDir]);
401 + self::$registeredLoaders[$this->vendorDir] = $this;
402 + }
303 403 }
304 404
305 405 /**
306 406 * Unregisters this instance as an autoloader.
407 + *
408 + * @return void
307 409 */
308 410 public function unregister()
309 411 {
310 412 spl_autoload_unregister(array($this, 'loadClass'));
413 +
414 + if (null !== $this->vendorDir) {
415 + unset(self::$registeredLoaders[$this->vendorDir]);
416 + }
311 417 }
312 418
313 419 /**
314 420 * Loads the given class or interface.
@@ -313,9 +419,9 @@
313 419 /**
314 420 * Loads the given class or interface.
315 421 *
316 422 * @param string $class The name of the class
317 - * @return bool|null True if loaded, null otherwise
423 + * @return true|null True if loaded, null otherwise
318 424 */
319 425 public function loadClass($class)
320 426 {
321 427 if ($file = $this->findFile($class)) {
@@ -322,8 +428,10 @@
322 428 includeFile($file);
323 429
324 430 return true;
325 431 }
432 +
433 + return null;
326 434 }
327 435
328 436 /**
329 437 * Finds the path to the file where the class is defined.
@@ -366,8 +474,23 @@
366 474
367 475 return $file;
368 476 }
369 477
478 + /**
479 + * Returns the currently registered loaders indexed by their corresponding vendor directories.
480 + *
481 + * @return self[]
482 + */
483 + public static function getRegisteredLoaders()
484 + {
485 + return self::$registeredLoaders;
486 + }
487 +
488 + /**
489 + * @param string $class
490 + * @param string $ext
491 + * @return string|false
492 + */
370 493 private function findFileWithExtension($class, $ext)
371 494 {
372 495 // PSR-4 lookup
373 496 $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
@@ -376,13 +499,13 @@
376 499 if (isset($this->prefixLengthsPsr4[$first])) {
377 500 $subPath = $class;
378 501 while (false !== $lastPos = strrpos($subPath, '\\')) {
379 502 $subPath = substr($subPath, 0, $lastPos);
380 - $search = $subPath.'\\';
503 + $search = $subPath . '\\';
381 504 if (isset($this->prefixDirsPsr4[$search])) {
505 + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
382 506 foreach ($this->prefixDirsPsr4[$search] as $dir) {
383 - $length = $this->prefixLengthsPsr4[$first][$search];
384 - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
507 + if (file_exists($file = $dir . $pathEnd)) {
385 508 return $file;
386 509 }
387 510 }
388 511 }
@@ -437,8 +560,12 @@
437 560 /**
438 561 * Scope isolated include.
439 562 *
440 563 * Prevents access to $this/self from included files.
564 + *
565 + * @param string $file
566 + * @return void
567 + * @private
441 568 */
442 569 function includeFile($file)
443 570 {
444 571 include $file;