PHP Class Cake\Console\Shell

Is the equivalent of Cake\Controller\Controller on the command line.
Inheritance: use trait Cake\ORM\Locator\LocatorAwareTrait, use trait Cake\Log\LogTrait, use trait Cake\Utility\MergeVariablesTrait, use trait Cake\Datasource\ModelAwareTrait
Show file Open project: cakephp/cakephp Class Usage Examples

Public Properties

Property Type Description
$OptionParser Cake\Console\ConsoleOptionParser An instance of ConsoleOptionParser that has been configured for this class.
$Tasks Cake\Console\TaskRegistry Task Collection for the command, used to create Tasks.
$args array Contains arguments parsed from the command line.
$command string The command (method/task) that is being run.
$interactive boolean If true, the script will ask for permission to perform actions.
$name string The name of the shell in camelized.
$params array Contains command switches parsed from the command line.
$plugin string Is automatically set by ShellDispatcher when a shell is constructed.
$taskNames array Contains the loaded tasks
$tasks array Contains tasks to load and instantiate

Protected Properties

Property Type Description
$_io ConsoleIo ConsoleIo instance.
$_taskMap array Normalized map of tasks.

Public Methods

Method Description
__construct ( ConsoleIo $io = null ) Constructs this Shell instance.
__debugInfo ( ) : array Returns an array that can be used to describe the internal state of this object.
__get ( string $name ) : Shell Overload get for lazy building of tasks
abort ( string $message, integer $exitCode = self::CODE_ERROR ) : void Displays a formatted error message and exits the application with status code 1
clear ( ) : void Clear the console
createFile ( string $path, string $contents ) : boolean Creates a file at given path
dispatchShell ( ) : mixed Dispatch a command to another Shell. Similar to Object::requestAction() but intended for running shells from other shells.
err ( string | array | null $message = null, integer $newlines = 1 ) : integer | boolean Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
error ( string $title, string | null $message = null, integer $exitCode = self::CODE_ERROR ) : integer Displays a formatted error message and exits the application with status code 1
getOptionParser ( ) : Cake\Console\ConsoleOptionParser Gets the option parser instance and configures it.
hasMethod ( string $name ) : boolean Check to see if this shell has a callable method by the given name.
hasTask ( string $task ) : boolean Check to see if this shell has a task with the provided name.
helper ( string $name, array $settings = [] ) : Cake\Console\Helper Render a Console Helper
hr ( integer $newlines, integer $width = 63 ) : void Outputs a series of minus characters to the standard output, acts as a visual separator.
in ( string $prompt, string | array | null $options = null, string | null $default = null ) : mixed Prompts the user for input, and returns it.
info ( string | array | null $message = null, integer $newlines = 1, integer $level = Shell::NORMAL ) : integer | boolean Convenience method for out() that wraps message between tag
initialize ( ) : void Initializes the Shell acts as constructor for subclasses allows configuration of tasks prior to shell execution
io ( ConsoleIo $io = null ) : ConsoleIo Get/Set the io object for this shell.
loadTasks ( ) : boolean Loads tasks defined in public $tasks
nl ( integer $multiplier = 1 ) : string Returns a single or multiple linefeeds sequences.
out ( string | array | null $message = null, integer $newlines = 1, integer $level = Shell::NORMAL ) : integer | boolean Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
param ( string $name ) : string | boolean | null Safely access the values in $this->params.
parseDispatchArguments ( array $args ) : array Parses the arguments for the dispatchShell() method.
quiet ( string | array $message, integer $newlines = 1 ) : integer | boolean Output at all levels.
runCommand ( array $argv, boolean $autoMethod = false, array $extra = [] ) : integer | boolean | null Runs the Shell with the provided argv.
shortPath ( string $file ) : string Makes absolute file path easier to read
startup ( ) : void Starts up the Shell and displays the welcome message.
success ( string | array | null $message = null, integer $newlines = 1, integer $level = Shell::NORMAL ) : integer | boolean Convenience method for out() that wraps message between tag
verbose ( string | array $message, integer $newlines = 1 ) : integer | boolean Output at the verbose level.
warn ( string | array | null $message = null, integer $newlines = 1 ) : integer | boolean Convenience method for err() that wraps message between tag
wrapText ( string $text, integer | array $options = [] ) : string Wrap a block of text.

Protected Methods

Method Description
_displayHelp ( string $command ) : integer | boolean Display the help in the correct format
_setOutputLevel ( ) : void Set the output level based on the parameters.
_stop ( integer | string $status ) : void Stop execution of the current script.
_welcome ( ) : void Displays a header for the shell

Method Details

__construct() public method

Constructs this Shell instance.
public __construct ( ConsoleIo $io = null )
$io ConsoleIo An io instance.

__debugInfo() public method

Returns an array that can be used to describe the internal state of this object.
public __debugInfo ( ) : array
return array

__get() public method

Overload get for lazy building of tasks
public __get ( string $name ) : Shell
$name string The task to get.
return Shell Object of Task

_displayHelp() protected method

