Class CommandTask
Base class for Shell Command reflection.
Constants
Property Summary
-
$OptionParser public
Cake\Console\ConsoleOptionParser
An instance of ConsoleOptionParser that has been configured for this class.
-
$Tasks public
TaskRegistry
Task Collection for the command, used to create Tasks.
-
$_io protected
Cake\Console\ConsoleIo
ConsoleIo instance.
-
$_modelFactories protected
array
A list of model factory functions.
-
$_taskMap protected
array
Normalized map of tasks.
-
$args public
array
Contains arguments parsed from the command line.
-
$command public
string
The command (method/task) that is being run.
-
$interactive public
bool
If true, the script will ask for permission to perform actions.
-
$modelClass public
string
This object's primary model class name. Should be a plural form. CakePHP will not inflect the name.
-
$name public
string
The name of the shell in camelized.
-
$params public
array
Contains command switches parsed from the command line.
-
$plugin public
string
The name of the plugin the shell belongs to. Is automatically set by ShellDispatcher when a shell is constructed.
-
$taskNames public
array
Contains the loaded tasks
-
$tasks public
array
Contains tasks to load and instantiate
Method Summary
-
__construct() public
Constructs this Shell instance.
-
__debugInfo() public
Returns an array that can be used to describe the internal state of this object.
-
__get() public
Overload get for lazy building of tasks
-
_appendShells() protected
Scan the provided paths for shells, and append them into $shellList
-
_displayHelp() protected
Display the help in the correct format
-
_mergeProperty() protected
Merge a single property with the values declared in all parent classes.
-
_mergePropertyData() protected
Merge each of the keys in a property together.
-
_mergeVars() protected
Merge the list of $properties with all parent classes of the current class.
-
_scanDir() protected
Scan a directory for .php files and return the class names that should be within them.
-
_setModelClass() protected
Set the modelClass and modelKey properties based on conventions.
-
_stop() protected
Stop execution of the current script. Wraps exit() making testing easier.
-
_welcome() protected
Displays a header for the shell
-
clear() public
Clear the console
-
commands() public
Return a list of all commands
-
createFile() public
Creates a file at given path
-
dispatchShell() public
Dispatch a command to another Shell. Similar to Object::requestAction() but intended for running shells from other shells.
-
err() public
Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
-
error() public
Displays a formatted error message and exits the application with status code 1
-
getOptionParser() public
Gets the option parser instance and configures it.
-
getShell() public
Get Shell instance for the given command
-
getShellList() public
Gets the shell command listing.
-
hasMethod() public
Check to see if this shell has a callable method by the given name.
-
hasTask() public
Check to see if this shell has a task with the provided name.
-
hr() public
Outputs a series of minus characters to the standard output, acts as a visual separator.
-
in() public
Prompts the user for input, and returns it.
-
initialize() public
Initializes the Shell acts as constructor for subclasses allows configuration of tasks prior to shell execution
-
io() public
Get/Set the io object for this shell.
-
loadModel() public
Loads and constructs repository objects required by this object
-
loadTasks() public
Loads tasks defined in public $tasks
-
log() public
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
-
modelFactory() public
Register a callable to generate repositories of a given type.
-
nl() public
Returns a single or multiple linefeeds sequences.
-
options() public
Get Shell instance for the given command
-
out() public
Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
-
param() public
Safely access the values in $this->params.
-
quiet() public
Output at all levels.
-
runCommand() public
Runs the Shell with the provided argv.
-
shortPath() public
Makes absolute file path easier to read
-
startup() public
Starts up the Shell and displays the welcome message. Allows for checking and configuring prior to command or main execution
-
subCommands() public
Return a list of subcommands for a given command
-
verbose() public
Output at the verbose level.
-
wrapText() public
Wrap a block of text. Allows you to set the width, and indenting on a block of text.
Method Detail
__construct() ¶ public
__construct(Cake\Console\ConsoleIo $io = null)
Constructs this Shell instance.
Parameters
-
Cake\Console\ConsoleIo
$io optional An io instance.
Links
__debugInfo() ¶ public
__debugInfo(): array
Returns an array that can be used to describe the internal state of this object.
Returns
array
__get() ¶ public
__get(string $name): Cake\Console\Shell
Overload get for lazy building of tasks
Parameters
-
string
$name The task to get.
Returns
Cake\Console\Shell
_appendShells() ¶ protected
_appendShells(string $type, array $shells, array $shellList): array
Scan the provided paths for shells, and append them into $shellList
Parameters
-
string
$type The type of object.
-
array
$shells The shell name.
-
array
$shellList List of shells.
Returns
array
_displayHelp() ¶ protected
_displayHelp(string $command): int|bool
Display the help in the correct format
Parameters
-
string
$command The command to get help for.
Returns
int|bool
_mergeProperty() ¶ protected
_mergeProperty(string $property, array $parentClasses, array $options): void
Merge a single property with the values declared in all parent classes.
Parameters
-
string
$property The name of the property being merged.
-
array
$parentClasses An array of classes you want to merge with.
-
array
$options Options for merging the property, see _mergeVars()
Returns
void
_mergePropertyData() ¶ protected
_mergePropertyData(array $current, array $parent, bool $isAssoc): mixed
Merge each of the keys in a property together.
Parameters
-
array
$current The current merged value.
-
array
$parent The parent class' value.
-
bool
$isAssoc Whether or not the merging should be done in associative mode.
Returns
mixed
_mergeVars() ¶ protected
_mergeVars(array $properties, array $options = []): void
Merge the list of $properties with all parent classes of the current class.
Options:
associative
- A list of properties that should be treated as associative arrays. Properties in this list will be passed through Hash::normalize() before merging.
Parameters
-
array
$properties An array of properties and the merge strategy for them.
-
array
$options optional The options to use when merging properties.
Returns
void
_scanDir() ¶ protected
_scanDir(string $dir): array
Scan a directory for .php files and return the class names that should be within them.
Parameters
-
string
$dir The directory to read.
Returns
array
_setModelClass() ¶ protected
_setModelClass(string $name): void
Set the modelClass and modelKey properties based on conventions.
If the properties are already set they will not be overwritten
Parameters
-
string
$name Class name.
Returns
void
_stop() ¶ protected
_stop(int|string $status = 0): void
Stop execution of the current script. Wraps exit() making testing easier.
Parameters
-
int|string
$status optional see http://php.net/exit for values
Returns
void
clear() ¶ public
clear(): void
Clear the console
Returns
void
Links
createFile() ¶ public
createFile(string $path, string $contents): bool
Creates a file at given path
Parameters
-
string
$path Where to put the file.
-
string
$contents Content to put in the file.
Returns
bool
Links
dispatchShell() ¶ public
dispatchShell(): mixed
Dispatch a command to another Shell. Similar to Object::requestAction() but intended for running shells from other shells.
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');
Returns
mixed
Links
err() ¶ public
err(string|array|null $message = null, int $newlines = 1): void
Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
Parameters
-
string|array|null
$message optional A string or an array of strings to output
-
int
$newlines optional Number of newlines to append
Returns
void
error() ¶ public
error(string $title, string|null $message = null): void
Displays a formatted error message and exits the application with status code 1
Parameters
-
string
$title Title of the error
-
string|null
$message optional An optional error message
Returns
void
Links
getOptionParser() ¶ public
getOptionParser(): ConsoleOptionParser
Gets the option parser instance and configures it.
By overriding this method you can configure the ConsoleOptionParser before returning it.
Returns
ConsoleOptionParser
Links
getShell() ¶ public
getShell(mixed $commandName): mixed
Get Shell instance for the given command
Parameters
-
mixed
$commandName The command you want.
Returns
mixed
hasMethod() ¶ public
hasMethod(string $name): bool
Check to see if this shell has a callable method by the given name.
Parameters
-
string
$name The method name to check.
Returns
bool
Links
hasTask() ¶ public
hasTask(string $task): bool
Check to see if this shell has a task with the provided name.
Parameters
-
string
$task The task name to check.
Returns
bool
Links
hr() ¶ public
hr(int $newlines = 0, int $width = 63): void
Outputs a series of minus characters to the standard output, acts as a visual separator.
Parameters
-
int
$newlines optional Number of newlines to pre- and append
-
int
$width optional Width of the line, defaults to 63
Returns
void
Links
in() ¶ public
in(string $prompt, string|array|null $options = null, string|null $default = null): mixed
Prompts the user for input, and returns it.
Parameters
-
string
$prompt Prompt text.
-
string|array|null
$options optional Array or string of options.
-
string|null
$default optional Default input value.
Returns
mixed
Links
initialize() ¶ public
initialize(): void
Initializes the Shell acts as constructor for subclasses allows configuration of tasks prior to shell execution
Returns
void
Links
io() ¶ public
io(Cake\Console\ConsoleIo $io = null): Cake\Console\ConsoleIo
Get/Set the io object for this shell.
Parameters
-
Cake\Console\ConsoleIo
$io optional The ConsoleIo object to use.
Returns
Cake\Console\ConsoleIo
loadModel() ¶ public
loadModel(string|null $modelClass = null, string $type = 'Table'): object
Loads and constructs repository objects required by this object
Typically used to load ORM Table objects as required. Can also be used to load other types of repository objects your application uses.
If a repository provider does not return an object a MissingModelException will be thrown.
Parameters
-
string|null
$modelClass optional Name of model class to load. Defaults to $this->modelClass
-
string
$type optional The type of repository to load. Defaults to 'Table' which delegates to Cake\ORM\TableRegistry.
Returns
object
Throws
Cake\Datasource\Exception\MissingModelException
If the model class cannot be found.
InvalidArgumentException
When using a type that has not been registered.
log() ¶ public
log(mixed $msg, int|string $level = LogLevel::ERROR, string|array $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Parameters
-
mixed
$msg Log message.
-
int|string
$level optional Error level.
-
string|array
$context optional Additional log data relevant to this message.
Returns
bool
modelFactory() ¶ public
modelFactory(string $type, callable $factory): void
Register a callable to generate repositories of a given type.
Parameters
-
string
$type The name of the repository type the factory function is for.
-
callable
$factory The factory function used to create instances.
Returns
void
nl() ¶ public
nl(int $multiplier = 1): string
Returns a single or multiple linefeeds sequences.
Parameters
-
int
$multiplier optional Number of times the linefeed sequence should be repeated
Returns
string
Links
options() ¶ public
options(mixed $commandName): array
Get Shell instance for the given command
Parameters
-
mixed
$commandName The command to get options for.
Returns
array
out() ¶ public
out(string|array|null $message = null, int $newlines = 1, int $level = Shell::NORMAL): int|bool
Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
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.
Parameters
-
string|array|null
$message optional A string or an array of strings to output
-
int
$newlines optional Number of newlines to append
-
int
$level optional The message's output level, see above.
Returns
int|bool
Links
param() ¶ public
param(string $name): string|bool|null
Safely access the values in $this->params.
Parameters
-
string
$name The name of the parameter to get.
Returns
string|bool|null
quiet() ¶ public
quiet(string|array $message, int $newlines = 1): int|bool
Output at all levels.
Parameters
-
string|array
$message A string or an array of strings to output
-
int
$newlines optional Number of newlines to append
Returns
int|bool
runCommand() ¶ public
runCommand(array $argv, bool $autoMethod = false): mixed
Runs the Shell with the provided argv.
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.
Parameters
-
array
$argv Array of arguments to run the shell with. This array should be missing the shell name.
-
bool
$autoMethod optional 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.
Returns
mixed
Links
shortPath() ¶ public
shortPath(string $file): string
Makes absolute file path easier to read
Parameters
-
string
$file Absolute file path
Returns
string
Links
startup() ¶ public
startup(): void
Starts up the Shell and displays the welcome message. 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.
Returns
void
Links
subCommands() ¶ public
subCommands(string $commandName): array
Return a list of subcommands for a given command
Parameters
-
string
$commandName The command you want subcommands from.
Returns
array
verbose() ¶ public
verbose(string|array $message, int $newlines = 1): int|bool
Output at the verbose level.
Parameters
-
string|array
$message A string or an array of strings to output
-
int
$newlines optional Number of newlines to append
Returns
int|bool
wrapText() ¶ public
wrapText(string $text, int|array $options = []): string
Wrap a block of text. Allows you to set the width, and indenting on a block of text.
Options
width
The width to wrap to. Defaults to 72wordWrap
Only wrap on words breaks (spaces) Defaults to true.indent
Indent the text with the string provided. Defaults to null.
Parameters
-
string
$text Text the text to format.
-
int|array
$options optional Array of options to use, or an integer to wrap the text to.
Returns
string
See Also
Links
Property Detail
$OptionParser ¶ public
An instance of ConsoleOptionParser that has been configured for this class.
Type
Cake\Console\ConsoleOptionParser
$modelClass ¶ public
This object's primary model class name. Should be a plural form. CakePHP will not inflect the name.
Example: For an object named 'Comments', the modelClass would be 'Comments'.
Plugin classes should use Plugin.Comments
style names to correctly load
models from the correct plugin.
Type
string
$plugin ¶ public
The name of the plugin the shell belongs to. Is automatically set by ShellDispatcher when a shell is constructed.
Type
string