Coding Tutorial v7 : custom console command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    Coding Tutorial v7 : custom console command

    Replace {myModule} with a name of your module (too in php files ..)

    create file in :
    custom/Espo/Modules/{myModule}/Console/Commands/Whoami.php

    PHP Code:
    <?php
    namespace Espo\Modules\{myModule}\Console\Commands;
    
    use Espo\Core\Exceptions\Error;
    use Espo\Core\Console\IO;
    
    class Whoami extends \Espo\Core\Console\Commands\Base
    {
    protected $io;
    
    public function __construct(IO $io)
    {
    $this->io = $io;
    }
    
    public function run(array $options, array $flagList, array $argumentList) : void
    {
    $name = $argumentList[0] ?? null;
    if ($name){
    $this->io->writeLine('Your name is : ' .$name);
    }else{
    $this->io->writeLine("Please give your name.");
    $name = $this->io->readLine();
    }
    
    if (!$name) {
    throw new Error('Invalid argument. Please give your name');
    }
    $this->io->writeLine('Hello ' .$name);
    $this->io->writeLine("1) Your name is {$name} right?");
    $this->io->writeLine("2) What is my name?" );
    $this->io->writeLine("Select 1 or 2 or return for cancel.");
    
    $response = $this->io->readLine();
    switch ($response)
    {
    case 1 :
    $this->io->writeLine("Yes.. no complicate !");
    break;
    case 2 :
    $whoAmi = @exec('whoami');
    $this->io->writeLine($whoAmi);
    break;
    default:
    $this->io->writeLine("Canceled.");
    return;
    }
    $this->io->writeLine("Done.");
    }
    }
    create file in :
    custom/Espo/Modules/{myModule}/Resources/metadata/app/consoleCommands.js

    PHP Code:
    {
    "whoami": {
    "className": "\\Espo\\Modules\\{myModule}\\Console\\Commands\\Whoami"
    }
    
    } 
    
    clearCache

    php bin/command whoami
    php bin/command whoami espoCRM

    This is just for learning
    Last edited by item; 11-08-2021, 09:22 PM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • telecastg
    Active Community Member
    • Jun 2018
    • 907

    #2
    Excellent, thanks item !

    Comment

    • Kritika
      Junior Member
      • May 2022
      • 14

      #3
      Help pleas (((

      Error: Command 'unique-numeric' does not exist.

      Comment

      • emillod
        Active Community Member
        • Apr 2017
        • 1405

        #4
        item nice one!
        Maybe i'll record a video with this

        Comment

        Working...