PHP Class Webmozart\Console\Api\Config\CommandConfig

There are two different ways of creating a command configuration: * Call {@link create()} or {@link ApplicationConfig::beginCommand()} and use the fluent interface: php $config = CommandConfig::create() ->setName('server') ->setDescription('List and manage servers') ->beginSubCommand('add') ->setDescription('Add a new server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ... ; * Extend the class and implement the {@link configure()} method: php class ServerCommandConfig extends CommandConfig { protected function configure() { $this ->setName('server') ->setDescription('List and manage servers') ->beginSubCommand('add') ->setDescription('Add a new server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ... ; } } You can choose between two different ways of executing a command: * You can register a callback with {@link setCallback()}. The callback receives the input, the standard output and the error output as arguments: php $config->setCallback( function (InputInterface $input, OutputInterface $output, OutputInterface $errorOutput) { ... } ); * You can implement a custom command handler and return the handler from {@link getHandler()}. Since the command handler is separated, it can be easily tested: php class ServerConfig extends CommandConfig { public function getHandler() { return new ServerHandler(); } }
Since: 1.0
Author: Bernhard Schussek ([email protected])
Inheritance: extends Config
显示文件 Open project: webmozart/console Class Usage Examples

Public Methods

Method Description
__construct ( string $name = null, ApplicationConfig $applicationConfig = null ) Creates a new configuration.
addAlias ( string $alias ) : static Adds an alias name.
addAliases ( array $aliases ) : static Adds a list of alias names.
addSubCommandConfig ( SubCommandConfig $config ) : static Adds configuration for a sub-command.
addSubCommandConfigs ( array $configs ) : static Adds sub-command configurations to the command.
beginOptionCommand ( string $name, string $shortName = null ) : OptionCommandConfig Starts a configuration block for an option command.
beginSubCommand ( string $name ) : SubCommandConfig Starts a configuration block for a sub-command.
buildArgsFormat ( ArgsFormat $baseFormat = null ) : ArgsFormat Builds an {@link ArgsFormat} instance with the given base format.
create ( string $name = null, ApplicationConfig $applicationConfig = null ) : static Creates a new configuration.
disable ( ) : static Disables the command.
disableIf ( boolean $condition ) : static Disables the command if a condition holds and enables it otherwise.
editOptionCommand ( string $name ) : OptionCommandConfig Alias for {@link getSubCommandConfig()}.
editSubCommand ( string $name ) : SubCommandConfig Alias for {@link getSubCommandConfig()}.
enable ( ) : static Enables the command.
enableIf ( boolean $condition ) : static Enables the command if a condition holds and disables it otherwise.
end ( ) : ApplicationConfig Ends the block when dynamically configuring a command configuration.
getAliases ( ) : string[] Returns the alias names of the command.
getApplicationConfig ( ) : ApplicationConfig Returns the application configuration.
getDescription ( ) : string Returns the description of the command.
getHelp ( ) : string Returns the help text of the command.
getName ( ) : string Returns the name of the command.
getProcessTitle ( ) : string | null Returns the title of the command process.
getSubCommandConfig ( string $name ) : SubCommandConfig Returns the sub-command configuration for a given name.
getSubCommandConfigs ( ) : SubCommandConfig[] Returns the configurations of all sub-commands.
hasSubCommandConfig ( string $name ) : boolean Returns whether the command has a sub-command with a given name.
hasSubCommandConfigs ( ) : boolean Returns whether the command has any registered sub-command configurations.
isAnonymous ( ) : boolean Returns whether the command is anonymous.
isDefault ( ) : boolean Returns whether the command is a default command.
isEnabled ( ) : boolean Returns whether the command is enabled or not in the current environment.
markAnonymous ( ) : static Marks the command as anonymous command.
markDefault ( ) : static Marks the command as default command.
markNoDefault ( ) : static Marks the command as neither anonymous nor default.
setAliases ( array $aliases ) : static Sets the alias names of the command.
setApplicationConfig ( ApplicationConfig $applicationConfig ) Sets the application configuration.
setDescription ( string $description ) : static Sets the description of the command.
setHelp ( string $help ) : static Sets the help text of the command.
setName ( string $name ) : static Sets the name of the command.
setProcessTitle ( string | null $processTitle ) : static Sets the title of the command process.
setSubCommandConfigs ( array $configs ) : static Sets the sub-command configurations of the command.

Protected Methods

Method Description
getDefaultArgsParser ( )
getDefaultHandler ( )
getDefaultHandlerMethod ( )
getDefaultHelperSet ( )
getDefaultLenientArgsParsing ( )

Method Details

__construct() public method

Creates a new configuration.
public __construct ( string $name = null, ApplicationConfig $applicationConfig = null )
$name string The name of the command.
$applicationConfig ApplicationConfig The application configuration.

addAlias() public method

