diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index f9734828b692..cfe12b79c2fa 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -289,6 +289,8 @@ public function findQualifiedNameFromPath(string $path) /** * Scans the defined namespaces, returning a list of all files * that are contained within the subpath specified by $path. + * + * @return string[] List of file paths */ public function listFiles(string $path): array { @@ -309,6 +311,9 @@ public function listFiles(string $path): array $tempFiles = get_filenames($fullPath, true); + // Remove directories + $tempFiles = array_filter($tempFiles, static fn ($path) => strtolower(substr($path, -4)) === '.php'); + if (! empty($tempFiles)) { $files = array_merge($files, $tempFiles); } @@ -320,6 +325,8 @@ public function listFiles(string $path): array /** * Scans the provided namespace, returning a list of all files * that are contained within the sub path specified by $path. + * + * @return string[] List of file paths */ public function listNamespaceFiles(string $prefix, string $path): array { diff --git a/system/CLI/Commands.php b/system/CLI/Commands.php index d153b3aa857a..2714db30bcf9 100644 --- a/system/CLI/Commands.php +++ b/system/CLI/Commands.php @@ -76,10 +76,6 @@ public function getCommands() /** * Discovers all commands in the framework and within user code, * and collects instances of them to work with. - * - * @TODO this approach (find qualified classname from path) causes error, - * when using Composer autoloader. - * See https://github.com/codeigniter4/CodeIgniter4/issues/5818 */ public function discoverCommands() { @@ -100,9 +96,9 @@ public function discoverCommands() // Loop over each file checking to see if a command with that // alias exists in the class. foreach ($files as $file) { - $className = $locator->findQualifiedNameFromPath($file); + $className = $locator->getClassname($file); - if (empty($className) || ! class_exists($className)) { + if ($className === '' || ! class_exists($className)) { continue; } diff --git a/tests/system/Autoloader/FileLocatorTest.php b/tests/system/Autoloader/FileLocatorTest.php index 54a471d74453..3b86cfb7e535 100644 --- a/tests/system/Autoloader/FileLocatorTest.php +++ b/tests/system/Autoloader/FileLocatorTest.php @@ -211,6 +211,18 @@ public function testListFilesSimple() $this->assertTrue(in_array($expectedWin, $files, true) || in_array($expectedLin, $files, true)); } + public function testListFilesDoesNotContainDirectories() + { + $files = $this->locator->listFiles('Config/'); + + $directory = str_replace( + '/', + DIRECTORY_SEPARATOR, + APPPATH . 'Config/Boot' + ); + $this->assertNotContains($directory, $files); + } + public function testListFilesWithFileAsInput() { $files = $this->locator->listFiles('Config/App.php');