Documentation
Creating Console Commands
Introduction
Craft is the name of the console interface in cygnite. It makes your job easy creating custom commands. It also provides you various inbuilt commands for developing an application. Craft driven by powerful Symfony console component. Typically your commands are stored in the src/Apps/Console/Commands/ folder, however you are free to change the default path.
Listing Available Commands
You may wish to view all available commands in your application. To display list of available Craft commands, you may use list command.
php craft list
You may also wish to see usage of any particular command, such cases you can simply pass help parameter before the command name as below.
php craft help app:greet
In the above example app:greet is the command name. It will display the usage of app:greet command.
Creating A Command
You can create your own custom commands and register them to the application. To register you can simply specify the command class name in the array stack in src\Apps\Console\BootStrap.php file. Each command class must extends \Cygnite\Console\Command\Command class, the class should have a method called process/handle where you can write your logic. The command class should have name, description, and arguments properties which should be filled. And the configure method where you can specify the help message for your command.
Let us take a look at an example command.
namespace Apps\Console\Commands;
use Cygnite\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
class GreetCommand extends Command
{
/**
* Specify your command name
*
* @var string
*/
protected $name = 'app:greet';
/**
* Describe command
*
* @var string
*/
protected $description = 'My First Awesome Console Command!';
/**
* Console command arguments
*
* @var array
*/
protected $arguments = [
['name', null, InputArgument::OPTIONAL, null],
];
/**
* Set help message for the command, this is optional method
*
* {@inheritdoc}
*/
protected function configure()
{
$this->setHelp("<<<EOT
The <info>greet</info> command is application console command
<info>cygnite app:greet</info>
EOT>>>"
);
}
/**
* Execute the command
*
* @return mixed|void
*/
public function process()
{
$name = $this->argument('name');
$this->info("Hello $name!!".PHP_EOL);
}
}
Each commands are resolved via Ioc container, meaning that its automatically inject all dependencies type-hinted in the constructor. For example below:
public function __construct(\Apps\Models\User $user)
{
parent::__construct();
show($user); // Will display the object of User model
}
Note: If you define your own constructor in your command class, you MUST call parent::construct() from within it.
Registering Your Command
You must register the newly created command in order to access via console. You need to specify the command namespace in the src/Apps/Console/BootStrap.php file, in the commands property as below.
namespace Apps\Console;
use Cygnite\Console\CraftApplication;
/**
* Class Bootstrap
*
* @package Apps\Console
*/
class Bootstrap extends CraftApplication
{
/**
* Add craft console commands in the array stack
*
* @var array
*/
protected $commands = [
'Apps\Console\Commands\GreetCommand',
];
public function __construct($version)
{
parent::__construct($version);
}
/**
* We will register all console commands here
*
* @return array
*/
public function setCommands()
{
parent::register($this->commands);
}
/**
* Run registered commands
*
* @return $this|void
*/
public function run()
{
$this->setCommands();
parent::run();
return $this;
}
}
Running Your Command
To call this command, run from console/terminal:
cd var/www/cygnite/console/bin
php craft app:greet Sanjoy
It will display the output:
Hello Sanjoy!!
Arguments
While working with console commands it is common that getting input from the user. Arguments can be required, value none, optional, and/or arrays. Argument type expectation must be set in arguments property.
Required argument:
/**
* Console command arguments
*
*
* @var array
*/
protected $arguments = [
['name', null, InputArgument::REQUIRED, 'Who do you want to greet?'],
];
// php craft app:greet sanjoy
Optional argument:
/**
* Console command arguments
*
* @var array
*/
protected $arguments = [
['last_name', null, InputArgument::OPTIONAL, 'Your last name?'],
];
// php craft app:greet sanjoy
// php craft app:greet sanjoy dey
Array argument:
/**
* Console command arguments
*
* @var array
*/
protected $arguments = [
['names', null, InputArgument::IS_ARRAY, 'Separate multiple names with a comma.'],
];
// php craft app:greet sanjoy,john,fabien
Retrieving Input Argument
You can access console arguments and options of your command.
$name = $this->argument('name'); // Retrieve name argument
$names = $this->argument(); // Retrieve all arguments as array
Similar as argument method, you may call option method to get all the options parameters as array:
$option = $this->option('color'); // Retrieve option argument
$options = $this->option(); // Retrieve all options as array
Calling Console Helpers
$formatter = $this->helper('formatter');
Read more: Symfony Console Helpers
Asking For Input
$name = $this->ask('What is your name?');
Asking For Confirmation
if ($this->confirm('Do you wish to continue? [y|n]')) {
//
}
Writing Output
You may use different methods to send output to the console screen, such as info, write, comment, question, error etc.
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info('Display number of items in the console.'); // Information message
$this->error('Something went wrong!'); // To display error message
$this->comment('Awesome!!'); //Write as comment string.
$this->write('Display this in the console.'); //Write as standard string output
}