3636 * @filesource
3737 */
3838
39- use Config \Autoload ;
40-
4139/**
4240 * Class Loader
4341 *
4846 */
4947class FileLocator
5048{
51-
52- /**
53- * Stores our namespaces
54- *
55- * @var array
56- */
57- protected $ namespaces ;
58-
5949 /**
6050 * @var \CodeIgniter\Autoloader\Autoloader
6151 */
@@ -95,7 +85,8 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
9585 $ file = strpos ($ file , '. ' . $ ext ) !== false ? $ file : $ file . '. ' . $ ext ;
9686
9787 // Clean the folder name from the filename
98- if (! empty ($ folder ))
88+ //if (! empty($folder))
89+ if (! empty ($ folder ) /*&& strpos($file, $folder) === false*/ )
9990 {
10091 $ file = str_replace ($ folder . '/ ' , '' , $ file );
10192 }
@@ -123,14 +114,15 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
123114
124115 while (! empty ($ segments ))
125116 {
126- $ prefix .= empty ($ prefix ) ? ucfirst (array_shift ($ segments )) : '\\' . ucfirst (array_shift ($ segments ));
117+ $ prefix .= empty ($ prefix )
118+ ? ucfirst (array_shift ($ segments ))
119+ : '\\' . ucfirst (array_shift ($ segments ));
127120
128121 if (! array_key_exists ($ prefix , $ this ->autoloader ->getNamespace ()))
129122 {
130123 continue ;
131124 }
132-
133- $ path = $ this ->autoloader ->getNamespace ($ prefix ) . '/ ' ;
125+ $ path = $ this ->getNamespaces ($ prefix );
134126 $ filename = implode ('/ ' , $ segments );
135127 break ;
136128 }
@@ -241,13 +233,13 @@ public function search(string $path, string $ext = 'php'): array
241233
242234 $ foundPaths = [];
243235
244- foreach ($ this ->autoloader -> getNamespace () as $ name => $ folder )
236+ foreach ($ this ->getNamespaces () as $ namespace )
245237 {
246- $ folder = rtrim ($ folder , '/ ' ) . '/ ' ;
238+ $ namespace [ ' path ' ] = rtrim ($ namespace [ ' path ' ] , '/ ' ) . '/ ' ;
247239
248- if (is_file ($ folder . $ path ) === true )
240+ if (is_file ($ namespace [ ' path ' ] . $ path ) === true )
249241 {
250- $ foundPaths [] = $ folder . $ path ;
242+ $ foundPaths [] = $ namespace [ ' path ' ] . $ path ;
251243 }
252244 }
253245
@@ -259,6 +251,33 @@ public function search(string $path, string $ext = 'php'): array
259251
260252 //--------------------------------------------------------------------
261253
254+ protected function getNamespaces (string $ prefix = null )
255+ {
256+ if ($ prefix )
257+ {
258+ $ path = $ this ->autoloader ->getNamespace ($ prefix );
259+
260+ return isset ($ path [0 ]) ? $ path [0 ] : '' ;
261+ }
262+
263+ $ namespaces = [];
264+
265+ foreach ($ this ->autoloader ->getNamespace () as $ prefix => $ paths )
266+ {
267+ foreach ($ paths as $ path )
268+ {
269+ $ namespaces [] = [
270+ 'prefix ' => $ prefix ,
271+ 'path ' => $ path ,
272+ ];
273+ }
274+ }
275+
276+ return $ namespaces ;
277+ }
278+
279+ //--------------------------------------------------------------------
280+
262281 /**
263282 * Attempts to load a file and instantiate a new class by looking
264283 * at its full path and comparing that to our existing psr4 namespaces
@@ -277,18 +296,21 @@ public function findQualifiedNameFromPath(string $path)
277296 return ;
278297 }
279298
280- foreach ($ this ->autoloader -> getNamespace () as $ namespace => $ nsPath )
299+ foreach ($ this ->getNamespaces () as $ namespace )
281300 {
282- $ nsPath = realpath ($ nsPath );
283- if (is_numeric ($ namespace ) || empty ($ nsPath ))
301+ $ namespace ['path ' ] = realpath ($ namespace ['path ' ]);
302+
303+ if (empty ($ namespace ['path ' ]))
284304 {
285305 continue ;
286306 }
287307
288- if (mb_strpos ($ path , $ nsPath ) === 0 )
308+ if (mb_strpos ($ path , $ namespace [ ' path ' ] ) === 0 )
289309 {
290- $ className = '\\' . $ namespace . '\\' .
291- ltrim (str_replace ('/ ' , '\\' , mb_substr ($ path , mb_strlen ($ nsPath ))), '\\' );
310+ $ className = '\\' . $ namespace ['prefix ' ] . '\\' .
311+ ltrim (str_replace ('/ ' , '\\' , mb_substr (
312+ $ path , mb_strlen ($ namespace ['path ' ]))
313+ ), '\\' );
292314 // Remove the file extension (.php)
293315 $ className = mb_substr ($ className , 0 , -4 );
294316
@@ -317,9 +339,9 @@ public function listFiles(string $path): array
317339 $ files = [];
318340 helper ('filesystem ' );
319341
320- foreach ($ this ->autoloader -> getNamespace () as $ namespace => $ nsPath )
342+ foreach ($ this ->getNamespaces () as $ namespace )
321343 {
322- $ fullPath = realpath (rtrim ($ nsPath , '/ ' ) . '/ ' . $ path );
344+ $ fullPath = realpath (rtrim ($ namespace [ ' path ' ] , '/ ' ) . '/ ' . $ path );
323345
324346 if (! is_dir ($ fullPath ))
325347 {
@@ -373,8 +395,7 @@ protected function legacyLocate(string $file, string $folder = null): string
373395 * Checks to see if a file exists on the file system. This is split
374396 * out to it's own method to make testing simpler.
375397 *
376- * @codeCoverageIgnore
377- * @param string $path
398+ * @param string $path
378399 *
379400 * @return boolean
380401 */
0 commit comments