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
Afficher le fichier Open project: cakephp/cakephp Class Usage Examples

Méthodes publiques

Свойство 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

Свойство Type Description
$_io ConsoleIo ConsoleIo instance.
$_taskMap array Normalized map of tasks.

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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 méthode

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

__debugInfo() public méthode

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

__get() public méthode

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

_displayHelp() protected méthode

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

_setOutputLevel() protected méthode

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

_stop() protected méthode

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
Résultat void

_welcome() protected méthode

Displays a header for the shell
protected _welcome ( ) : void
Résultat void

abort() public méthode

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.
Résultat void

clear() public méthode

Clear the console
public clear ( ) : void
Résultat void

createFile() public méthode

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.
Résultat boolean Success

dispatchShell() public méthode

### 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
Résultat mixed The return of the other shell.

err() public méthode

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
Résultat integer | boolean The number of bytes returned from writing to stderr.

error() public méthode

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.
Résultat integer Error code

getOptionParser() public méthode

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

hasMethod() public méthode

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.
Résultat boolean

hasTask() public méthode

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.
Résultat boolean Success

helper() public méthode

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.
Résultat Cake\Console\Helper The created helper instance.

hr() public méthode

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
Résultat void

in() public méthode

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.
Résultat mixed Either the default value, or the user-provided input.

info() public méthode

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.
Résultat integer | boolean The number of bytes returned from writing to stdout.

initialize() public méthode

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

io() public méthode

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

loadTasks() public méthode

Loads tasks defined in public $tasks
public loadTasks ( ) : boolean
Résultat boolean

nl() public méthode

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

out() public méthode

### 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.
Résultat integer | boolean The number of bytes returned from writing to stdout.

param() public méthode

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

parseDispatchArguments() public méthode

Parses the arguments for the dispatchShell() method.
public parseDispatchArguments ( array $args ) : array
$args array Arguments fetch from the dispatchShell() method with func_get_args()
Résultat 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 méthode

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
Résultat integer | boolean The number of bytes returned from writing to stdout.

runCommand() public méthode

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
Résultat integer | boolean | null

shortPath() public méthode

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

startup() public méthode

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
Résultat void

success() public méthode

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.
Résultat integer | boolean The number of bytes returned from writing to stdout.

verbose() public méthode

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
Résultat integer | boolean The number of bytes returned from writing to stdout.

warn() public méthode

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
Résultat integer | boolean The number of bytes returned from writing to stderr.

wrapText() public méthode

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.
Résultat string Wrapped / indented text

Property Details

$OptionParser public_oe property

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

$Tasks public_oe property

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

$_io protected_oe property

ConsoleIo instance.
protected ConsoleIo,Cake\Console $_io
Résultat ConsoleIo

$_taskMap protected_oe property

Normalized map of tasks.
protected array $_taskMap
Résultat array

$args public_oe property

Contains arguments parsed from the command line.
public array $args
Résultat array

$command public_oe property

The command (method/task) that is being run.
public string $command
Résultat string

$interactive public_oe property

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

$name public_oe property

The name of the shell in camelized.
public string $name
Résultat string

$params public_oe property

Contains command switches parsed from the command line.
public array $params
Résultat array

$plugin public_oe property

Is automatically set by ShellDispatcher when a shell is constructed.
public string $plugin
Résultat string

$taskNames public_oe property

Contains the loaded tasks
public array $taskNames
Résultat array

$tasks public_oe property

Contains tasks to load and instantiate
public array $tasks
Résultat array