Skip to content
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

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

Merged
merged 3 commits into from
Apr 4, 2022

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Apr 2, 2022

Description
Fixes #5818
Supersedes #5834

  • fix: FileLocator::listFiles() returns directories
  • fix: discoverCommands() loads incorrect classname

Cannot write a unit test for Cannot declare class ..., because the name is already in use.
To check manually using this repo:

  1. Add "App\\": "app/" in the autoload.psr4 section in the root composer.json
  2. Run composer dump -o to regenerate the class map.
  3. Add 'MyNameSpace' => APPPATH . 'ThirdParty/MyFolder' in Config\Autoload::$psr4 array.
  4. Run php spark make:command TeseCommand --namespace MyNameSpace
  5. Run php spark. This will now error.
  6. Check out this PR branch (fix-discoverCommands).
  7. Run php spark. No errors should appear.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Apr 2, 2022
@paulbalandan
Copy link
Member

This PR basically does two parts:

  1. Removes the directories generated by get_filenames(); and
  2. Uses $locator->getClassName() instead of $locator->findQualifiedNameFromPath().

In 2, I understand the change will be more "intelligent" as it no longer guesses the class name from the path but rather checks the file itself to get the class name. This is +1 for me.

For 1, I don't understand why the need to filter out the directories? Maybe because the method is listFiles(). Well, that makes sense then.

@kenjis
Copy link
Member Author

kenjis commented Apr 2, 2022

@paulbalandan

For 1, I don't understand why the need to filter out the directories? Maybe because the method is listFiles(). Well, that makes sense then.

Very simple. If it contains directories, spark does not work.

$ php spark

CodeIgniter v4.1.9 Command Line Tool - Server Time: 2022-04-02 18:32:08 UTC-05:00


[ErrorException]

file_get_contents(): Read of 8192 bytes failed with errno=21 Is a directory

at SYSTEMPATH/Autoloader/FileLocator.php:115

Backtrace:
  1    [internal function]
       CodeIgniter\Debug\Exceptions()->errorHandler(8, 'file_get_contents(): Read of 8192 bytes failed with errno=21 Is a directory', '/Users/kenji/work/codeigniter/CodeIgniter4/system/Autoloader/FileLocator.php', 115)

  2    SYSTEMPATH/Autoloader/FileLocator.php:115
       file_get_contents('/Users/kenji/work/codeigniter/CodeIgniter4/system/Commands/Cache')

  3    SYSTEMPATH/CLI/Commands.php:99
       CodeIgniter\Autoloader\FileLocator()->getClassname('/Users/kenji/work/codeigniter/CodeIgniter4/system/Commands/Cache')

  4    SYSTEMPATH/CLI/Commands.php:46
       CodeIgniter\CLI\Commands()->discoverCommands()

  5    SYSTEMPATH/Config/Services.php:159
       CodeIgniter\CLI\Commands()->__construct()

  6    SYSTEMPATH/Config/BaseService.php:251
       CodeIgniter\Config\Services::commands(false)

  7    SYSTEMPATH/Config/BaseService.php:192
       CodeIgniter\Config\BaseService::__callStatic('commands', [...])

  8    SYSTEMPATH/Config/Services.php:156
       CodeIgniter\Config\BaseService::getSharedInstance('commands')

  9    SYSTEMPATH/Config/BaseService.php:251
       CodeIgniter\Config\Services::commands()

 10    SYSTEMPATH/CLI/CommandRunner.php:35
       CodeIgniter\Config\BaseService::__callStatic('commands', [])

 11    SYSTEMPATH/CodeIgniter.php:857
       CodeIgniter\CLI\CommandRunner()->__construct()

 12    SYSTEMPATH/CodeIgniter.php:458
       CodeIgniter\CodeIgniter()->createController()

 13    SYSTEMPATH/CodeIgniter.php:346
       CodeIgniter\CodeIgniter()->handleRequest(null, Object(Config\Cache), false)

 14    SYSTEMPATH/CLI/Console.php:48
       CodeIgniter\CodeIgniter()->run()

 15    ROOTPATH/spark:79
       CodeIgniter\CLI\Console()->run()

@paulbalandan
Copy link
Member

Very simple. If it contains directories, spark does not work.

Ah, yes! I missed the file_get_contents.

@kenjis kenjis merged commit 5e65c4c into codeigniter4:develop Apr 4, 2022
@kenjis kenjis deleted the fix-discoverCommands branch April 4, 2022 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: defining new namespace under app/ causes Cannot declare class ..., because the name is already in use
2 participants