Skip to content

Commit 624db39

Browse files
authored
Merge pull request #2059 from MGatner/update-getnamespaces
Add multi-path support to `locateFile()`
2 parents 2f65567 + daf3d58 commit 624db39

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
106106
unset($segments[0]);
107107
}
108108

109-
$path = '';
109+
$paths = [];
110110
$prefix = '';
111111
$filename = '';
112112

@@ -115,31 +115,43 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
115115

116116
while (! empty($segments))
117117
{
118-
$prefix .= empty($prefix)
119-
? ucfirst(array_shift($segments))
120-
: '\\' . ucfirst(array_shift($segments));
118+
$prefix .= empty($prefix) ? array_shift($segments) : '\\' . array_shift($segments);
121119

122120
if (empty($namespaces[$prefix]))
123121
{
124122
continue;
125123
}
126-
$path = $this->getNamespaces($prefix);
124+
$paths = $namespaces[$prefix];
127125

128126
$filename = implode('/', $segments);
129127
break;
130128
}
131-
132-
// IF we have a folder name, then the calling function
133-
// expects this file to be within that folder, like 'Views',
134-
// or 'libraries'.
135-
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false)
129+
130+
// if no namespaces matched then quit
131+
if (empty($paths))
136132
{
137-
$filename = $folder . '/' . $filename;
133+
return false;
138134
}
135+
136+
// Check each path in the namespace
137+
foreach ($paths as $path)
138+
{
139+
// If we have a folder name, then the calling function
140+
// expects this file to be within that folder, like 'Views',
141+
// or 'libraries'.
142+
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false)
143+
{
144+
$path .= $folder;
145+
}
139146

140-
$path .= $filename;
141-
142-
return is_file($path) ? $path : false;
147+
$path = rtrim($path, '/') . '/' . $filename;
148+
if (is_file($path))
149+
{
150+
return $path;
151+
}
152+
}
153+
154+
return false;
143155
}
144156

145157
//--------------------------------------------------------------------
@@ -265,21 +277,10 @@ protected function ensureExt(string $path, string $ext): string
265277
/**
266278
* Return the namespace mappings we know about.
267279
*
268-
* @param string|null $prefix
269-
*
270280
* @return array|string
271281
*/
272-
protected function getNamespaces(string $prefix = null)
282+
protected function getNamespaces()
273283
{
274-
if ($prefix)
275-
{
276-
$path = $this->autoloader->getNamespace($prefix);
277-
278-
return isset($path[0])
279-
? rtrim($path[0], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR
280-
: '';
281-
}
282-
283284
$namespaces = [];
284285

285286
foreach ($this->autoloader->getNamespace() as $prefix => $paths)

0 commit comments

Comments
 (0)