An alias is an alternative name that can be used when calling the command. Aliases are a useful way for migrating a command from one name to another. Existing alias names are preserved.
public addAlias ( string $alias ) : static
$alias string The alias name to add.
return static The current instance.

addAliases() public method

Existing alias names are preserved.
public addAliases ( array $aliases ) : static
$aliases array The alias names to add.
return static The current instance.

addSubCommandConfig() public method

Adds configuration for a sub-command.
See also: beginSubCommand()
public addSubCommandConfig ( SubCommandConfig $config ) : static
$config SubCommandConfig The sub-command configuration.
return static The current instance.

addSubCommandConfigs() public method

Adds sub-command configurations to the command.
See also: beginSubCommand()
public addSubCommandConfigs ( array $configs ) : static
$configs array The sub-command configurations.
return static The current instance.

beginOptionCommand() public method

An option command is executed if the corresponding option is passed after the command name. For example, if the command "server" has an option command named "--add" with the short name "-a", that command can be called with: $ console server --add ... $ console server -a ... The configuration of the option command is returned by this method. You can use the fluent interface to configure the option command before jumping back to this configuration with {@link SubCommandConfig::end()}: php protected function configure() { $this ->beginCommand('server') ->setDescription('List and manage servers') ->beginOptionCommand('add', 'a') ->setDescription('Add a server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ->end() ... ; }
See also: editOptionCommand()
public beginOptionCommand ( string $name, string $shortName = null ) : OptionCommandConfig
$name string The name of the option command.
$shortName string The short name of the option command.
return OptionCommandConfig The option command configuration.

beginSubCommand() public method

A sub-command is executed if the name of the command is passed after the name of the containing command. For example, if the command "server" has a sub-command command named "add", that command can be called with: $ console server add ... The configuration of the sub-command is returned by this method. You can use the fluent interface to configure the sub-command before jumping back to this configuration with {@link SubCommandConfig::end()}: php protected function configure() { $this ->beginCommand('server') ->setDescription('List and manage servers') ->beginSubCommand('add') ->setDescription('Add a server') ->addArgument('host', Argument::REQUIRED) ->addOption('port', 'p', Option::VALUE_OPTIONAL, null, 80) ->end() ->end() ... ; }
See also: editSubCommand()
public beginSubCommand ( string $name ) : SubCommandConfig
$name string The name of the sub-command.
return SubCommandConfig The sub-command configuration.

buildArgsFormat() public method

Builds an {@link ArgsFormat} instance with the given base format.
public buildArgsFormat ( ArgsFormat $baseFormat = null ) : ArgsFormat
$baseFormat Webmozart\Console\Api\Args\Format\ArgsFormat The base format.
return Webmozart\Console\Api\Args\Format\ArgsFormat The built format for the console arguments.

create() public static method

Creates a new configuration.
public static create ( string $name = null, ApplicationConfig $applicationConfig = null ) : static
$name string The name of the command.
$applicationConfig ApplicationConfig The application configuration.
return static The created configuration.

disable() public method

Disables the command.
public disable ( ) : static
return static The current instance.

disableIf() public method

Disables the command if a condition holds and enables it otherwise.
public disableIf ( boolean $condition ) : static
$condition boolean The condition under which to disable the command.
return static The current instance.

editOptionCommand() public method

This method can be used to nicely edit an option command inherited from a parent configuration using the fluent API: php protected function configure() { parent::configure(); $this ->editCommand('server') ->editOptionCommand('add') ... ->end() ->end() ... ; }
See also: beginOptionCommand()
public editOptionCommand ( string $name ) : OptionCommandConfig
$name string The name of the option command to edit.
return OptionCommandConfig The option command configuration.

editSubCommand() public method

This method can be used to nicely edit a sub-command inherited from a parent configuration using the fluent API: php protected function configure() { parent::configure(); $this ->editCommand('server') ->editSubCommand('add') ... ->end() ->end() ... ; }
See also: beginSubCommand()
public editSubCommand ( string $name ) : SubCommandConfig
$name string The name of the sub-command to edit.
return SubCommandConfig The sub-command configuration.

enable() public method

Enables the command.
public enable ( ) : static
return static The current instance.

enableIf() public method

Enables the command if a condition holds and disables it otherwise.
public enableIf ( boolean $condition ) : static
$condition boolean The condition under which to enable the command.
return static The current instance.

end() public method

This method is usually used together with {@link ApplicationConfig::beginCommand()}: php $config ->beginCommand('command') ... ->end() ... ;
public end ( ) : ApplicationConfig
return ApplicationConfig The application configuration.

getAliases() public method

Returns the alias names of the command.
public getAliases ( ) : string[]
return string[] An array of alias names of the command.

getApplicationConfig() public method

Returns the application configuration.
public getApplicationConfig ( ) : ApplicationConfig
return ApplicationConfig The application configuration.

getDefaultArgsParser() protected method

protected getDefaultArgsParser ( )

getDefaultHandler() protected method

protected getDefaultHandler ( )

getDefaultHandlerMethod() protected method

protected getDefaultHandlerMethod ( )

getDefaultHelperSet() protected method

protected getDefaultHelperSet ( )

getDefaultLenientArgsParsing() protected method

getDescription() public method

Returns the description of the command.
See also: setDescription()
public getDescription ( ) : string
return string The description of the command.

getHelp() public method

The help text provides additional information about a command that is displayed in the help view.
See also: setHelp()
public getHelp ( ) : string
return string The help text of the command.

getName() public method

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

getProcessTitle() public method

Returns the title of the command process.
See also: setProcessTitle()
public getProcessTitle ( ) : string | null
return string | null The process title or `null` if no title should be set.

getSubCommandConfig() public method

Returns the sub-command configuration for a given name.
See also: beginSubCommand()
public getSubCommandConfig ( string $name ) : SubCommandConfig
$name string The name of the sub-command.
return SubCommandConfig The sub-command configuration.

getSubCommandConfigs() public method

Returns the configurations of all sub-commands.
See also: beginSubCommand()
public getSubCommandConfigs ( ) : SubCommandConfig[]
return SubCommandConfig[] The sub-command configurations.

hasSubCommandConfig() public method

Returns whether the command has a sub-command with a given name.
See also: beginSubCommand()
public hasSubCommandConfig ( string $name ) : boolean
$name string The name of the sub-command.
return boolean Returns `true` if the sub-command configuration with the given name exists and `false` otherwise.

hasSubCommandConfigs() public method

Returns whether the command has any registered sub-command configurations.
See also: beginSubCommand()
public hasSubCommandConfigs ( ) : boolean
return boolean Returns `true` if sub-command configurations were added to the command and `false` otherwise.

isAnonymous() public method

Returns whether the command is anonymous.
public isAnonymous ( ) : boolean
return boolean Returns `true` if {@link markAnonymous()} was called and `false` otherwise.

isDefault() public method

Returns whether the command is a default command.
public isDefault ( ) : boolean
return boolean Returns `true` if either {@link markDefault()} or {@link markAnonymous()} was called and `false` otherwise.

isEnabled() public method

Returns whether the command is enabled or not in the current environment.
public isEnabled ( ) : boolean
return boolean Returns `true` if the command is currently enabled and `false` otherwise.

markAnonymous() public method

Anonymous commands cannot be called by name: php protected function configure() { $this ->beginCommand('add') ->markAnonymous() ->addArgument('host', Argument::REQUIRED) ->end() ... ; } The name "add" is given to the command only to access the command later on. Since the command is anonymous, the name cannot be passed when when calling the command: php $ ./console add localhost Instead, the command should be called without name: php $ ./console localhost
public markAnonymous ( ) : static
return static The current instance.

markDefault() public method

The names of default commands can be omitted when calling the command. For example, the following command can be called in two ways: php protected function configure() { $this ->beginCommand('add') ->markDefault() ->addArgument('host', Argument::REQUIRED) ->end() ... ; } The first way is to call the command regularly. The second way is to omit the name of the command: php $ ./console add localhost $ ./console localhost
public markDefault ( ) : static
return static The current instance.

markNoDefault() public method

Marks the command as neither anonymous nor default.
public markNoDefault ( ) : static
return static The current instance.

setAliases() public method

Existing alias names are replaced.
public setAliases ( array $aliases ) : static
$aliases array The alias names.
return static The current instance.

setApplicationConfig() public method

Sets the application configuration.
public setApplicationConfig ( ApplicationConfig $applicationConfig )
$applicationConfig ApplicationConfig The application configuration.

setDescription() public method

The description is a short one-liner that describes the command in the command listing. The description should be written in imperative form rather than in descriptive form. So: > List the contents of a directory. should be preferred over > Lists the contents of a directory.
See also: getDescription()
public setDescription ( string $description ) : static
$description string The description.
return static The current instance.

setHelp() public method

The help text provides additional information about a command that is displayed in the help view.
See also: getHelp()
public setHelp ( string $help ) : static
$help string The help text of the command.
return static The current instance.

setName() public method

Sets the name of the command.
public setName ( string $name ) : static
$name string The name of the command.
return static The current instance.

setProcessTitle() public method

Sets the title of the command process.
See also: getProcessTitle()
public setProcessTitle ( string | null $processTitle ) : static
$processTitle string | null The process title or `null` if no title should be set.
return static The current instance.

setSubCommandConfigs() public method

Sets the sub-command configurations of the command.
See also: beginSubCommand()
public setSubCommandConfigs ( array $configs ) : static
$configs array The sub-command configurations.
return static The current instance.