41
41
* 'Swift_' => __DIR__.'/Swift',
42
42
* ));
43
43
*
44
+ *
45
+ * // to enable searching the include path (eg. for PEAR packages)
46
+ * $loader->useIncludePath(true);
47
+ *
44
48
* // activate the autoloader
45
49
* $loader->register();
46
50
*
@@ -60,6 +64,29 @@ class UniversalClassLoader
60
64
private $ prefixes = array ();
61
65
private $ namespaceFallbacks = array ();
62
66
private $ prefixFallbacks = array ();
67
+ private $ useIncludePath = false ;
68
+
69
+ /**
70
+ * Turns on searching the include for class files. Allows easy loading
71
+ * of installed PEAR packages
72
+ *
73
+ * @param Boolean $useIncludePath
74
+ */
75
+ public function useIncludePath ($ useIncludePath )
76
+ {
77
+ $ this ->useIncludePath = $ useIncludePath ;
78
+ }
79
+
80
+ /**
81
+ * Can be used to check if the autoloader uses the include path to check
82
+ * for classes.
83
+ *
84
+ * @return Boolean
85
+ */
86
+ public function getUseIncludePath ()
87
+ {
88
+ return $ this ->useIncludePath ;
89
+ }
63
90
64
91
/**
65
92
* Gets the configured namespaces.
@@ -219,47 +246,54 @@ public function findFile($class)
219
246
if (false !== $ pos = strrpos ($ class , '\\' )) {
220
247
// namespaced class name
221
248
$ namespace = substr ($ class , 0 , $ pos );
249
+ $ className = substr ($ class , $ pos + 1 );
250
+ $ normalizedClass = str_replace ('\\' , DIRECTORY_SEPARATOR , $ namespace ).DIRECTORY_SEPARATOR .str_replace ('_ ' , DIRECTORY_SEPARATOR , $ className ).'.php ' ;
222
251
foreach ($ this ->namespaces as $ ns => $ dirs ) {
223
252
if (0 !== strpos ($ namespace , $ ns )) {
224
253
continue ;
225
254
}
226
255
227
256
foreach ($ dirs as $ dir ) {
228
- $ className = substr ($ class , $ pos + 1 );
229
- $ file = $ dir .DIRECTORY_SEPARATOR .str_replace ('\\' , DIRECTORY_SEPARATOR , $ namespace ).DIRECTORY_SEPARATOR .str_replace ('_ ' , DIRECTORY_SEPARATOR , $ className ).'.php ' ;
257
+ $ file = $ dir .DIRECTORY_SEPARATOR .$ normalizedClass ;
230
258
if (file_exists ($ file )) {
231
259
return $ file ;
232
260
}
233
261
}
234
262
}
235
263
236
264
foreach ($ this ->namespaceFallbacks as $ dir ) {
237
- $ file = $ dir .DIRECTORY_SEPARATOR .str_replace ( '\\' , DIRECTORY_SEPARATOR , $ class ). ' .php ' ;
265
+ $ file = $ dir .DIRECTORY_SEPARATOR .$ normalizedClass ;
238
266
if (file_exists ($ file )) {
239
267
return $ file ;
240
268
}
241
269
}
270
+
242
271
} else {
243
272
// PEAR-like class name
273
+ $ normalizedClass = str_replace ('_ ' , DIRECTORY_SEPARATOR , $ class ).'.php ' ;
244
274
foreach ($ this ->prefixes as $ prefix => $ dirs ) {
245
275
if (0 !== strpos ($ class , $ prefix )) {
246
276
continue ;
247
277
}
248
278
249
279
foreach ($ dirs as $ dir ) {
250
- $ file = $ dir .DIRECTORY_SEPARATOR .str_replace ( ' _ ' , DIRECTORY_SEPARATOR , $ class ). ' .php ' ;
280
+ $ file = $ dir .DIRECTORY_SEPARATOR .$ normalizedClass ;
251
281
if (file_exists ($ file )) {
252
282
return $ file ;
253
283
}
254
284
}
255
285
}
256
286
257
287
foreach ($ this ->prefixFallbacks as $ dir ) {
258
- $ file = $ dir .DIRECTORY_SEPARATOR .str_replace ( ' _ ' , DIRECTORY_SEPARATOR , $ class ). ' .php ' ;
288
+ $ file = $ dir .DIRECTORY_SEPARATOR .$ normalizedClass ;
259
289
if (file_exists ($ file )) {
260
290
return $ file ;
261
291
}
262
292
}
263
293
}
294
+
295
+ if ($ this ->useIncludePath && $ file = stream_resolve_include_path ($ normalizedClass )) {
296
+ return $ file ;
297
+ }
264
298
}
265
299
}
0 commit comments