PHP Class Symfony\Component\Console\Command\Command

Show file Open project: symfony/symfony Class Usage Examples

Public Methods

Method Description
__construct ( string | null $name = null ) Constructor.
addArgument ( string $name, integer $mode = null, string $description = '', mixed $default = null ) : Command Adds an argument.
addOption ( string $name, string $shortcut = null, integer $mode = null, string $description = '', mixed $default = null ) : Command Adds an option.
addUsage ( string $usage ) : Command Add a command usage example.
getAliases ( ) : array Returns the aliases for the command.
getApplication ( ) : Application Gets the application instance for this command.
getDefinition ( ) : Symfony\Component\Console\Input\InputDefinition Gets the InputDefinition attached to this Command.
getDescription ( ) : string Returns the description for the command.
getHelp ( ) : string Returns the help for the command.
getHelper ( string $name ) : mixed Gets a helper instance by name.
getHelperSet ( ) : Symfony\Component\Console\Helper\HelperSet Gets the helper set.
getName ( ) : string Returns the command name.
getNativeDefinition ( ) : Symfony\Component\Console\Input\InputDefinition Gets the InputDefinition to be used to create representations of this Command.
getProcessedHelp ( ) : string Returns the processed help for the command replacing the %command.name% and %command.full_name% patterns with the real values dynamically.
getSynopsis ( boolean $short = false ) : string Returns the synopsis for the command.
getUsages ( ) : array Returns alternative usages of the command.
ignoreValidationErrors ( ) Ignores validation errors.
isEnabled ( ) : boolean Checks whether the command is enabled or not in the current environment.
isHidden ( ) : boolean
mergeApplicationDefinition ( boolean $mergeArgs = true ) Merges the application definition with the command definition.
run ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) : integer Runs the command.
setAliases ( string[] $aliases ) : Command Sets the aliases for the command.
setApplication ( Application $application = null ) Sets the application instance for this command.
setCode ( callable $code ) : Command Sets the code to execute when running this command.
setDefinition ( array | Symfony\Component\Console\Input\InputDefinition $definition ) : Command Sets an array of argument and option instances.
setDescription ( string $description ) : Command Sets the description for the command.
setHelp ( string $help ) : Command Sets the help for the command.
setHelperSet ( Symfony\Component\Console\Helper\HelperSet $helperSet ) Sets the helper set.
setHidden ( boolean $hidden ) : Command
setName ( string $name ) : Command Sets the name of the command.
setProcessTitle ( string $title ) : Command Sets the process title of the command.

Protected Methods

Method Description
configure ( ) Configures the current command.
execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) : null | integer Executes the current command.
initialize ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) Initializes the command just after the input has been validated.
interact ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) Interacts with the user.

Private Methods

Method Description
validateName ( string $name ) Validates a command name.

Method Details

__construct() public method

Constructor.
public __construct ( string | null $name = null )
$name string | null The name of the command; passing null means it must be set in configure()

addArgument() public method

Adds an argument.
public addArgument ( string $name, integer $mode = null, string $description = '', mixed $default = null ) : Command
$name string The argument name
$mode integer The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
$description string A description text
$default mixed The default value (for InputArgument::OPTIONAL mode only)
return Command The current instance

addOption() public method

Adds an option.
public addOption ( string $name, string $shortcut = null, integer $mode = null, string $description = '', mixed $default = null ) : Command
$name string The option name
$shortcut string The shortcut (can be null)
$mode integer The option mode: One of the InputOption::VALUE_* constants
$description string A description text
$default mixed The default value (must be null for InputOption::VALUE_NONE)
return Command The current instance

addUsage() public method

Add a command usage example.
public addUsage ( string $usage ) : Command
$usage string The usage, it'll be prefixed with the command name
return Command The current instance

configure() protected method

Configures the current command.
protected configure ( )

execute() protected method

This method is not abstract because you can use this class as a concrete class. In this case, instead of defining the execute() method, you set the code to execute by passing a Closure to the setCode() method.
See also: setCode()
protected execute ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) : null | integer
$input Symfony\Component\Console\Input\InputInterface An InputInterface instance
$output Symfony\Component\Console\Output\OutputInterface An OutputInterface instance
return null | integer null or 0 if everything went fine, or an error code

getAliases() public method

Returns the aliases for the command.
public getAliases ( ) : array
return array An array of aliases for the command

getApplication() public method

Gets the application instance for this command.
public getApplication ( ) : Application
return Symfony\Component\Console\Application An Application instance

getDefinition() public method

Gets the InputDefinition attached to this Command.
public getDefinition ( ) : Symfony\Component\Console\Input\InputDefinition
return Symfony\Component\Console\Input\InputDefinition An InputDefinition instance

