37
37
*
38
38
* @author Fabien Potencier <fabien@symfony.com>
39
39
* @author Jordi Boggiano <j.boggiano@seld.be>
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
+ private $ vendorDir ;
46
+
45
47
// PSR-4
46
48
private $ prefixLengthsPsr4 = array ();
47
49
private $ prefixDirsPsr4 = array ();
@@ -57,10 +59,17 @@ class ClassLoader
57
59
private $ missingClasses = array ();
58
60
private $ apcuPrefix ;
59
61
62
+ private static $ registeredLoaders = array ();
63
+
64
+ public function __construct ($ vendorDir = null )
65
+ {
66
+ $ this ->vendorDir = $ vendorDir ;
67
+ }
68
+
60
69
public function getPrefixes ()
61
70
{
62
71
if (!empty ($ this ->prefixesPsr0 )) {
63
- return call_user_func_array ('array_merge ' , $ this ->prefixesPsr0 );
72
+ return call_user_func_array ('array_merge ' , array_values ( $ this ->prefixesPsr0 ) );
64
73
}
65
74
66
75
return array ();
@@ -300,6 +309,17 @@ public function getApcuPrefix()
300
309
public function register ($ prepend = false )
301
310
{
302
311
spl_autoload_register (array ($ this , 'loadClass ' ), true , $ prepend );
312
+
313
+ if (null === $ this ->vendorDir ) {
314
+ return ;
315
+ }
316
+
317
+ if ($ prepend ) {
318
+ self ::$ registeredLoaders = array ($ this ->vendorDir => $ this ) + self ::$ registeredLoaders ;
319
+ } else {
320
+ unset(self ::$ registeredLoaders [$ this ->vendorDir ]);
321
+ self ::$ registeredLoaders [$ this ->vendorDir ] = $ this ;
322
+ }
303
323
}
304
324
305
325
/**
@@ -308,13 +328,17 @@ public function register($prepend = false)
308
328
public function unregister ()
309
329
{
310
330
spl_autoload_unregister (array ($ this , 'loadClass ' ));
331
+
332
+ if (null !== $ this ->vendorDir ) {
333
+ unset(self ::$ registeredLoaders [$ this ->vendorDir ]);
334
+ }
311
335
}
312
336
313
337
/**
314
338
* Loads the given class or interface.
315
339
*
316
340
* @param string $class The name of the class
317
- * @return bool |null True if loaded, null otherwise
341
+ * @return true |null True if loaded, null otherwise
318
342
*/
319
343
public function loadClass ($ class )
320
344
{
@@ -323,6 +347,8 @@ public function loadClass($class)
323
347
324
348
return true ;
325
349
}
350
+
351
+ return null ;
326
352
}
327
353
328
354
/**
@@ -367,6 +393,16 @@ public function findFile($class)
367
393
return $ file ;
368
394
}
369
395
396
+ /**
397
+ * Returns the currently registered loaders indexed by their corresponding vendor directories.
398
+ *
399
+ * @return self[]
400
+ */
401
+ public static function getRegisteredLoaders ()
402
+ {
403
+ return self ::$ registeredLoaders ;
404
+ }
405
+
370
406
private function findFileWithExtension ($ class , $ ext )
371
407
{
372
408
// PSR-4 lookup
0 commit comments