Fr3nch13/CakePHP Utilities

Command extends BaseCommand
in package
uses LocatorAwareTrait, LogTrait, ModelAwareTrait

Base class for commands using the full stack CakePHP Framework.

Includes traits that integrate logging and ORM models to console commands.

Attributes
#[AllowDynamicProperties]

Table of Contents

Properties

$_modelFactories  : array<string|int, callable|LocatorInterface>
A list of overridden model factory functions.
$_modelType  : string
The model type to use.
$_tableLocator  : LocatorInterface|null
Table locator instance
$defaultTable  : string|null
This object's default table alias.
$modelClass  : string|null
This object's primary model class name. Should be a plural form.
$name  : string
The name of this command.

Methods

__construct()  : mixed
Constructor
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.
fetchTable()  : Table
Convenience method to get a table instance.
getDescription()  : string
Get the command description.
getModelType()  : string
Get the model type to be used by this class
getName()  : string
Get the command name.
getOptionParser()  : ConsoleOptionParser
Get the option parser.
getRootName()  : string
Get the root command name.
getTableLocator()  : LocatorInterface
Gets the table locator.
initialize()  : void
Hook method invoked by CakePHP when a command is about to be executed.
loadModel()  : RepositoryInterface
Loads and constructs repository objects required by this object
log()  : bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
modelFactory()  : void
Override a existing callable to generate repositories of a given type.
run()  : int|null
Run the command.
setModelType()  : $this
Set the model type to be used by this class
setName()  : $this
Set the name this command uses in the collection.
setTableLocator()  : $this
Sets the table locator.
_setModelClass()  : void
Set the modelClass property based on conventions.
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

$_modelType

The model type to use.

protected string $_modelType = 'Table'

$defaultTable

This object's default table alias.

protected string|null $defaultTable = null

$modelClass

This object's primary model class name. Should be a plural form.

Use Cake\ORM\Locator\LocatorAwareTrait::$defaultTable instead.

protected string|null $modelClass

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.

$name

The name of this command.

protected string $name = 'cake unknown'

Methods

__construct()

Constructor

public __construct() : mixed

By default CakePHP will construct command objects when building the CommandCollection for your application.

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
throws
StopException
psalm-return

never-return

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
string

execute()

Implement this method with your command's logic.

public execute(Arguments $args, ConsoleIo $io) : int|null|void
Parameters
$args : Arguments

The command arguments.

$io : ConsoleIo

The console io

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.

fetchTable()

Convenience method to get a table instance.

public fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
$alias : string|null = null

The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.

$options : array<string, mixed> = []

The options you want to build the table with. If a table has already been loaded the registry options will be ignored.

Tags
throws
CakeException

If $alias argument and $defaultTable property both are null.

see
TableLocator::get()
since
4.3.0
Return values
Table

getDescription()

Get the command description.

public static getDescription() : string
Return values
string

getModelType()

Get the model type to be used by this class

public getModelType() : string
Return values
string

getName()

Get the command name.

public getName() : string
Return values
string

getOptionParser()

Get the option parser.

public getOptionParser() : ConsoleOptionParser

You can override buildOptionParser() to define your options & arguments.

Tags
throws
RuntimeException

When the parser is invalid

Return values
ConsoleOptionParser

getRootName()

Get the root command name.

public getRootName() : string
Return values
string

initialize()

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.

loadModel()

Loads and constructs repository objects required by this object

public loadModel([string|null $modelClass = null ][, string|null $modelType = null ]) : RepositoryInterface

Use LocatorAwareTrait::fetchTable() instead.

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
$modelClass : string|null = null

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.

$modelType : string|null = null

The type of repository to load. Defaults to the getModelType() value.

Tags
throws
MissingModelException

If the model class cannot be found.

throws
UnexpectedValueException

If $modelClass argument is not provided and ModelAwareTrait::$modelClass property value is empty.

Return values
RepositoryInterface

The model instance created.

log()

Convenience method to write a message to Log. See Log::write() for more information on writing to logs.

public log(string $message[, string|int $level = LogLevel::ERROR ][, array<string|int, mixed>|string $context = [] ]) : bool
Parameters
$message : string

Log message.

$level : string|int = LogLevel::ERROR

Error level.

$context : array<string|int, mixed>|string = []

Additional log data relevant to this message.

Return values
bool

Success of log write.

modelFactory()

Override a existing callable to generate repositories of a given type.

public modelFactory(string $type, LocatorInterface|callable $factory) : void
Parameters
$type : string

The name of the repository type the factory function is for.

$factory : LocatorInterface|callable

The factory function used to create instances.

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
inheritDoc
Return values
int|null

Exit code or null for success.

setModelType()

Set the model type to be used by this class

public setModelType(string $modelType) : $this
Parameters
$modelType : string

The model type

Return values
$this

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
inheritDoc
Return values
$this

_setModelClass()

Set the modelClass property based on conventions.

protected _setModelClass(string $name) : void

If the property is already set it will not be overwritten

Parameters
$name : string

Class name.


        
On this page

Search results