Download the PHP package krisanalfa/konsole without Composer
On this page you can find all versions of the php package krisanalfa/konsole. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download krisanalfa/konsole
More information about krisanalfa/konsole
Files in krisanalfa/konsole
Package konsole
Short Description Minimum Console Application with PHP.
License MIT
Informations about the package konsole
Konsole
- Introduction
- Installing
- Writing Commands
- Command Structure
- Command I/O
- Defining Input Expectations
- Retrieving Input
- Prompting For Input
- Writing Output
- Registering Commands
- Calling Commands from Other Commands
- Logging
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:
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
:
Installing
Installing Konsole can be done simply via composer
command:
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:
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:
To add a description in your newly generated command, you can supply --description
or -D
option:
If you want to force generate the command, you may supply --force
or -F
option:
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.
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
:
You may also make arguments optional and define default values for optional arguments:
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:
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
:
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:
In this example, the user may pass a value for the option like so:
You may also assign default values to options:
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:
If you would like to define arguments or options to expect array inputs, you may use the *
character:
Or:
Input Descriptions
You may assign descriptions to input arguments and options by separating the parameter from the description using a colon:
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:
If you need to retrieve all of the arguments as an array
, call argument
with no parameters:
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
:
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:
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:
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
.
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:
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:
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:
To display an warning message, use the warn
method. Warning message text is typically displayed in orange:
To display an error message, use the error
method. Error message text is typically displayed in red:
If you want to display plain console output, use the line
method. The line
method does not receive any unique coloration:
`
If you want to suggest user to do something, you can use the suggest
method:
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:
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:
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:
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:
Or if you wish to add more than one commands, you may pass the argument of registerCommand
method in array:
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:
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:
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.
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:
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:
All versions of konsole with dependencies
illuminate/support Version ^5.2
illuminate/container Version ^5.2
illuminate/config Version ^5.2
monolog/monolog Version ^1.17