BaseCommand

Type
Class
Namespace
Craft
Inherits
Craft\BaseCommand » CConsoleCommand » CComponent
Extended by
Craft\QuerygenCommand
Since
2.0

BaseCommand extends Yii's {@link \CConsoleCommand} and represents an executable console command.

It works like {@link \CController} by parsing command line options and dispatching the request to a specific action with appropriate option values.

Users call a console command via the following command format:

yiic CommandName ActionName --Option1=Value1 --Option2=Value2 ...

Child classes mainly needs to implement various action methods whose name must be prefixed with "action". The parameters to an action method are considered as options for that specific action. The action specified as {@link defaultAction} will be invoked when a user does not specify the action name in his command.

Options are bound to action parameters via parameter names. For example, the following action method will allow us to run a command with yiic sitemap --type=News:

class SitemapCommand extends BaseCommand
{
    public function actionIndex($type)
    {
        ....
    }
}

The return value of action methods will be used as application exit code if it is an integer value.

A Craft plugin can add its own custom commands by adding a 'consolecommands' folder and adding a class that extends BaseCommand.

See also http://craftcms.com

View source

Public Properties

PropertyDescription
$defaultActionstring – The name of the default action.

Public Methods

MethodDescription
__call()Calls the named method which is not a class method.
__construct()Constructor.
__get()Returns a property value, an event handler list or a behavior based on its name.
__isset()Checks if a property value is null.
__set()Sets value of a component property.
__unset()Sets a component property to be null.
asa()Returns the named behavior object.
attachBehavior()Attaches a behavior to this component.
attachBehaviors()Attaches a list of behaviors to the component.
attachEventHandler()Attaches an event handler to an event.
behaviors()Returns a list of behaviors that this command should behave as.
buildFileList()Builds the file list of a directory.
canGetProperty()Determines whether a property can be read.
canSetProperty()Determines whether a property can be set.
confirm()Asks user to confirm by typing y or n.
copyFiles()Copies a list of files from one place to another.
detachBehavior()Detaches a behavior from the component.
detachBehaviors()Detaches all behaviors from the component.
detachEventHandler()Detaches an existing event handler.
disableBehavior()Disables an attached behavior.
disableBehaviors()Disables all behaviors attached to this component.
enableBehavior()Enables an attached behavior.
enableBehaviors()Enables all behaviors attached to this component.
ensureDirectory()Creates all parent directories if they do not exist.
evaluateExpression()Evaluates a PHP expression or callback under the context of this component.
getCommandRunner()
getEventHandlers()Returns the list of attached event handlers for an event.
getHelp()Provides the command description.
getName()
getOptionHelp()Provides the command option help information.
hasEvent()Determines whether an event is defined.
hasEventHandler()Checks whether the named event has attached handlers.
hasProperty()Determines whether a property is defined.
init()Initializes the command object.
onAfterAction()This event is raised after an action finishes execution.
onBeforeAction()This event is raised before an action is to be executed.
pluralize()Converts a word to its plural form.
prompt()Reads input via the readline PHP extension if that's available, or fgets() if readline is not installed.
raiseEvent()Raises an event.
renderFile()Renders a view file.
run()Executes the command.
usageError()Displays a usage error.

Protected Methods

MethodDescription
afterAction()This method is invoked right after an action finishes execution.
beforeAction()This method is invoked right before an action is to be executed.
resolveRequest()Parses the command line arguments and determines which action to perform.