Display the help in the correct format
protected _displayHelp ( string $command ) : integer | boolean
$command string The command to get help for.
return integer | boolean The number of bytes returned from writing to stdout.

_setOutputLevel() protected method

This reconfigures both the output level for out() and the configured stdout/stderr logging
protected _setOutputLevel ( ) : void
return void

_stop() protected method

Raises a StopException to try and halt the execution.
protected _stop ( integer | string $status ) : void
$status integer | string see http://php.net/exit for values
return void

_welcome() protected method

Displays a header for the shell
protected _welcome ( ) : void
return void

abort() public method

Displays a formatted error message and exits the application with status code 1
public abort ( string $message, integer $exitCode = self::CODE_ERROR ) : void
$message string The error message
$exitCode integer The exit code for the shell task.
return void

clear() public method

Clear the console
public clear ( ) : void
return void

createFile() public method

Creates a file at given path
public createFile ( string $path, string $contents ) : boolean
$path string Where to put the file.
$contents string Content to put in the file.
return boolean Success

dispatchShell() public method

### Usage: With a string command: return $this->dispatchShell('schema create DbAcl'); Avoid using this form if you have string arguments, with spaces in them. The dispatched will be invoked incorrectly. Only use this form for simple command dispatching. With an array command: return $this->dispatchShell('schema', 'create', 'i18n', '--dry'); With an array having two key / value pairs: - command can accept either a string or an array. Represents the command to dispatch - extra can accept an array of extra parameters to pass on to the dispatcher. This parameters will be available in the param property of the called Shell return $this->dispatchShell([ 'command' => 'schema create DbAcl', 'extra' => ['param' => 'value'] ]); or return $this->dispatchShell([ 'command' => ['schema', 'create', 'DbAcl'], 'extra' => ['param' => 'value'] ]);
public dispatchShell ( ) : mixed
return mixed The return of the other shell.

err() public method

Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
public err ( string | array | null $message = null, integer $newlines = 1 ) : integer | boolean
$message string | array | null A string or an array of strings to output
$newlines integer Number of newlines to append
return integer | boolean The number of bytes returned from writing to stderr.

error() public method

Displays a formatted error message and exits the application with status code 1
Deprecation: Since 3.2.0. Use Shell::abort() instead.
public error ( string $title, string | null $message = null, integer $exitCode = self::CODE_ERROR ) : integer
$title string Title of the error
$message string | null An optional error message
$exitCode integer The exit code for the shell task.
return integer Error code

getOptionParser() public method

By overriding this method you can configure the ConsoleOptionParser before returning it.
public getOptionParser ( ) : Cake\Console\ConsoleOptionParser
return Cake\Console\ConsoleOptionParser

hasMethod() public method

Check to see if this shell has a callable method by the given name.
public hasMethod ( string $name ) : boolean
$name string The method name to check.
return boolean

hasTask() public method

Check to see if this shell has a task with the provided name.
public hasTask ( string $task ) : boolean
$task string The task name to check.
return boolean Success

helper() public method

Create and render the output for a helper object. If the helper object has not already been loaded, it will be loaded and constructed.
public helper ( string $name, array $settings = [] ) : Cake\Console\Helper
$name string The name of the helper to render
$settings array Configuration data for the helper.
return Cake\Console\Helper The created helper instance.

hr() public method

Outputs a series of minus characters to the standard output, acts as a visual separator.
public hr ( integer $newlines, integer $width = 63 ) : void
$newlines integer Number of newlines to pre- and append
$width integer Width of the line, defaults to 63
return void

in() public method

Prompts the user for input, and returns it.
public in ( string $prompt, string | array | null $options = null, string | null $default = null ) : mixed
$prompt string Prompt text.
$options string | array | null Array or string of options.
$default string | null Default input value.
return mixed Either the default value, or the user-provided input.

info() public method

Convenience method for out() that wraps message between tag
See also: http://book.cakephp.org/3.0/en/console-and-shells.html#Shell::out
public info ( string | array | null $message = null, integer $newlines = 1, integer $level = Shell::NORMAL ) : integer | boolean
$message string | array | null A string or an array of strings to output
$newlines integer Number of newlines to append
$level integer The message's output level, see above.
return integer | boolean The number of bytes returned from writing to stdout.

initialize() public method

Initializes the Shell acts as constructor for subclasses allows configuration of tasks prior to shell execution
public initialize ( ) : void
return void

io() public method

Get/Set the io object for this shell.
public io ( ConsoleIo $io = null ) : ConsoleIo
$io ConsoleIo The ConsoleIo object to use.
return ConsoleIo The current ConsoleIo object.

loadTasks() public method

Loads tasks defined in public $tasks
public loadTasks ( ) : boolean
return boolean

nl() public method

Returns a single or multiple linefeeds sequences.
public nl ( integer $multiplier = 1 ) : string
$multiplier integer Number of times the linefeed sequence should be repeated
return string

out() public method

