CommandRunner
in package
implements
EventDispatcherInterface
uses
EventDispatcherTrait
Run CLI commands for the provided application.
Table of Contents
Interfaces
- EventDispatcherInterface
- Objects implementing this interface can emit events.
Properties
- $_eventClass : string
- Default class name for new event objects.
- $_eventManager : EventManagerInterface|null
- Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
- $aliases : array<string|int, string>
- Alias mappings.
- $app : ConsoleApplicationInterface
- The application console commands are being run for.
- $factory : CommandFactoryInterface|null
- The application console commands are being run for.
- $root : string
- The root command name. Defaults to `cake`.
Methods
- __construct() : mixed
- Constructor
- dispatchEvent() : EventInterface
- Wrapper for creating and dispatching events.
- getEventManager() : EventManagerInterface
- Get the application's event manager or the global one.
- run() : int
- Run the command contained in $argv.
- setAliases() : $this
- Replace the entire alias map for a runner.
- setEventManager() : $this
- Get/set the application's event manager.
- bootstrap() : void
- Application bootstrap wrapper.
- createCommand() : CommandInterface|Shell
- The wrapper for creating shell instances.
- getCommand() : CommandInterface|Shell
- Get the shell instance for a given command name
- loadRoutes() : void
- Ensure that the application's routes are loaded.
- longestCommandName() : array<string|int, mixed>
- Build the longest command name that exists in the collection
- resolveName() : string
- Resolve the command name into a name that exists in the collection.
- runCommand() : int|null
- Execute a Command class.
- runShell() : int|bool|null
- Execute a Shell class.
Properties
$_eventClass
Default class name for new event objects.
protected
string
$_eventClass
= \Cake\Event\Event::class
$_eventManager
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
protected
EventManagerInterface|null
$_eventManager
$aliases
Alias mappings.
protected
array<string|int, string>
$aliases
= []
$app
The application console commands are being run for.
protected
ConsoleApplicationInterface
$app
$factory
The application console commands are being run for.
protected
CommandFactoryInterface|null
$factory
$root
The root command name. Defaults to `cake`.
protected
string
$root
Methods
__construct()
Constructor
public
__construct(ConsoleApplicationInterface $app[, string $root = 'cake' ][, CommandFactoryInterface|null $factory = null ]) : mixed
Parameters
- $app : ConsoleApplicationInterface
-
The application to run CLI commands for.
- $root : string = 'cake'
-
The root command name to be removed from argv.
- $factory : CommandFactoryInterface|null = null
-
Command factory instance.
dispatchEvent()
Wrapper for creating and dispatching events.
public
dispatchEvent(string $name[, array<string|int, mixed>|null $data = null ][, object|null $subject = null ]) : EventInterface
Returns a dispatched event.
Parameters
- $name : string
-
Name of the event.
- $data : array<string|int, mixed>|null = null
-
Any value you wish to be transported with this event to it can be read by listeners.
- $subject : object|null = null
-
The object that this event applies to ($this by default).
Return values
EventInterfacegetEventManager()
Get the application's event manager or the global one.
public
getEventManager() : EventManagerInterface
Return values
EventManagerInterfacerun()
Run the command contained in $argv.
public
run(array<string|int, mixed> $argv[, ConsoleIo|null $io = null ]) : int
Use the application to do the following:
- Bootstrap the application
- Create the CommandCollection using the console() hook on the application.
- Trigger the
Console.buildCommands
event of auto-wiring plugins. - Run the requested command.
Parameters
- $argv : array<string|int, mixed>
-
The arguments from the CLI environment.
- $io : ConsoleIo|null = null
-
The ConsoleIo instance. Used primarily for testing.
Tags
Return values
int —The exit code of the command.
setAliases()
Replace the entire alias map for a runner.
public
setAliases(array<string|int, string> $aliases) : $this
Aliases allow you to define alternate names for commands
in the collection. This can be useful to add top level switches
like --version
or -h
Usage
$runner->setAliases(['--version' => 'version']);
Parameters
- $aliases : array<string|int, string>
-
The map of aliases to replace.
Return values
$thissetEventManager()
Get/set the application's event manager.
public
setEventManager(EventManagerInterface $eventManager) : $this
If the application does not support events and this method is used as a setter, an exception will be raised.
Parameters
- $eventManager : EventManagerInterface
-
The event manager to set.
Tags
Return values
$thisbootstrap()
Application bootstrap wrapper.
protected
bootstrap() : void
Calls the application's bootstrap()
hook. After the application the
plugins are bootstrapped.
createCommand()
The wrapper for creating shell instances.
protected
createCommand(string $className, ConsoleIo $io) : CommandInterface|Shell
Parameters
- $className : string
-
Shell class name.
- $io : ConsoleIo
-
The IO wrapper for the created shell class.
Return values
CommandInterface|ShellgetCommand()
Get the shell instance for a given command name
protected
getCommand(ConsoleIo $io, CommandCollection $commands, string $name) : CommandInterface|Shell
Parameters
- $io : ConsoleIo
-
The IO wrapper for the created shell class.
- $commands : CommandCollection
-
The command collection to find the shell in.
- $name : string
-
The command name to find
Return values
CommandInterface|ShellloadRoutes()
Ensure that the application's routes are loaded.
protected
loadRoutes() : void
Console commands and shells often need to generate URLs.
longestCommandName()
Build the longest command name that exists in the collection
protected
longestCommandName(CommandCollection $commands, array<string|int, mixed> $argv) : array<string|int, mixed>
Build the longest command name that matches a defined command. This will traverse a maximum of 3 tokens.
Parameters
- $commands : CommandCollection
-
The command collection to check.
- $argv : array<string|int, mixed>
-
The CLI arguments.
Return values
array<string|int, mixed> —An array of the resolved name and modified argv.
resolveName()
Resolve the command name into a name that exists in the collection.
protected
resolveName(CommandCollection $commands, ConsoleIo $io, string|null $name) : string
Apply backwards compatible inflections and aliases. Will step forward up to 3 tokens in $argv to generate a command name in the CommandCollection. More specific command names take precedence over less specific ones.
Parameters
- $commands : CommandCollection
-
The command collection to check.
- $io : ConsoleIo
-
ConsoleIo object for errors.
- $name : string|null
-
The name from the CLI args.
Tags
Return values
string —The resolved name.
runCommand()
Execute a Command class.
protected
runCommand(CommandInterface $command, array<string|int, mixed> $argv, ConsoleIo $io) : int|null
Parameters
- $command : CommandInterface
-
The command to run.
- $argv : array<string|int, mixed>
-
The CLI arguments to invoke.
- $io : ConsoleIo
-
The console io
Return values
int|null —Exit code
runShell()
Execute a Shell class.
protected
runShell(Shell $shell, array<string|int, mixed> $argv) : int|bool|null
Parameters
- $shell : Shell
-
The shell to run.
- $argv : array<string|int, mixed>
-
The CLI arguments to invoke.
Return values
int|bool|null —Exit code