BaseCommand
in package
implements
CommandInterface
Base class for console commands.
Provides hooks for common command features:
-
initialize
Acts as a post-construct hook. -
buildOptionParser
Build/Configure the option parser for your command. -
execute
Execute your command with parsed Arguments and ConsoleIo
Table of Contents
Interfaces
- CommandInterface
- Describe the interface between a command and the surrounding console libraries.
Properties
- $name : string
- The name of this command.
Methods
- abort() : void
- Halt the the current process with a StopException.
- defaultName() : string
- Get the command name.
- execute() : int|null|void
- Implement this method with your command's logic.
- executeCommand() : int|null
- Execute another command with the provided set of arguments.
- getDescription() : string
- Get the command description.
- getName() : string
- Get the command name.
- getOptionParser() : ConsoleOptionParser
- Get the option parser.
- getRootName() : string
- Get the root command name.
- initialize() : void
- Hook method invoked by CakePHP when a command is about to be executed.
- run() : int|null
- Run the command.
- setName() : $this
- Set the name this command uses in the collection.
- buildOptionParser() : ConsoleOptionParser
- Hook method for defining this command's option parser.
- displayHelp() : void
- Output help content
- setOutputLevel() : void
- Set the output level based on the Arguments.
Properties
$name
The name of this command.
protected
string
$name
= 'cake unknown'
Methods
abort()
Halt the the current process with a StopException.
public
abort([int $code = self::CODE_ERROR ]) : void
Parameters
- $code : int = self::CODE_ERROR
-
The exit code to use.
Tags
defaultName()
Get the command name.
public
static defaultName() : string
Returns the command name based on class name.
For e.g. for a command with class name UpdateTableCommand
the default
name returned would be 'update_table'
.
Return values
stringexecute()
Implement this method with your command's logic.
public
abstract execute(Arguments $args, ConsoleIo $io) : int|null|void
Parameters
Return values
int|null|void —The exit code or null for success
executeCommand()
Execute another command with the provided set of arguments.
public
executeCommand(CommandInterface|string $command[, array<string|int, mixed> $args = [] ][, ConsoleIo|null $io = null ]) : int|null
If you are using a string command name, that command's dependencies will not be resolved with the application container. Instead you will need to pass the command as an object with all of its dependencies.
Parameters
- $command : CommandInterface|string
-
The command class name or command instance.
- $args : array<string|int, mixed> = []
-
The arguments to invoke the command with.
- $io : ConsoleIo|null = null
-
The ConsoleIo instance to use for the executed command.
Return values
int|null —The exit code or null for success of the command.
getDescription()
Get the command description.
public
static getDescription() : string
Return values
stringgetName()
Get the command name.
public
getName() : string
Return values
stringgetOptionParser()
Get the option parser.
public
getOptionParser() : ConsoleOptionParser
You can override buildOptionParser() to define your options & arguments.
Tags
Return values
ConsoleOptionParsergetRootName()
Get the root command name.
public
getRootName() : string
Return values
stringinitialize()
Hook method invoked by CakePHP when a command is about to be executed.
public
initialize() : void
Override this method and implement expensive/important setup steps that should not run on every command run. This method will be called before the options and arguments are validated and processed.
run()
Run the command.
public
run(array<string|int, mixed> $argv, ConsoleIo $io) : int|null
Parameters
- $argv : array<string|int, mixed>
-
Arguments from the CLI environment.
- $io : ConsoleIo
-
The console io
Tags
Return values
int|null —Exit code or null for success.
setName()
Set the name this command uses in the collection.
public
setName(string $name) : $this
Parameters
- $name : string
-
The name the command uses in the collection.
Tags
Return values
$thisbuildOptionParser()
Hook method for defining this command's option parser.
protected
buildOptionParser(ConsoleOptionParser $parser) : ConsoleOptionParser
Parameters
- $parser : ConsoleOptionParser
-
The parser to be defined
Return values
ConsoleOptionParser —The built parser.
displayHelp()
Output help content
protected
displayHelp(ConsoleOptionParser $parser, Arguments $args, ConsoleIo $io) : void
Parameters
- $parser : ConsoleOptionParser
-
The option parser.
- $args : Arguments
-
The command arguments.
- $io : ConsoleIo
-
The console io
setOutputLevel()
Set the output level based on the Arguments.
protected
setOutputLevel(Arguments $args, ConsoleIo $io) : void