3737 *
3838 * @author Fabien Potencier <[email protected] > 3939 * @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/
4242 */
4343class ClassLoader
4444{
45+ private $ vendorDir ;
46+
4547 // PSR-4
4648 private $ prefixLengthsPsr4 = array ();
4749 private $ prefixDirsPsr4 = array ();
@@ -57,10 +59,17 @@ class ClassLoader
5759 private $ missingClasses = array ();
5860 private $ apcuPrefix ;
5961
62+ private static $ registeredLoaders = array ();
63+
64+ public function __construct ($ vendorDir = null )
65+ {
66+ $ this ->vendorDir = $ vendorDir ;
67+ }
68+
6069 public function getPrefixes ()
6170 {
6271 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 ) );
6473 }
6574
6675 return array ();
@@ -300,6 +309,17 @@ public function getApcuPrefix()
300309 public function register ($ prepend = false )
301310 {
302311 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+ }
303323 }
304324
305325 /**
@@ -308,13 +328,17 @@ public function register($prepend = false)
308328 public function unregister ()
309329 {
310330 spl_autoload_unregister (array ($ this , 'loadClass ' ));
331+
332+ if (null !== $ this ->vendorDir ) {
333+ unset(self ::$ registeredLoaders [$ this ->vendorDir ]);
334+ }
311335 }
312336
313337 /**
314338 * Loads the given class or interface.
315339 *
316340 * @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
318342 */
319343 public function loadClass ($ class )
320344 {
@@ -323,6 +347,8 @@ public function loadClass($class)
323347
324348 return true ;
325349 }
350+
351+ return null ;
326352 }
327353
328354 /**
@@ -367,6 +393,16 @@ public function findFile($class)
367393 return $ file ;
368394 }
369395
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+
370406 private function findFileWithExtension ($ class , $ ext )
371407 {
372408 // PSR-4 lookup
0 commit comments