Class CommandRunner
Run CLI commands for the provided application.
Property Summary
-
$_eventClass protected
stringDefault class name for new event objects.
-
$_eventManager protected
Cake\Event\EventManagerInterfaceInstance of the Cake\Event\EventManager this object is using to dispatch inner events.
-
$aliases protected
string[]Alias mappings.
-
$app protected
Cake\Core\ConsoleApplicationInterfaceThe application console commands are being run for.
-
$factory protected
Cake\Console\CommandFactoryInterfaceThe application console commands are being run for.
-
$root protected
stringThe root command name. Defaults to
cake.
Method Summary
-
__construct() public
Constructor
-
bootstrap() protected
Application bootstrap wrapper.
Calls the application's
bootstrap()hook. After the application the plugins are bootstrapped. -
createCommand() protected
The wrapper for creating shell instances.
-
dispatchEvent() public
Wrapper for creating and dispatching events.
Returns a dispatched event.
-
getCommand() protected
Get the shell instance for a given command name
-
getEventManager() public
Get the application's event manager or the global one.
-
loadRoutes() protected
Ensure that the application's routes are loaded.
Console commands and shells often need to generate URLs.
-
longestCommandName() protected
Build the longest command name that exists in the collection
Build the longest command name that matches a defined command. This will traverse a maximum of 3 tokens.
-
resolveName() protected
Resolve the command name into a name that exists in the collection.
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.
-
run() public
Run the command contained in $argv.
Use the application to do the following:
- Bootstrap the application
- Create the CommandCollection using the console() hook on the application.
- Trigger the
Console.buildCommandsevent of auto-wiring plugins. - Run the requested command.
-
runCommand() protected
Execute a Command class.
-
runShell() protected
Execute a Shell class.
-
setAliases() public
Replace the entire alias map for a runner.
Aliases allow you to define alternate names for commands in the collection. This can be useful to add top level switches like
--versionor-hUsage
$runner->setAliases(['--version' => 'version']); -
setEventManager() public
Get/set the application's event manager.
If the application does not support events and this method is used as a setter, an exception will be raised.
Method Detail
__construct() ¶ public
__construct(Cake\Core\ConsoleApplicationInterface $app, string $root = 'cake', Cake\Console\CommandFactoryInterface|null $factory = null)
Constructor
Parameters
-
Cake\Core\ConsoleApplicationInterface$app The application to run CLI commands for.
-
string$root optional The root command name to be removed from argv.
-
Cake\Console\CommandFactoryInterface|null$factory optional Command factory instance.
bootstrap() ¶ protected
bootstrap(): void
Application bootstrap wrapper.
Calls the application's bootstrap() hook. After the application the
plugins are bootstrapped.
Returns
voidcreateCommand() ¶ protected
createCommand(string $className, Cake\Console\ConsoleIo $io): Cake\Console\Shell|Cake\Console\CommandInterface
The wrapper for creating shell instances.
Parameters
-
string$className Shell class name.
-
Cake\Console\ConsoleIo$io The IO wrapper for the created shell class.
Returns
Cake\Console\Shell|Cake\Console\CommandInterfacedispatchEvent() ¶ public
dispatchEvent(string $name, array|null $data = null, object|null $subject = null): Cake\Event\EventInterface
Wrapper for creating and dispatching events.
Returns a dispatched event.
Parameters
-
string$name Name of the event.
-
array|null$data optional Any value you wish to be transported with this event to it can be read by listeners.
-
object|null$subject optional The object that this event applies to ($this by default).
Returns
Cake\Event\EventInterfacegetCommand() ¶ protected
getCommand(Cake\Console\ConsoleIo $io, Cake\Console\CommandCollection $commands, string $name): Cake\Console\Shell|Cake\Console\CommandInterface
Get the shell instance for a given command name
Parameters
-
Cake\Console\ConsoleIo$io The IO wrapper for the created shell class.
-
Cake\Console\CommandCollection$commands The command collection to find the shell in.
-
string$name The command name to find
Returns
Cake\Console\Shell|Cake\Console\CommandInterfacegetEventManager() ¶ public
getEventManager(): Cake\Event\EventManagerInterface
Get the application's event manager or the global one.
Returns
Cake\Event\EventManagerInterfaceloadRoutes() ¶ protected
loadRoutes(): void
Ensure that the application's routes are loaded.
Console commands and shells often need to generate URLs.
Returns
voidlongestCommandName() ¶ protected
longestCommandName(Cake\Console\CommandCollection $commands, array $argv): array
Build the longest command name that exists in the collection
Build the longest command name that matches a defined command. This will traverse a maximum of 3 tokens.
Parameters
-
Cake\Console\CommandCollection$commands The command collection to check.
-
array$argv The CLI arguments.
Returns
arrayAn array of the resolved name and modified argv.
resolveName() ¶ protected
resolveName(Cake\Console\CommandCollection $commands, Cake\Console\ConsoleIo $io, string|null $name): string
Resolve the command name into a name that exists in the collection.
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
-
Cake\Console\CommandCollection$commands The command collection to check.
-
Cake\Console\ConsoleIo$io ConsoleIo object for errors.
-
string|null$name The name from the CLI args.
Returns
stringThe resolved name.
Throws
Cake\Console\Exception\MissingOptionExceptionrun() ¶ public
run(array $argv, Cake\Console\ConsoleIo|null $io = null): int
Run the command contained in $argv.
Use the application to do the following:
- Bootstrap the application
- Create the CommandCollection using the console() hook on the application.
- Trigger the
Console.buildCommandsevent of auto-wiring plugins. - Run the requested command.
Parameters
-
array$argv The arguments from the CLI environment.
-
Cake\Console\ConsoleIo|null$io optional The ConsoleIo instance. Used primarily for testing.
Returns
intThe exit code of the command.
Throws
RuntimeExceptionrunCommand() ¶ protected
runCommand(Cake\Console\CommandInterface $command, array $argv, Cake\Console\ConsoleIo $io): int|null
Execute a Command class.
Parameters
-
Cake\Console\CommandInterface$command The command to run.
-
array$argv The CLI arguments to invoke.
-
Cake\Console\ConsoleIo$io The console io
Returns
int|nullExit code
runShell() ¶ protected
runShell(Cake\Console\Shell $shell, array $argv): int|bool|null
Execute a Shell class.
Parameters
-
Cake\Console\Shell$shell The shell to run.
-
array$argv The CLI arguments to invoke.
Returns
int|bool|nullExit code
setAliases() ¶ public
setAliases(string[] $aliases): $this
Replace the entire alias map for a runner.
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
-
string[]$aliases The map of aliases to replace.
Returns
$thissetEventManager() ¶ public
setEventManager(Cake\Event\EventManagerInterface $events): $this
Get/set the application's event manager.
If the application does not support events and this method is used as a setter, an exception will be raised.
Parameters
-
Cake\Event\EventManagerInterface$events The event manager to set.
Returns
$thisThrows
InvalidArgumentExceptionProperty Detail
$_eventManager ¶ protected
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Type
Cake\Event\EventManagerInterface$app ¶ protected
The application console commands are being run for.
Type
Cake\Core\ConsoleApplicationInterface$factory ¶ protected
The application console commands are being run for.
Type
Cake\Console\CommandFactoryInterface