PHP Class lithium\console\Command

Inheritance: extends lithium\core\Object
显示文件 Open project: unionofrad/lithium Class Usage Examples

Public Properties

Property Type Description
$help boolean Similar to running li3 help COMMAND.
$plain boolean Only shows only text output without styles.
$request object A Request object.
$response object A Response object.
$silent boolean Only shows error output.

Protected Properties

Property Type Description
$_autoConfig array Auto configuration.
$_classes array Dynamic dependencies.

Public Methods

Method Description
__construct ( array $config = [] ) : void Constructor.
__invoke ( string $action, array $args = [] ) : object Called by the Dispatcher class to invoke an action.
clear ( ) : void Clears the entire screen.
columns ( array $rows, array $options = [] ) : void Writes rows of columns.
error ( string | array $error = null, mixed $options = ['nl' => 1] ) : integer Writes a string to error stream.
header ( string $text, integer $line = null ) : void Writes a header to the output stream. In addition to the actual text, horizontal lines before and afterwards are written. The lines will have the same length as the text. This behavior can be modified by providing the length of lines as a second paramerter.
hr ( integer $length = 80, integer $newlines = 1 ) : integer Adds a horizontal line to output stream.
in ( string $prompt = null, array $options = [] ) : string | boolean Handles input. Will continue to loop until $options['quit'] or result is part of $options['choices'].
nl ( integer $number = 1 ) : string Add newlines ("\n") a given number of times and return them in a single string.
out ( string | array $output = null, mixed $options = ['nl' => 1] ) : integer Writes a string to the output stream.
stop ( integer $status, string | null $message = null ) : void Stop execution, by exiting the script.

Protected Methods

Method Description
_help ( ) : boolean Invokes the Help command.
_init ( ) : void Command Initializer.
_response ( string $type, string | array $string, mixed $options ) : void Handles the response that is sent to the stream.

Method Details

__construct() public method

Constructor.
public __construct ( array $config = [] ) : void
$config array Available configuration options are: - `'request'` _object|null_ - `'response'` _array_ - `'classes'` _array_
return void

__invoke() public method

Called by the Dispatcher class to invoke an action.
See also: lithium\console\Dispatcher
See also: lithium\console\Response
public __invoke ( string $action, array $args = [] ) : object
$action string The name of the method to run.
$args array The arguments from the request.
return object The response object associated with this command.

_help() protected method

The invoked Help command will take over request and response objects of the originally invoked command. Thus the response of the Help command becomes the response of the original one.
protected _help ( ) : boolean
return boolean

_init() protected method

Populates the $response property with a new instance of the Response class passing it configuration and assigns the values from named parameters of the request (if applicable) to properties of the command.
protected _init ( ) : void
return void

_response() protected method

Handles the response that is sent to the stream.
protected _response ( string $type, string | array $string, mixed $options ) : void
$type string The stream either output or error.
$string string | array The message to render.
$options mixed When passed an integer or boolean it is used as the number of of new lines, when passed a string it is interpreted as style to use otherwise when an array following options are available: - `'nl'` _integer|boolean_: number of new lines to add at the end. `false` to disable adding a newline. - `'style'` _string_: the style name to wrap around the output.
return void

clear() public method

Clears the entire screen.
public clear ( ) : void
return void

columns() public method

This method expects asceding integer values as the keys, which map to the appropriate columns. Currently, there is no special "header" option, but you can define them for your own. Example Usage: $output = array( array('Name', 'Age'), array('----', '---'), ); foreach($users as $user) { $output[] = array($user->name, $user->age); } $this->columns($output); Would render something similar to: Name Age ---- --- Jane Doe 22 Foo Bar 18 This method also calculates the needed space between the columns. All option params given also get passed down to the out() method, which allow custom formatting. Passing something like $this->columns($output, array('style' => 'red) would print the table in red.
See also: lithium\console\Response::styles()
public columns ( array $rows, array $options = [] ) : void
$rows array The rows to print, with each column as an array element.
$options array Optional params: - separator : Different column separator, defaults to `\t` - style : the style name to wrap around the columns output
return void

error() public method

Writes a string to error stream.
public error ( string | array $error = null, mixed $options = ['nl' => 1] ) : integer
$error string | array The string or an array of strings to write.
$options mixed When passed an integer or boolean it is used as the number of of new lines, when passed a string it is interpreted as style to use otherwise when an array following options are available: - `'nl'` _integer|boolean_: number of new lines to add at the end. `false` to disable adding a newline. - `'style'` _string_: the style name to wrap around the output.
return integer

header() public method

Given the text 'Lithium' this generates following output: ------- Lithium -------
public header ( string $text, integer $line = null ) : void
$text string The heading text.
$line integer The length of the line. Defaults to the length of text.
return void

hr() public method

Adds a horizontal line to output stream.
public hr ( integer $length = 80, integer $newlines = 1 ) : integer
$length integer The length of the line, defaults to 80.
$newlines integer How many new lines to print afterwards, defaults to 1.
return integer

in() public method

Handles input. Will continue to loop until $options['quit'] or result is part of $options['choices'].
public in ( string $prompt = null, array $options = [] ) : string | boolean
$prompt string
$options array
return string | boolean Returns the result of the input data. If the input is equal to the `quit` option boolean `false` is returned.

nl() public method

Add newlines ("\n") a given number of times and return them in a single string.
public nl ( integer $number = 1 ) : string
$number integer The number of new lines to fill into a string.
return string

out() public method

Writes a string to the output stream.
public out ( string | array $output = null, mixed $options = ['nl' => 1] ) : integer
$output string | array The string or an array of strings to write.
$options mixed When passed an integer or boolean it is used as the number of of new lines, when passed a string it is interpreted as style to use otherwise when an array following options are available: - `'nl'` _integer|boolean_: number of new lines to add at the end. `false` to disable adding a newline. - `'style'` _string_: the style name to wrap around the output.
return integer

stop() public method

Stop execution, by exiting the script.
public stop ( integer $status, string | null $message = null ) : void
$status integer Numeric value that will be used on `exit()`.
$message string | null An optional message that will be written to the stream.
return void

Property Details

$_autoConfig protected_oe property

Auto configuration.
protected array $_autoConfig
return array

$_classes protected_oe property

Dynamic dependencies.
protected array $_classes
return array

$help public_oe property

Similar to running li3 help COMMAND.
public bool $help
return boolean

$plain public_oe property

Only shows only text output without styles.
public bool $plain
return boolean

$request public_oe property

A Request object.
See also: lithium\console\Request
public object $request
return object

$response public_oe property

A Response object.
See also: lithium\console\Response
public object $response
return object

$silent public_oe property

Only shows error output.
public bool $silent
return boolean