Class Symfony\Component\Console\Application

InheritanceSymfony\Component\Console\Application

An Application is the container for a collection of commands.

It is the main entry point of a Console application.

This class is optimized for a standard CLI environment.

Usage:

$app = new Application('myapp', '1.0 (stable)');
$app->add(new SimpleCommand());
$app->run();

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__construct() Constructor. Symfony\Component\Console\Application
add() Adds a command object. Symfony\Component\Console\Application
addCommands() Adds an array of command objects. Symfony\Component\Console\Application
all() Gets the commands (registered in the given namespace if provided). Symfony\Component\Console\Application
areExceptionsCaught() Gets whether to catch exceptions or not during commands execution. Symfony\Component\Console\Application
doRun() Runs the current application. Symfony\Component\Console\Application
extractNamespace() Returns the namespace part of the command name. Symfony\Component\Console\Application
find() Finds a command by name or alias. Symfony\Component\Console\Application
findNamespace() Finds a registered namespace by a name or an abbreviation. Symfony\Component\Console\Application
get() Returns a registered command by name or alias. Symfony\Component\Console\Application
getAbbreviations() Returns an array of possible abbreviations given a set of names. Symfony\Component\Console\Application
getDefinition() Gets the InputDefinition related to this Application. Symfony\Component\Console\Application
getHelp() Gets the help message. Symfony\Component\Console\Application
getHelperSet() Get the helper set associated with the command. Symfony\Component\Console\Application
getLongVersion() Returns the long version of the application. Symfony\Component\Console\Application
getName() Gets the name of the application. Symfony\Component\Console\Application
getNamespaces() Returns an array of all unique namespaces used by currently registered commands. Symfony\Component\Console\Application
getTerminalDimensions() Tries to figure out the terminal dimensions based on the current environment. Symfony\Component\Console\Application
getVersion() Gets the application version. Symfony\Component\Console\Application
has() Returns true if the command exists, false otherwise. Symfony\Component\Console\Application
isAutoExitEnabled() Gets whether to automatically exit after a command execution or not. Symfony\Component\Console\Application
register() Registers a new command. Symfony\Component\Console\Application
renderException() Renders a caught exception. Symfony\Component\Console\Application
run() Runs the current application. Symfony\Component\Console\Application
setAutoExit() Sets whether to automatically exit after a command execution or not. Symfony\Component\Console\Application
setCatchExceptions() Sets whether to catch exceptions or not during commands execution. Symfony\Component\Console\Application
setDefaultCommand() Sets the default Command name. Symfony\Component\Console\Application
setDefinition() Set an input definition to be used with this application. Symfony\Component\Console\Application
setDispatcher() Symfony\Component\Console\Application
setHelperSet() Set a helper set to be used with the command. Symfony\Component\Console\Application
setName() Sets the application name. Symfony\Component\Console\Application
setTerminalDimensions() Sets terminal dimensions. Symfony\Component\Console\Application
setVersion() Sets the application version. Symfony\Component\Console\Application

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
configureIO() Configures the input and output instances based on the user arguments and options. Symfony\Component\Console\Application
doRunCommand() Runs the current command. Symfony\Component\Console\Application
getCommandName() Gets the name of the command based on input. Symfony\Component\Console\Application
getDefaultCommands() Gets the default commands that should always be available. Symfony\Component\Console\Application
getDefaultHelperSet() Gets the default helper set with the helpers that should always be available. Symfony\Component\Console\Application
getDefaultInputDefinition() Gets the default input definition. Symfony\Component\Console\Application
getTerminalHeight() Tries to figure out the terminal height in which this application runs. Symfony\Component\Console\Application
getTerminalWidth() Tries to figure out the terminal width in which this application runs. Symfony\Component\Console\Application

Method Details

__construct() public method

Constructor.

public void __construct ( $name 'UNKNOWN', $version 'UNKNOWN' )
$name string

The name of the application

$version string

The version of the application

add() public method

Adds a command object.

If a command with the same name already exists, it will be overridden. If the command is not enabled it will not be added.

