krisanalfa/konsole

Minimum Console Application with PHP.

1.0.0 2016-02-21 07:52 UTC

This package is auto-updated.

Last update: 2024-11-29 04:00:37 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License Monthly Downloads Daily Downloads

Introduction

Konsole is a minimum console application built on Laravel Console components. To view a list of all available Konsole commands, you may use the list command:

php konsole list

Every command also includes a "help" screen which displays and describes the command's available arguments and options. To view a help screen, simply precede the name of the command with help:

php konsole help generate

Installing

Installing Konsole can be done simply via composer command:

composer create-project krisanalfa/konsole my-console-application
cd my-console-application
php konsole --version

Writing Commands

In addition to the commands provided with Konsole, you may also build your own custom commands for working with your application. You may store your custom commands in the src/Konsole/Commands directory; however, you are free to choose your own storage location as long as your commands can be autoloaded based on your composer.json settings.

To create a new command, you may use the generate Konsole command, which will generate a command stub to help you get started:

php konsole generate SendEmails

The command above would generate a class at src/Konsole/Commands/SendEmails.php. When creating the command, the --command or -C option may be used to assign the terminal command name:

php konsole make:console SendEmails --command=emails:send

To add a description in your newly generated command, you can supply --description or -D option:

php konsole make:console SendEmails --command=emails:send --description='Send email to a given user id.'

If you want to force generate the command, you may supply --force or -F option:

php konsole make:console SendEmails --command=emails:send --force

Command Structure

Once your command is generated, you should fill out the signature and description properties of the class, which will be used when displaying your command on the list screen.

The handle method will be called when your command is executed. You may place any command logic in this method. Let's take a look at an example command.

Note that we are able to inject any dependencies we need into the command's constructor. The Laravel service container will automatically inject all dependencies type-hinted in the constructor. For greater code reusability, it is good practice to keep your console commands light and let them defer to application services to accomplish their tasks.

namespace Konsole\Commands;

use Konsole\Command;

class SendEmails extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = '';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
    }
}

Command I/O

Defining Input Expectations

When writing console commands, it is common to gather input from the user through arguments or options. Konsole makes it very convenient to define the input you expect from the user using the signature property on your commands. The signature property allows you to define the name, arguments, and options for the command in a single, expressive, route-like syntax.

All user supplied arguments and options are wrapped in curly braces. In the following example, the command defines one required argument: user:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'email:send {user}';

You may also make arguments optional and define default values for optional arguments:

// Optional argument...
protected $signature = 'email:send {user?}'

// Optional argument with default value...
protected $signature = 'email:send {user=foo}'

Options, like arguments, are also a form of user input. However, they are prefixed by two hyphens (--) when they are specified on the command line. We can define options in the signature like so:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'email:send {user} {--pretending}';

In this example, the --pretending switch may be specified when calling the Konsole command. If the --pretending switch is passed, the value of the option will be true. Otherwise, the value will be false:

php konsole email:send 1 --pretending

You may also specify that the option should be assigned a value by the user by suffixing the option name with a = sign, indicating that a value should be provided:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'email:send {user} {--pretending=}';

In this example, the user may pass a value for the option like so:

php konsole email:send 1 --pretending=default

You may also assign default values to options:

protected $signature = 'email:send {user} {--pretending=default}';

To assign a shortcut when defining an option, you may specify it before the option name and use a | delimiter to separate the shortcut from the full option name:

protected $signature = 'email:send {user} {--P|pretending}';

If you would like to define arguments or options to expect array inputs, you may use the * character:

protected $signature = 'email:send {user*}';

Or:

protected $signature = 'email:send {user} {--id=*}';

Input Descriptions

You may assign descriptions to input arguments and options by separating the parameter from the description using a colon:

/**
 * The name and signature of the console command.
 *
 * @var string
 */
protected $signature = 'email:send
                        {user : The ID of the user}
                        {--pretending= : Whether the job should be prentended}';

Retrieving Input

While your command is executing, you will obviously need to access the values for the arguments and options accepted by your command. To do so, you may use the argument and option methods:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $userId = $this->argument('user');

    //
}

If you need to retrieve all of the arguments as an array, call argument with no parameters:

$arguments = $this->argument();

Options may be retrieved just as easily as arguments using the option method. Like the argument method, you may call option without any parameters in order to retrieve all of the options as an array:

// Retrieve a specific option...
$isPretending = ($this->option('pretending') !== null);

// Retrieve all options...
$options = $this->option();

If the argument or option does not exist, null will be returned.

Prompting For Input

In addition to displaying output, you may also ask the user to provide input during the execution of your command. The ask method will prompt the user with the given question, accept their input, and then return the user's input back to your command:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $name = $this->ask('What is your name?');
}

