Skip to content

Commit 3a1afc5

Browse files
committed
fix: FileLocator::locateFile() bug with a similar namespace name
1 parent 6b06020 commit 3a1afc5

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
7272

7373
foreach (array_keys($namespaces) as $namespace) {
7474
if (substr($file, 0, strlen($namespace)) === $namespace) {
75+
$fileWithoutNamespace = substr($file, strlen($namespace));
76+
// Check if this is really another sub-namespace,
77+
// or just a namespace with a very similar name.
78+
if ($fileWithoutNamespace[0] !== '\\') {
79+
continue;
80+
}
7581
// There may be sub-namespaces of the same vendor,
7682
// so overwrite them with namespaces found later.
77-
$paths = $namespaces[$namespace];
78-
79-
$fileWithoutNamespace = substr($file, strlen($namespace));
80-
$filename = ltrim(str_replace('\\', '/', $fileWithoutNamespace), '/');
83+
$paths = $namespaces[$namespace];
84+
$filename = ltrim(str_replace('\\', '/', $fileWithoutNamespace), '/');
8185
}
8286
}
8387

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ protected function setUp(): void
4444
'CodeIgniter\\Devkit' => [
4545
TESTPATH . '_support/',
4646
],
47+
'Acme\SampleProject' => TESTPATH . '_support',
48+
'Acme\Sample' => TESTPATH . '_support/does/not/exists',
4749
]);
4850

4951
$this->locator = new FileLocator($autoloader);
@@ -151,6 +153,15 @@ public function testLocateFileNotFoundWithBadNamespace()
151153
$this->assertFalse($this->locator->locateFile($file, 'Views'));
152154
}
153155

156+
public function testLocateFileWithProperNamespace()
157+
{
158+
$file = 'Acme\SampleProject\View\Views\simple';
159+
160+
$expected = ROOTPATH . 'tests/_support/View/Views/simple.php';
161+
162+
$this->assertSame($expected, $this->locator->locateFile($file, 'Views'));
163+
}
164+
154165
public function testSearchSimple()
155166
{
156167
$expected = APPPATH . 'Config/App.php';

0 commit comments

Comments
 (0)