Skip to content

Commit 0bdef6b

Browse files
committed
apply 3rd parameter $prioritizeApp = true to FileLocator::search()
1 parent caf0fce commit 0bdef6b

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,28 +226,43 @@ public function getClassname(string $file) : string
226226
* 'app/Modules/bar/Config/Routes.php',
227227
* ]
228228
*
229-
* @param string $path
230-
* @param string $ext
229+
* @param string $path
230+
* @param string $ext
231+
* @param boolean $prioritizeApp
231232
*
232233
* @return array
233234
*/
234-
public function search(string $path, string $ext = 'php'): array
235+
public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array
235236
{
236237
$path = $this->ensureExt($path, $ext);
237238

238239
$foundPaths = [];
240+
$appPaths = [];
239241

240242
foreach ($this->getNamespaces() as $namespace)
241243
{
242244
if (isset($namespace['path']) && is_file($namespace['path'] . $path))
243245
{
244-
$foundPaths[] = $namespace['path'] . $path;
246+
$fullPath = $namespace['path'] . $path;
247+
if ($prioritizeApp || strpos($fullPath, APPPATH) !== 0)
248+
{
249+
$foundPaths[] = $fullPath;
250+
}
251+
else
252+
{
253+
$appPaths[] = $fullPath;
254+
}
245255
}
246256
}
247257

248258
// Remove any duplicates
249259
$foundPaths = array_unique($foundPaths);
250260

261+
if (! $prioritizeApp && ! empty($appPaths))
262+
{
263+
$foundPaths = array_merge($foundPaths, array_unique($appPaths));
264+
}
265+
251266
return $foundPaths;
252267
}
253268

system/Language/Language.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,7 @@ protected function load(string $file, string $locale, bool $return = false)
338338
*/
339339
protected function requireFile(string $path): array
340340
{
341-
$files = Services::locator()->search($path);
342-
$files = array_merge(
343-
array_filter($files, function ($value) {
344-
return strpos($value, APPPATH) === false;
345-
}),
346-
array_filter($files, function ($value) {
347-
return strpos($value, APPPATH) === 0;
348-
})
349-
);
350-
341+
$files = Services::locator()->search($path, 'php', false);
351342
$strings = [];
352343

353344
foreach ($files as $file)

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,22 @@ public function testSearchForFileNotExist()
191191
$this->assertArrayNotHasKey(0, $foundFiles);
192192
}
193193

194+
//--------------------------------------------------------------------
195+
196+
public function testSearchPrioritizeSystemOverApp()
197+
{
198+
$foundFiles = $this->locator->search('Language/en/Validation.php', 'php', false);
199+
200+
$this->assertEquals([
201+
SYSTEMPATH . 'Language/en/Validation.php',
202+
APPPATH . 'Language/en/Validation.php',
203+
],
204+
$foundFiles
205+
);
206+
}
207+
208+
//--------------------------------------------------------------------
209+
194210
public function testListNamespaceFilesEmptyPrefixAndPath()
195211
{
196212
$this->assertEmpty($this->locator->listNamespaceFiles('', ''));

0 commit comments

Comments
 (0)