The secret method is similar to ask, but the user's input will not be visible to them as they type in the console. This method is useful when asking for sensitive information such as a password:

$password = $this->secret('What is the password?');

Asking For Confirmation

If you need to ask the user for a simple confirmation, you may use the confirm method. By default, this method will return false. However, if the user enters y in response to the prompt, the method will return true.

// The default answer is 'no|N'
if ($this->confirm('Do you wish to continue? [y|N]')) {
    // Do something if user answer 'yes|y'
}

Giving The User A Choice

The anticipate method can be used to provide autocompletion for possible choices. The user can still choose any answer, regardless of the auto-completion hints:

$name = $this->anticipate('What is your name?', ['Alfa', 'Fitria']);

If you need to give the user a predefined set of choices, you may use the choice method. The user chooses the index of the answer, but the value of the answer will be returned to you. You may set the default value to be returned if nothing is chosen:

$name = $this->choice('What is your name?', ['Alfa', 'Fitria'], $default);

Writing Output

To send output to the console, use the line, info, comment, question, warn and error methods. Each of these methods will use the appropriate ANSI colors for their purpose.

To display an information message to the user, use the info method. Typically, this will display in the console as green text:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $this->info('Display this on the screen');
}

To display an warning message, use the warn method. Warning message text is typically displayed in orange:

$this->warn('Something went wrong!');

To display an error message, use the error method. Error message text is typically displayed in red:

$this->error('Something went wrong!');

If you want to display plain console output, use the line method. The line method does not receive any unique coloration:

$this->line('Display this on the screen');

If you want to suggest user to do something, you can use the suggest method:

$this->suggest('Better you pick Sven, because you have Magnus on your side.');

Table Layouts

The table method makes it easy to correctly format multiple rows / columns of data. Just pass in the headers and rows to the method. The width and height will be dynamically calculated based on the given data:

$headers = ['Name', 'Email'];

$this->table($headers, $collection->toArray());

Progress Bars

For long running tasks, it could be helpful to show a progress indicator. Using the output object, we can start, advance and stop the Progress Bar. You have to define the number of steps when you start the progress, then advance the Progress Bar after each step:

$bar = $this->output->createProgressBar(count($users));

foreach ($users as $user) {
    $this->performTask($user);

    $bar->advance();
}

$bar->finish();

For more advanced options, check out the Symfony Progress Bar component documentation.

Registering Commands

Once your command is finished, you need to register it with Konsole so it will be available for use. This done within the config/app.php. You may add your commands in commands section:

'commands' => [
    'Konsole\Commands\SendEmails',
    'Konsole\Commands\GenerateCommand',
],

In some cases you may want to register your command via Service Provider. This is done within the bootstrap/app.php file. Within this file, you may register your own command via registerCommand method:

$konsole->registerCommand('Konsole\Commands\SendEmails');

Or if you wish to add more than one commands, you may pass the argument of registerCommand method in array:

$konsole->registerCommand([
    'Konsole\Commands\FooBarBaz',
    'Konsole\Commands\SendEmails',
    'Konsole\Commands\AnotherCommand',
]);

Calling Commands From Other Commands

Sometimes you may wish to call other commands from an existing Konsole command. You may do so using the call method. This call method accepts the command name and an array of command parameters:

/**
 * Execute the console command.
 *
 * @return mixed
 */
public function handle()
{
    $this->call('email:send', [
        'user' => 1, '--pretending' => 'default'
    ]);

    //
}

If you would like to call another console command and suppress all of its output, you may use the callSilent method. The callSilent method has the same signature as the call method:

$this->callSilent('email:send', [
    'user' => 1, '--pretending' => 'default'
]);

Logging

The Konsole logging facilities provide a simple layer on top of the powerful Monolog library. By default, Konsole is configured to create daily log files for your application which are stored in the var/log directory. You may write information to the logs using the $konsole->make('log') object. The Konsole logger provides the eight logging levels defined in RFC 5424: emergency, alert, critical, error, warning, notice, info and debug.

$konsole->log->emergency($message);
$konsole->log->alert($message);
$konsole->log->critical($message);
$konsole->log->message($message);
$konsole->log->warning($message);
$konsole->log->notice($message);
$konsole->log->info($message);
$konsole->log->debug($message);

Contextual Information

An array of contextual data may also be passed to the log methods. This contextual data will be formatted and displayed with the log message:

$konsole->make('log')->info('User failed to login.', ['id' => $user->id]);

Accessing The Underlying Monolog Instance

Monolog has a variety of additional handlers you may use for logging. If needed, you may access the underlying Monolog instance being used by Konsole:

$monolog = $konsole->make('log');