Base class for command-line utilities for automating programmer chores.
Is the equivalent of Cake\Controller\Controller on the command line.
int 1
Default error code
int 0
Default success code
int ConsoleIo::NORMAL
Output constant for making normal shells.
int ConsoleIo::QUIET
Output constants for making quiet shells.
int ConsoleIo::VERBOSE
Output constant making verbose shells.
Cake\Console\ConsoleOptionParserAn instance of ConsoleOptionParser that has been configured for this class.
Cake\Console\TaskRegistryTask Collection for the command, used to create Tasks.
Cake\Console\ConsoleIoConsoleIo instance.
array<callableCake\Datasource\Locator\LocatorInterface>A list of overridden model factory functions.
stringThe model type to use.
Cake\ORM\Locator\LocatorInterface|nullTable locator instance
array<string, array>Normalized map of tasks.
arrayContains arguments parsed from the command line.
string|nullThe command (method/task) that is being run.
string|nullThis object's default table alias.
boolIf true, the script will ask for permission to perform actions.
string|nullThis object's primary model class name. Should be a plural form. CakePHP will not inflect the name.
stringThe name of the shell in camelized.
arrayContains command switches parsed from the command line.
stringThe name of the plugin the shell belongs to. Is automatically set by ShellDispatcher when a shell is constructed.
stringThe root command name used when generating help output.
array<string>Contains the loaded tasks
array|boolContains tasks to load and instantiate
Constructs this Shell instance.
Returns an array that can be used to describe the internal state of this object.
Overload get for lazy building of tasks
Display the help in the correct format
Merge a single property with the values declared in all parent classes.
Merge each of the keys in a property together.
Merge the list of $properties with all parent classes of the current class.
Set the modelClass property based on conventions.
Set the output level based on the parameters.
Stop execution of the current script. Raises a StopException to try and halt the execution.
Checks that the tasks in the task map are actually available
Displays a header for the shell
Displays a formatted error message and exits the application with an error code.
Clear the console
Creates a file at given path
Dispatch a command to another Shell. Similar to Object::requestAction() but intended for running shells from other shells.
Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
Convenience method to get a table instance.
Get the io object for this shell.
Get the model type to be used by this class
Gets the option parser instance and configures it.
Gets the table locator.
Check to see if this shell has a callable method by the given name.
Check to see if this shell has a task with the provided name.
Render a Console Helper
Outputs a series of minus characters to the standard output, acts as a visual separator.
Prompts the user for input, and returns it.
Convenience method for out() that wraps message between tag
Initializes the Shell acts as constructor for subclasses allows configuration of tasks prior to shell execution
Loads and constructs repository objects required by this object
Loads tasks defined in public $tasks
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Main entry method for the shell.
Override a existing callable to generate repositories of a given type.
Returns a single or multiple linefeeds sequences.
Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
Safely access the values in $this->params.
Parses the arguments for the dispatchShell() method.
Output at all levels.
Runs the Shell with the provided argv.
Set the io object for this shell.
Set the model type to be used by this class
Set the root command name for help output.
Sets the table locator.
Makes absolute file path easier to read
Starts up the Shell and displays the welcome message. Allows for checking and configuring prior to command or main execution
Convenience method for out() that wraps message between tag
Output at the verbose level.
Convenience method for err() that wraps message between tag
Wrap a block of text. Allows you to set the width, and indenting on a block of text.
__construct(Cake\Console\ConsoleIo|null $io = null, Cake\ORM\Locator\LocatorInterface|null $locator = null)
Constructs this Shell instance.
Cake\Console\ConsoleIo|null $io optional An io instance.
Cake\ORM\Locator\LocatorInterface|null $locator optional Table locator instance.
__debugInfo(): array<string, mixed>
Returns an array that can be used to describe the internal state of this object.
array<string, mixed>__get(string $name): Cake\Console\Shell
Overload get for lazy building of tasks
string $name The task to get.
Cake\Console\Shell_displayHelp(string|null $command = null): int|null
Display the help in the correct format
string|null $command optional The command to get help for.
int|null_mergeProperty(string $property, array<string> $parentClasses, array<string, mixed> $options): void
Merge a single property with the values declared in all parent classes.
string $property The name of the property being merged.
array<string> $parentClasses An array of classes you want to merge with.
array<string, mixed> $options Options for merging the property, see _mergeVars()
void_mergePropertyData(array $current, array $parent, bool $isAssoc): array
Merge each of the keys in a property together.
array $current The current merged value.
array $parent The parent class' value.
bool $isAssoc Whether the merging should be done in associative mode.
array_mergeVars(array<string> $properties, array<string, mixed> $options = []): void
Merge the list of $properties with all parent classes of the current class.
associative - A list of properties that should be treated as associative arrays. Properties in this list will be passed through Hash::normalize() before merging.array<string> $properties An array of properties and the merge strategy for them.
array<string, mixed> $options optional The options to use when merging properties.
void_setModelClass(string $name): void
Set the modelClass property based on conventions.
If the property is already set it will not be overwritten
string $name Class name.
void_setOutputLevel(): void
Set the output level based on the parameters.
This reconfigures both the output level for out() and the configured stdout/stderr logging
void_stop(int $status = self::CODE_SUCCESS): void
Stop execution of the current script. Raises a StopException to try and halt the execution.
int $status optional see https://secure.php.net/exit for values
voidCake\Console\Exception\StopException_validateTasks(): void
Checks that the tasks in the task map are actually available
voidRuntimeException_welcome(): void
Displays a header for the shell
voidabort(string $message, int $exitCode = self::CODE_ERROR): void
Displays a formatted error message and exits the application with an error code.
string $message The error message
int $exitCode optional The exit code for the shell task.
voidCake\Console\Exception\StopExceptionclear(): void
Clear the console
voidcreateFile(string $path, string $contents): bool
Creates a file at given path
string $path Where to put the file.
string $contents Content to put in the file.
booldispatchShell(): int
Dispatch a command to another Shell. Similar to Object::requestAction() but intended for running shells from other shells.
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'] ]);
interr(array<string>|string $message, int $newlines = 1): int
Outputs a single or multiple error messages to stderr. If no parameters are passed outputs just a newline.
array<string>|string $message A string or an array of strings to output
int $newlines optional Number of newlines to append
intfetchTable(string|null $alias = null, array<string, mixed> $options = []): Cake\ORM\Table
Convenience method to get a table instance.
string|null $alias optional The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.
array<string, mixed> $options optional The options you want to build the table with. If a table has already been loaded the registry options will be ignored.
Cake\ORM\TableCake\Core\Exception\CakeExceptiongetIo(): Cake\Console\ConsoleIo
Get the io object for this shell.
Cake\Console\ConsoleIogetModelType(): string
Get the model type to be used by this class
stringgetOptionParser(): Cake\Console\ConsoleOptionParser
Gets the option parser instance and configures it.
By overriding this method you can configure the ConsoleOptionParser before returning it.
Cake\Console\ConsoleOptionParsergetTableLocator(): Cake\ORM\Locator\LocatorInterface
Gets the table locator.
Cake\ORM\Locator\LocatorInterfacehasMethod(string $name): bool
Check to see if this shell has a callable method by the given name.
string $name The method name to check.
boolhasTask(string $task): bool
Check to see if this shell has a task with the provided name.
string $task The task name to check.
boolhelper(string $name, array<string, mixed> $config = []): Cake\Console\Helper
Render a Console Helper
Create and render the output for a helper object. If the helper object has not already been loaded, it will be loaded and constructed.
string $name The name of the helper to render
array<string, mixed> $config optional Configuration data for the helper.
Cake\Console\Helperhr(int $newlines = 0, int $width = 63): void
Outputs a series of minus characters to the standard output, acts as a visual separator.
int $newlines optional Number of newlines to pre- and append
int $width optional Width of the line, defaults to 63
voidin(string $prompt, array<string>|string|null $options = null, string|null $default = null): string|null
Prompts the user for input, and returns it.
string $prompt Prompt text.
array<string>|string|null $options optional Array or string of options.
string|null $default optional Default input value.
string|nullinfo(array<string>|string $message, int $newlines = 1, int $level = Shell::NORMAL): int|null
Convenience method for out() that wraps message between tag
array<string>|string $message 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.
int|nullinitialize(): void
Initializes the Shell acts as constructor for subclasses allows configuration of tasks prior to shell execution
voidloadModel(string|null $modelClass = null, string|null $modelType = null): Cake\Datasource\RepositoryInterface
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.
string|null $modelClass optional Name of model class to load. Defaults to $this->modelClass. The name can be an alias like 'Post' or FQCN like App\Model\Table\PostsTable::class.
string|null $modelType optional The type of repository to load. Defaults to the getModelType() value.
Cake\Datasource\RepositoryInterfaceCake\Datasource\Exception\MissingModelExceptionUnexpectedValueExceptionloadTasks(): true
Loads tasks defined in public $tasks
truelog(string $message, string|int $level = LogLevel::ERROR, array|string $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
string $message Log message.
string|int $level optional Error level.
array|string $context optional Additional log data relevant to this message.
boolmain(mixed ...$args): int|bool|null|void
Main entry method for the shell.
int|bool|null|voidmodelFactory(string $type, Cake\Datasource\Locator\LocatorInterface|callable $factory): void
Override a existing callable to generate repositories of a given type.
string $type The name of the repository type the factory function is for.
Cake\Datasource\Locator\LocatorInterface|callable $factory The factory function used to create instances.
voidnl(int $multiplier = 1): string
Returns a single or multiple linefeeds sequences.
int $multiplier optional Number of times the linefeed sequence should be repeated
stringout(array<string>|string $message, int $newlines = 1, int $level = Shell::NORMAL): int|null
Outputs a single or multiple messages to stdout. If no parameters are passed outputs just a newline.
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.
array<string>|string $message 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.
int|nullparam(string $name): string|bool|null
Safely access the values in $this->params.
string $name The name of the parameter to get.
string|bool|nullparseDispatchArguments(array $args): array
Parses the arguments for the dispatchShell() method.
array $args Arguments fetch from the dispatchShell() method with func_get_args()
arrayquiet(array<string>|string $message, int $newlines = 1): int|null
Output at all levels.
array<string>|string $message A string or an array of strings to output
int $newlines optional Number of newlines to append
int|nullrunCommand(array $argv, bool $autoMethod = false, array $extra = []): int|bool|null
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:
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.
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.
array $extra optional Extra parameters that you can manually pass to the Shell to be dispatched. Built-in extra parameter is :
int|bool|nullsetIo(Cake\Console\ConsoleIo $io): void
Set the io object for this shell.
Cake\Console\ConsoleIo $io The ConsoleIo object to use.
voidsetModelType(string $modelType): $this
Set the model type to be used by this class
string $modelType The model type
$thissetRootName(string $name): $this
Set the root command name for help output.
string $name The name of the root command.
$thissetTableLocator(Cake\ORM\Locator\LocatorInterface $tableLocator): $this
Sets the table locator.
Cake\ORM\Locator\LocatorInterface $tableLocator LocatorInterface instance.
$thisshortPath(string $file): string
Makes absolute file path easier to read
string $file Absolute file path
stringstartup(): 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.
voidsuccess(array<string>|string $message, int $newlines = 1, int $level = Shell::NORMAL): int|null
Convenience method for out() that wraps message between tag
array<string>|string $message 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.
int|nullverbose(array<string>|string $message, int $newlines = 1): int|null
Output at the verbose level.
array<string>|string $message A string or an array of strings to output
int $newlines optional Number of newlines to append
int|nullwarn(array<string>|string $message, int $newlines = 1): int
Convenience method for err() that wraps message between tag
array<string>|string $message A string or an array of strings to output
int $newlines optional Number of newlines to append
intwrapText(string $text, array<string, mixed>|int $options = []): string
Wrap a block of text. Allows you to set the width, and indenting on a block of text.
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.string $text Text the text to format.
array<string, mixed>|int $options optional Array of options to use, or an integer to wrap the text to.
stringAn instance of ConsoleOptionParser that has been configured for this class.
Cake\Console\ConsoleOptionParserTask Collection for the command, used to create Tasks.
Cake\Console\TaskRegistryConsoleIo instance.
Cake\Console\ConsoleIoA list of overridden model factory functions.
array<callableCake\Datasource\Locator\LocatorInterface>The model type to use.
stringTable locator instance
Cake\ORM\Locator\LocatorInterface|nullNormalized map of tasks.
array<string, array>Contains arguments parsed from the command line.
arrayThe command (method/task) that is being run.
string|nullThis object's default table alias.
string|nullIf true, the script will ask for permission to perform actions.
boolThis 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.
Use empty string to not use auto-loading on this object. Null auto-detects based on controller name.
string|nullThe name of the shell in camelized.
stringContains command switches parsed from the command line.
arrayThe name of the plugin the shell belongs to. Is automatically set by ShellDispatcher when a shell is constructed.
stringThe root command name used when generating help output.
stringContains the loaded tasks
array<string>Contains tasks to load and instantiate
array|bool
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Console.Shell.html