Skip to content

fix: Commands::discoverCommands() loads incorrect classname #5849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
Expand All @@ -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
{
Expand Down
8 changes: 2 additions & 6 deletions system/CLI/Commands.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/system/Autoloader/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down