Skip to content

Commit 5e65c4c

Browse files
authored
Merge pull request #5849 from kenjis/fix-discoverCommands
fix: Commands::discoverCommands() loads incorrect classname
2 parents 27e7de2 + 7aba08a commit 5e65c4c

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ public function findQualifiedNameFromPath(string $path)
289289
/**
290290
* Scans the defined namespaces, returning a list of all files
291291
* that are contained within the subpath specified by $path.
292+
*
293+
* @return string[] List of file paths
292294
*/
293295
public function listFiles(string $path): array
294296
{
@@ -309,6 +311,9 @@ public function listFiles(string $path): array
309311

310312
$tempFiles = get_filenames($fullPath, true);
311313

314+
// Remove directories
315+
$tempFiles = array_filter($tempFiles, static fn ($path) => strtolower(substr($path, -4)) === '.php');
316+
312317
if (! empty($tempFiles)) {
313318
$files = array_merge($files, $tempFiles);
314319
}
@@ -320,6 +325,8 @@ public function listFiles(string $path): array
320325
/**
321326
* Scans the provided namespace, returning a list of all files
322327
* that are contained within the sub path specified by $path.
328+
*
329+
* @return string[] List of file paths
323330
*/
324331
public function listNamespaceFiles(string $prefix, string $path): array
325332
{

system/CLI/Commands.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ public function getCommands()
7676
/**
7777
* Discovers all commands in the framework and within user code,
7878
* and collects instances of them to work with.
79-
*
80-
* @TODO this approach (find qualified classname from path) causes error,
81-
* when using Composer autoloader.
82-
* See https://github.com/codeigniter4/CodeIgniter4/issues/5818
8379
*/
8480
public function discoverCommands()
8581
{
@@ -100,9 +96,9 @@ public function discoverCommands()
10096
// Loop over each file checking to see if a command with that
10197
// alias exists in the class.
10298
foreach ($files as $file) {
103-
$className = $locator->findQualifiedNameFromPath($file);
99+
$className = $locator->getClassname($file);
104100

105-
if (empty($className) || ! class_exists($className)) {
101+
if ($className === '' || ! class_exists($className)) {
106102
continue;
107103
}
108104

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ public function testListFilesSimple()
211211
$this->assertTrue(in_array($expectedWin, $files, true) || in_array($expectedLin, $files, true));
212212
}
213213

214+
public function testListFilesDoesNotContainDirectories()
215+
{
216+
$files = $this->locator->listFiles('Config/');
217+
218+
$directory = str_replace(
219+
'/',
220+
DIRECTORY_SEPARATOR,
221+
APPPATH . 'Config/Boot'
222+
);
223+
$this->assertNotContains($directory, $files);
224+
}
225+
214226
public function testListFilesWithFileAsInput()
215227
{
216228
$files = $this->locator->listFiles('Config/App.php');

0 commit comments

Comments
 (0)