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
+ /** @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-var array<string, string>
153
+ */
84
154
public function getClassMap ()
85
155
{
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
{
@@ -102,9 +175,11 @@ public function addClassMap(array $classMap)
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
{
@@ -147,11 +222,13 @@ public function add($prefix, $paths, $prepend = false)
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
{
@@ -195,8 +272,10 @@ public function addPsr4($prefix, $paths, $prepend = false)
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
{
@@ -211,10 +290,12 @@ public function set($prefix, $paths)
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
{
@@ -234,6 +315,8 @@ public function setPsr4($prefix, $paths)
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
{
@@ -256,6 +339,8 @@ public function getUseIncludePath()
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
{
@@ -276,6 +361,8 @@ public function isClassMapAuthoritative()
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
{
@@ -296,25 +383,44 @@ public function getApcuPrefix()
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.
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
{
@@ -323,6 +429,8 @@ public function loadClass($class)
323
429
324
430
return true ;
325
431
}
432
+
433
+ return null ;
326
434
}
327
435
328
436
/**
@@ -367,6 +475,21 @@ public function findFile($class)
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
@@ -438,6 +561,10 @@ private function findFileWithExtension($class, $ext)
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
{
0 commit comments