getDescription() public method

Returns the description for the command.
public getDescription ( ) : string
return string The description for the command

getHelp() public method

Returns the help for the command.
public getHelp ( ) : string
return string The help for the command

getHelper() public method

Gets a helper instance by name.
public getHelper ( string $name ) : mixed
$name string The helper name
return mixed The helper value

getHelperSet() public method

Gets the helper set.
public getHelperSet ( ) : Symfony\Component\Console\Helper\HelperSet
return Symfony\Component\Console\Helper\HelperSet A HelperSet instance

getName() public method

Returns the command name.
public getName ( ) : string
return string The command name

getNativeDefinition() public method

Can be overridden to provide the original command representation when it would otherwise be changed by merging with the application InputDefinition. This method is not part of public API and should not be used directly.
public getNativeDefinition ( ) : Symfony\Component\Console\Input\InputDefinition
return Symfony\Component\Console\Input\InputDefinition An InputDefinition instance

getProcessedHelp() public method

Returns the processed help for the command replacing the %command.name% and %command.full_name% patterns with the real values dynamically.
public getProcessedHelp ( ) : string
return string The processed help for the command

getSynopsis() public method

Returns the synopsis for the command.
public getSynopsis ( boolean $short = false ) : string
$short boolean Whether to show the short version of the synopsis (with options folded) or not
return string The synopsis

getUsages() public method

Returns alternative usages of the command.
public getUsages ( ) : array
return array

ignoreValidationErrors() public method

This is mainly useful for the help command.

initialize() protected method

This is mainly useful when a lot of commands extends one main command where some things need to be initialized based on the input arguments and options.
protected initialize ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface An InputInterface instance
$output Symfony\Component\Console\Output\OutputInterface An OutputInterface instance

interact() protected method

This method is executed before the InputDefinition is validated. This means that this is the only place where the command can interactively ask for values of missing required arguments.
protected interact ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface An InputInterface instance
$output Symfony\Component\Console\Output\OutputInterface An OutputInterface instance

isEnabled() public method

Override this to check for x or y and return false if the command can not run properly under the current conditions.
public isEnabled ( ) : boolean
return boolean

isHidden() public method

public isHidden ( ) : boolean
return boolean Whether the command should be publicly shown or not.

mergeApplicationDefinition() public method

This method is not part of public API and should not be used directly.
public mergeApplicationDefinition ( boolean $mergeArgs = true )
$mergeArgs boolean Whether to merge or not the Application definition arguments to Command definition arguments

run() public method

The code to execute is either defined directly with the setCode() method or by overriding the execute() method in a sub-class.
See also: setCode()
See also: execute()
public run ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output ) : integer
$input Symfony\Component\Console\Input\InputInterface An InputInterface instance
$output Symfony\Component\Console\Output\OutputInterface An OutputInterface instance
return integer The command exit code

setAliases() public method

Sets the aliases for the command.
public setAliases ( string[] $aliases ) : Command
$aliases string[] An array of aliases for the command
return Command The current instance

setApplication() public method

Sets the application instance for this command.
public setApplication ( Application $application = null )
$application Symfony\Component\Console\Application An Application instance

setCode() public method

If this method is used, it overrides the code defined in the execute() method.
See also: execute()
public setCode ( callable $code ) : Command
$code callable A callable(InputInterface $input, OutputInterface $output)
return Command The current instance

setDefinition() public method

Sets an array of argument and option instances.
public setDefinition ( array | Symfony\Component\Console\Input\InputDefinition $definition ) : Command
$definition array | Symfony\Component\Console\Input\InputDefinition An array of argument and option instances or a definition instance
return Command The current instance

setDescription() public method

Sets the description for the command.
public setDescription ( string $description ) : Command
$description string The description for the command
return Command The current instance

setHelp() public method

Sets the help for the command.
public setHelp ( string $help ) : Command
$help string The help for the command
return Command The current instance

setHelperSet() public method

Sets the helper set.
public setHelperSet ( Symfony\Component\Console\Helper\HelperSet $helperSet )
$helperSet Symfony\Component\Console\Helper\HelperSet A HelperSet instance

setHidden() public method

public setHidden ( boolean $hidden ) : Command
$hidden boolean Whether or not the command should be hidden from the list of commands
return Command The current instance

setName() public method

This method can set both the namespace and the name if you separate them by a colon (:) $command->setName('foo:bar');
public setName ( string $name ) : Command
$name string The command name
return Command The current instance

setProcessTitle() public method

This feature should be used only when creating a long process command, like a daemon. PHP 5.5+ or the proctitle PECL library is required
public setProcessTitle ( string $title ) : Command
$title string The process title
return Command The current instance