public Symfony\Component\Console\Command\Command|null add ( Symfony\Component\Console\Command\Command $command )
$command Symfony\Component\Console\Command\Command

A Command object

return Symfony\Component\Console\Command\Command|null

The registered command if enabled or null

addCommands() public method

Adds an array of command objects.

If a Command is not enabled it will not be added.

public void addCommands ( array $commands )
$commands Symfony\Component\Console\Command\Command[]

An array of commands

all() public method

Gets the commands (registered in the given namespace if provided).

The array keys are the full names and the values the command instances.

public Symfony\Component\Console\Command\Command[] all ( $namespace null )
$namespace string

A namespace name

return Symfony\Component\Console\Command\Command[]

An array of Command instances

areExceptionsCaught() public method

Gets whether to catch exceptions or not during commands execution.

public bool areExceptionsCaught ( )
return bool

Whether to catch exceptions or not during commands execution

configureIO() protected method

Configures the input and output instances based on the user arguments and options.

protected void configureIO ( 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

doRun() public method

Runs the current application.

public int doRun ( Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$input Symfony\Component\Console\Input\InputInterface

An Input instance

$output Symfony\Component\Console\Output\OutputInterface

An Output instance

return int

0 if everything went fine, or an error code

doRunCommand() protected method

Runs the current command.

If an event dispatcher has been attached to the application, events are also dispatched during the life-cycle of the command.

protected int doRunCommand ( Symfony\Component\Console\Command\Command $command, Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output )
$command Symfony\Component\Console\Command\Command

A Command instance

$input Symfony\Component\Console\Input\InputInterface

An Input instance

$output Symfony\Component\Console\Output\OutputInterface

An Output instance

return int

0 if everything went fine, or an error code

throws Exception

when the command being run threw an exception

extractNamespace() public method

Returns the namespace part of the command name.

This method is not part of public API and should not be used directly.

public string extractNamespace ( $name, $limit null )
$name string

The full name of the command

$limit string

The maximum number of parts of the namespace

return string

The namespace of the command

find() public method

Finds a command by name or alias.

Contrary to get, this command tries to find the best match if you give it an abbreviation of a name or alias.

public Symfony\Component\Console\Command\Command find ( $name )
$name string

A command name or a command alias

return Symfony\Component\Console\Command\Command

A Command instance

throws Symfony\Component\Console\Exception\CommandNotFoundException

When command name is incorrect or ambiguous

findNamespace() public method

Finds a registered namespace by a name or an abbreviation.

public string findNamespace ( $namespace )
$namespace string

A namespace or abbreviation to search for

return string

A registered namespace

throws Symfony\Component\Console\Exception\CommandNotFoundException

When namespace is incorrect or ambiguous

get() public method

Returns a registered command by name or alias.

public Symfony\Component\Console\Command\Command get ( $name )
$name string

The command name or alias

return Symfony\Component\Console\Command\Command

A Command object

throws Symfony\Component\Console\Exception\CommandNotFoundException

When command name given does not exist

getAbbreviations() public static method

Returns an array of possible abbreviations given a set of names.

public static array getAbbreviations ( $names )
$names array

An array of names

return array

An array of abbreviations

getCommandName() protected method

Gets the name of the command based on input.

protected string getCommandName ( Symfony\Component\Console\Input\InputInterface $input )
$input Symfony\Component\Console\Input\InputInterface

The input interface

return string

The command name

getDefaultCommands() protected method

Gets the default commands that should always be available.

protected Symfony\Component\Console\Command\Command[] getDefaultCommands ( )
return Symfony\Component\Console\Command\Command[]

An array of default Command instances

getDefaultHelperSet() protected method

Gets the default helper set with the helpers that should always be available.

protected Symfony\Component\Console\Helper\HelperSet getDefaultHelperSet ( )
return Symfony\Component\Console\Helper\HelperSet

A HelperSet instance

getDefaultInputDefinition() protected method

Gets the default input definition.

protected Symfony\Component\Console\Input\InputDefinition getDefaultInputDefinition ( )
return Symfony\Component\Console\Input\InputDefinition

An InputDefinition instance

getDefinition() public method

Gets the InputDefinition related to this Application.

public Symfony\Component\Console\Input\InputDefinition getDefinition ( )
return Symfony\Component\Console\Input\InputDefinition

The InputDefinition instance

getHelp() public method

Gets the help message.

public string getHelp ( )
return string

A help message

getHelperSet() public method

Get the helper set associated with the command.

public Symfony\Component\Console\Helper\HelperSet getHelperSet ( )
return Symfony\Component\Console\Helper\HelperSet

The HelperSet instance associated with this command

getLongVersion() public method

Returns the long version of the application.

public string getLongVersion ( )
return string

The long application version

getName() public method

Gets the name of the application.

public string getName ( )
return string

The application name

getNamespaces() public method

Returns an array of all unique namespaces used by currently registered commands.

It does not return the global namespace which always exists.

public string[] getNamespaces ( )
return string[]

An array of namespaces

getTerminalDimensions() public method

Tries to figure out the terminal dimensions based on the current environment.

public array getTerminalDimensions ( )
return array

Array containing width and height

getTerminalHeight() protected method

Tries to figure out the terminal height in which this application runs.

protected int|null getTerminalHeight ( )
getTerminalWidth() protected method

Tries to figure out the terminal width in which this application runs.

protected int|null getTerminalWidth ( )
getVersion() public method

Gets the application version.

public string getVersion ( )
return string

The application version

has() public method

Returns true if the command exists, false otherwise.

public bool has ( $name )
$name string

The command name or alias

return bool

True if the command exists, false otherwise

isAutoExitEnabled() public method

Gets whether to automatically exit after a command execution or not.

public bool isAutoExitEnabled ( )
return bool

Whether to automatically exit after a command execution or not

register() public method

Registers a new command.

public Symfony\Component\Console\Command\Command register ( $name )
$name string

The command name

return Symfony\Component\Console\Command\Command

The newly created command

renderException() public method

Renders a caught exception.

public void renderException ( Exception $e, Symfony\Component\Console\Output\OutputInterface $output )
$e Exception

An exception instance

$output Symfony\Component\Console\Output\OutputInterface

An OutputInterface instance

run() public method

Runs the current application.

public int run ( Symfony\Component\Console\Input\InputInterface $input null, Symfony\Component\Console\Output\OutputInterface $output null )
$input Symfony\Component\Console\Input\InputInterface

An Input instance

$output Symfony\Component\Console\Output\OutputInterface

An Output instance

return int

0 if everything went fine, or an error code

throws Exception

When doRun returns Exception

setAutoExit() public method

Sets whether to automatically exit after a command execution or not.

public void setAutoExit ( $boolean )
$boolean bool

Whether to automatically exit after a command execution or not

setCatchExceptions() public method

Sets whether to catch exceptions or not during commands execution.

public void setCatchExceptions ( $boolean )
$boolean bool

Whether to catch exceptions or not during commands execution

setDefaultCommand() public method

Sets the default Command name.

public void setDefaultCommand ( $commandName )
$commandName string

The Command name

setDefinition() public method

Set an input definition to be used with this application.

public void setDefinition ( Symfony\Component\Console\Input\InputDefinition $definition )
$definition Symfony\Component\Console\Input\InputDefinition

The input definition

setDispatcher() public method

public void setDispatcher ( Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher )
$dispatcher
setHelperSet() public method

Set a helper set to be used with the command.

public void setHelperSet ( Symfony\Component\Console\Helper\HelperSet $helperSet )
$helperSet Symfony\Component\Console\Helper\HelperSet

The helper set

setName() public method

Sets the application name.

public void setName ( $name )
$name string

The application name

setTerminalDimensions() public method

Sets terminal dimensions.

Can be useful to force terminal dimensions for functional tests.

public Symfony\Component\Console\Application setTerminalDimensions ( $width, $height )
$width int

The width

$height int

The height

return Symfony\Component\Console\Application

The current application

setVersion() public method

Sets the application version.

public void setVersion ( $version )
$version string

The application version