How to add new console commands

Discussion in 'Extensions' started by bolty, Jul 12, 2018.

  1. bolty

    bolty New Member

    Joined:
    Jun 27, 2018
    Messages:
    13
    Likes Received:
    4
    S.E:
    2019-06-27 10:31:35
    L.T:
    Regular
    L.C:
    1
    Hello
    I'm building an extension to migrate my customer data from my existing EMA (interspire) into MailWizz. I have too many customers to do this manually.

    I need to run my import task via cron and I'm wondering if my extension can add a command to console.php? If so how is this done?
     
  2. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    9,931
    Likes Received:
    2,325
    @bolty - it's totally possible to register commands from command line.
    In the run() method of the extension, you could do this:
    PHP:
    Yii::app()->commandMap['your-command-name'] = [
        
    'class'=>'ext-YOUR_EXT_NAME.console..commands.MySuperCommand',
    ];
    ( https://www.yiiframework.com/doc/api/1.1/CConsoleApplication#commandMap-detail )

    MySuperCommand must be a class, an instance of ConsoleCommand command, see:
    https://www.yiiframework.com/doc/guide/1.1/en/topics.console

    So the file will look like:
    PHP:
    <?php defined('MW_PATH') || exit('No direct script access allowed');

    class 
    MySuperCommand extends ConsoleCommand 
    {
        public function 
    actionIndex() 
        {
            echo 
    'Hello World!' "\n";
        }
    }
    Next, in the cron jobs, you can add a command like:
    Code:
    * * * * * php /absolute/path/to/apps/console/console.php your-command-name >/dev/null 2 >&1
    
     
  3. bolty

    bolty New Member

    Joined:
    Jun 27, 2018
    Messages:
    13
    Likes Received:
    4
    S.E:
    2019-06-27 10:31:35
    L.T:
    Regular
    L.C:
    1
    Thanks so much for the detailed answer! I’m new to Yii so I have a lot to learn
     
  4. bolty

    bolty New Member

    Joined:
    Jun 27, 2018
    Messages:
    13
    Likes Received:
    4
    S.E:
    2019-06-27 10:31:35
    L.T:
    Regular
    L.C:
    1
    Just updating this thread incase anyone else wants to create a console command from an extension.
    The solution was actually to add this in the extensions run() method:

    PHP:
    if ($this->isAppName('console')) {
                
    Yii::app()->getCommandRunner()->commands['your-command-name'] = [
                    
    'class'        =>'ext-YOUR_EXT_NAME.console.commands.MySuperCommand',
                ];
    }
    The reason you have to use Yii::app()->getCommandRunner()->commands not Yii::app()->commandMap is because by the time the run method of your extension is called CConsoleApplication has already passed the commandMap to CConsoleCommandRunner so it's too late to change the commandMap there

    Cheers
     
  5. twisted1919

    twisted1919 Administrator Staff Member

    Joined:
    Dec 27, 2014
    Messages:
    9,931
    Likes Received:
    2,325
    @bolty - Nice, thanks for the update ;)
     

Share This Page