### Output levels There are 3 built-in output level. Shell::QUIET, Shell::NORMAL, Shell::VERBOSE. The verbose and quiet output levels, map to the verbose and quiet output switches present in most shells. Using Shell::QUIET for a message means it will always display. While using Shell::VERBOSE means it will only display when verbose output is toggled.
public out ( string | array | null $message = null, integer $newlines = 1, integer $level = Shell::NORMAL ) : integer | boolean
$message string | array | null A string or an array of strings to output
$newlines integer Number of newlines to append
$level integer The message's output level, see above.
return integer | boolean The number of bytes returned from writing to stdout.

param() public method

Safely access the values in $this->params.
public param ( string $name ) : string | boolean | null
$name string The name of the parameter to get.
return string | boolean | null Value. Will return null if it doesn't exist.

parseDispatchArguments() public method

Parses the arguments for the dispatchShell() method.
public parseDispatchArguments ( array $args ) : array
$args array Arguments fetch from the dispatchShell() method with func_get_args()
return array First value has to be an array of the command arguments. Second value has to be an array of extra parameter to pass on to the dispatcher

quiet() public method

Output at all levels.
public quiet ( string | array $message, integer $newlines = 1 ) : integer | boolean
$message string | array A string or an array of strings to output
$newlines integer Number of newlines to append
return integer | boolean The number of bytes returned from writing to stdout.

runCommand() public method

Delegates calls to Tasks and resolves methods inside the class. Commands are looked up with the following order: - Method on the shell. - Matching task name. - main() method. If a shell implements a main() method, all missing method calls will be sent to main() with the original method name in the argv. For tasks to be invoked they *must* be exposed as subcommands. If you define any subcommands, you must define all the subcommands your shell needs, whether they be methods on this class or methods on tasks.
public runCommand ( array $argv, boolean $autoMethod = false, array $extra = [] ) : integer | boolean | null
$argv array Array of arguments to run the shell with. This array should be missing the shell name.
$autoMethod boolean Set to true to allow any public method to be called even if it was not defined as a subcommand. This is used by ShellDispatcher to make building simple shells easy.
$extra array Extra parameters that you can manually pass to the Shell to be dispatched. Built-in extra parameter is : - `requested` : if used, will prevent the Shell welcome message to be displayed
return integer | boolean | null

shortPath() public method

Makes absolute file path easier to read
public shortPath ( string $file ) : string
$file string Absolute file path
return string short path

startup() public method

Allows for checking and configuring prior to command or main execution Override this method if you want to remove the welcome information, or otherwise modify the pre-command flow.
public startup ( ) : void
return void

success() public method

Convenience method for out() that wraps message between tag
See also: http://book.cakephp.org/3.0/en/console-and-shells.html#Shell::out
public success ( string | array | null $message = null, integer $newlines = 1, integer $level = Shell::NORMAL ) : integer | boolean
$message string | array | null A string or an array of strings to output
$newlines integer Number of newlines to append
$level integer The message's output level, see above.
return integer | boolean The number of bytes returned from writing to stdout.

verbose() public method

Output at the verbose level.
public verbose ( string | array $message, integer $newlines = 1 ) : integer | boolean
$message string | array A string or an array of strings to output
$newlines integer Number of newlines to append
return integer | boolean The number of bytes returned from writing to stdout.

warn() public method

Convenience method for err() that wraps message between tag
See also: http://book.cakephp.org/3.0/en/console-and-shells.html#Shell::err
public warn ( string | array | null $message = null, integer $newlines = 1 ) : integer | boolean
$message string | array | null A string or an array of strings to output
$newlines integer Number of newlines to append
return integer | boolean The number of bytes returned from writing to stderr.

wrapText() public method

Allows you to set the width, and indenting on a block of text. ### Options - width The width to wrap to. Defaults to 72 - wordWrap Only wrap on words breaks (spaces) Defaults to true. - indent Indent the text with the string provided. Defaults to null.
public wrapText ( string $text, integer | array $options = [] ) : string
$text string Text the text to format.
$options integer | array Array of options to use, or an integer to wrap the text to.
return string Wrapped / indented text

Property Details

$OptionParser public property

An instance of ConsoleOptionParser that has been configured for this class.
public ConsoleOptionParser,Cake\Console $OptionParser
return Cake\Console\ConsoleOptionParser

$Tasks public property

Task Collection for the command, used to create Tasks.
public TaskRegistry,Cake\Console $Tasks
return Cake\Console\TaskRegistry

$_io protected property

ConsoleIo instance.
protected ConsoleIo,Cake\Console $_io
return ConsoleIo

$_taskMap protected property

Normalized map of tasks.
protected array $_taskMap
return array

$args public property

Contains arguments parsed from the command line.
public array $args
return array

$command public property

The command (method/task) that is being run.
public string $command
return string

$interactive public property

If true, the script will ask for permission to perform actions.
public bool $interactive
return boolean

$name public property

The name of the shell in camelized.
public string $name
return string

$params public property

Contains command switches parsed from the command line.
public array $params
return array

$plugin public property

Is automatically set by ShellDispatcher when a shell is constructed.
public string $plugin
return string

$taskNames public property

Contains the loaded tasks
public array $taskNames
return array

$tasks public property

Contains tasks to load and instantiate
public array $tasks
return array