Skip to content

Commit

Permalink
Merge pull request #5849 from kenjis/fix-discoverCommands
Browse files Browse the repository at this point in the history
fix: Commands::discoverCommands() loads incorrect classname
  • Loading branch information
kenjis authored Apr 4, 2022
2 parents 27e7de2 + 7aba08a commit 5e65c4c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
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

0 comments on commit 5e65c4c

Please sign in to comment.