Welcome Guest, Not a member yet? Register   Sign In
Custom CLI Return "Cannot declare class name in use"
#1

(This post was last modified: 03-31-2021, 08:46 AM by simonickalexs.)

I'm trying to test to make my own CLI command on my own library but it throw error cannot declare `someone\plugin\Commands` because the class name is already in use. Here's the source code.

PHP Code:
<?php
namespace someone\plugin\Commands;

use 
CodeIgniter\CLI\BaseCommand;
use 
CodeIgniter\CLI\CLI;

class 
PrintHelloWorld extends BaseCommand
{
    
/**
     * The Command's Group
     *
     * @var string
     */
    
protected $group 'Testing';

    
/**
     * The Command's Name
     *
     * @var string
     */
    
protected $name 'print:dummy';

    
/**
     * The Command's Description
     *
     * @var string
     */
    
protected $description 'Print Hello World';

    
/**
     * The Command's Usage
     *
     * @var string
     */
    
protected $usage 'print:dummy [options]';

    
/**
     * The path to someone\src directory.
     *
     * @var string
     */
    
protected $sourcePath;

    
/**
     * The command's options
     *
     * @var array
     */
    
protected $options = [];

    
/**
     * Actually execute the command.
     *
     * @param array $params
     *
     * @return void
     */
    
public function run(array $params)
    {
        
CLI::write('Hello world!.''green');
    }



Can someone explain why? Thank you.

Edit: I put it on the ThirdParty folder. Full path is on "ThirdParty\trydummy\src".
Reply
#2

(This post was last modified: 03-31-2021, 03:21 PM by [email protected].)

Assuming your app/config/Autoload.php contains this entry:
'Someone\plugins' => ROOTPATH . 'someone/plugins'

Then the namespace in your code says that your file is in:
someone/plugins/Commands folder
I've tested the above and your code works.

I don't understand your Edit: line  "I put it on the ThirdParty folder. Full path is on "ThirdParty\trydummy\src".
Your file should not be in this directory, it has to be in a 'Commands' folder

Try using php spark namespaces to check your namespaces map and can be found.

Once you have it working you could change the namespace mapping:
Eg: In app/config/Autoload.php
CustomSpark => ROOTPATH . 'someone/plugins'
and in your file:
namespace CustomSpark\Commands;

OR something even more sensible !
Reply
#3

(This post was last modified: 03-31-2021, 06:52 PM by simonickalexs.)

(03-31-2021, 02:54 PM)[email protected] Wrote: Assuming your app/config/Autoload.php contains this entry:
'Someone\plugins' => ROOTPATH . 'someone/plugins'

Then the namespace in your code says that your file is in:
someone/plugins/Commands folder
I've tested the above and your code works.

I don't understand your Edit: line  "I put it on the ThirdParty folder. Full path is on "ThirdParty\trydummy\src".
Your file should not be in this directory, it has to be in a 'Commands' folder

Try using php spark namespaces to check your namespaces map and can be found.

Once you have it working you could change the namespace mapping:
Eg: In app/config/Autoload.php
CustomSpark => ROOTPATH . 'someone/plugins'
and in your file:
namespace CustomSpark\Commands;

OR something even more sensible !

Thanks for your answer and sorry for misunderstanding my English is not very good. 

Quote:Assuming your app/config/Autoload.php contains this entry:

'Someone\plugins' => ROOTPATH . 'someone/plugins'

Actually, it "Someone\plugins" => APPPATH.'ThirdParty/trydummy/src' (I'm following this plugin https://github.com/lonnieezell/myth-auth...stallation).


Quote:Then the namespace in your code says that your file is in:
someone/plugins/Commands folder
I've tested the above and your code works.


My directory is looking like this
- app
   + Commands
   + Config
   + Controllers
   + Database
   + Filters
   + .....
   - ThirdParty
      - trydummy
        - src
          - Commands
             ThatCustomCommand.php
          + AnotherFolder
          + AnotherFolder
          somefile.php
   + Views
+ public


Quote:I don't understand your Edit: line  "I put it on the ThirdParty folder. Full path is on "ThirdParty\trydummy\src".

Your file should not be in this directory, it has to be in a 'Commands' folder

It's already in Commands folder like the docs said.



Quote:Once you have it working you could change the namespace mapping:
Eg: In app/config/Autoload.php
CustomSpark => ROOTPATH . 'someone/plugins'
and in your file:
namespace CustomSpark\Commands;


Should I use ROOTPATH instead of APPPATH?

Thank you!
Reply
#4

You wrote:
Should I use ROOTPATH instead of APPPATH?

NO, Use APPPATH if these folders are inside the app/ folder.

Edit your code file (make sure it is named PrintHelloWorld.php and lives in app/ThirdParty/trydummy/src/Commands)
change:
namespace someone\plugin\Commands;
to:
namespace someone\plugins\Commands; (you left the 's' off of plugins - it therefore did not match your Autoload.php entry)

I've tested it and it works.
Reply
#5

I found the problem, previously I add the someone\plugins namespace on composer.json manually, look like this

....
"autoload": {
"psr-4": {
"App\\": "app",
"Config\\": "app/Config",
"someone\\plugin\\": "app/ThirdParty/trydummy/src"
},
"exclude-from-classmap": [
"**/Database/Migrations/**"
]
},
.....

Then I forgot there's a Autoloader.php file, so I add there and delete the one on the composer.json.
Then I run this command `composer dump-autoload`, still occurred.
But I've changed the folder trydummy to try-dummy, and now it worked.
I don't know how to clear that mess that I've made. Thank you very much for your help!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB