SymfonyComponentConsoleApplication

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();
File
vendorSymfonyComponentConsoleApplication.php
Package
Default
Class hierarchy
SymfonyComponentConsoleApplication

Tags

author

Fabien Potencier fabien@symfony.com

api

Properties

$commands

$commands : 

Type

Tags

None found

$wantHelps

$wantHelps : 

Type

Tags

None found

$runningCommand

$runningCommand : 

Type

Tags

None found

$name

$name : 

Type

Tags

None found

$version

$version : 

Type

Tags

None found

$catchExceptions

$catchExceptions : 

Type

Tags

None found

$autoExit

$autoExit : 

Type

Tags

None found

$definition

$definition : 

Type

Tags

None found

$helperSet

$helperSet : 

Type

Tags

None found

Methods

__construct()

__construct(string  $name = 'UNKNOWN', string  $version = 'UNKNOWN') 

Constructor.

Parameters

string $name

The name of the application

string $version

The version of the application

Tags

api

run()

run(\Symfony\Component\Console\Input\InputInterface  $input = null, \Symfony\Component\Console\Output\OutputInterface  $output = null) : integer

Runs the current application.

Parameters

SymfonyComponentConsoleInputInputInterface $input

An Input instance

SymfonyComponentConsoleOutputOutputInterface $output

An Output instance

Throws

Exception

When doRun returns Exception

Returns

integer —

0 if everything went fine, or an error code

Tags

api

doRun()

doRun(\Symfony\Component\Console\Input\InputInterface  $input, \Symfony\Component\Console\Output\OutputInterface  $output) : integer

Runs the current application.

Parameters

SymfonyComponentConsoleInputInputInterface $input

An Input instance

SymfonyComponentConsoleOutputOutputInterface $output

An Output instance

Returns

integer —

0 if everything went fine, or an error code

Tags

None found

setHelperSet()

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

Set a helper set to be used with the command.

Parameters

SymfonyComponentConsoleHelperHelperSet $helperSet

The helper set

Tags

api

getHelperSet()

getHelperSet() : \Symfony\Component\Console\Helper\HelperSet

Get the helper set associated with the command.

Returns

SymfonyComponentConsoleHelperHelperSet

The HelperSet instance associated with this command

Tags

api

getDefinition()

getDefinition() : \Symfony\Component\Console\Input\InputDefinition

Gets the InputDefinition related to this Application.

Returns

SymfonyComponentConsoleInputInputDefinition

The InputDefinition instance

Tags

None found

getHelp()

getHelp() : string

Gets the help message.

Returns

string —

A help message.

Tags

None found

setCatchExceptions()

setCatchExceptions(Boolean  $boolean) 

Sets whether to catch exceptions or not during commands execution.

Parameters

Boolean $boolean

Whether to catch exceptions or not during commands execution

Tags

api

setAutoExit()

setAutoExit(Boolean  $boolean) 

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

Parameters

Boolean $boolean

Whether to automatically exit after a command execution or not

Tags

api

getName()

getName() : string

Gets the name of the application.

Returns

string —

The application name

Tags

api

setName()

setName(string  $name) 

Sets the application name.

Parameters

string $name

The application name

Tags

api

getVersion()

getVersion() : string

Gets the application version.

Returns

string —

The application version

Tags

api

setVersion()

setVersion(string  $version) 

Sets the application version.

Parameters

string $version

The application version

Tags

api

getLongVersion()

getLongVersion() : string

Returns the long version of the application.

Returns

string —

The long application version

Tags

api

register()

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

Registers a new command.

Parameters

string $name

The command name

Returns

SymfonyComponentConsoleCommandCommand

The newly created command

Tags

api

addCommands()

addCommands(array<mixed,\Symfony\Component\Console\Command\Command>  $commands) 

Adds an array of command objects.

Parameters

array<mixed,SymfonyComponentConsoleCommandCommand> $commands

An array of commands

Tags

api

add()

add(\Symfony\Component\Console\Command\Command  $command) : \Symfony\Component\Console\Command\Command

Adds a command object.

If a command with the same name already exists, it will be overridden.

Parameters

SymfonyComponentConsoleCommandCommand $command

A Command object

Returns

SymfonyComponentConsoleCommandCommand

The registered command

Tags

api

get()

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

Returns a registered command by name or alias.

Parameters

string $name

The command name or alias

Throws

InvalidArgumentException

When command name given does not exist

Returns

SymfonyComponentConsoleCommandCommand

A Command object

Tags

api

has()

has(string  $name) : Boolean

Returns true if the command exists, false otherwise.

Parameters

string $name

The command name or alias

Returns

Boolean —

true if the command exists, false otherwise

Tags

api

getNamespaces()

getNamespaces() : array

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

It does not returns the global namespace which always exists.

Returns

array —

An array of namespaces

Tags

None found

findNamespace()

findNamespace(string  $namespace) : string

Finds a registered namespace by a name or an abbreviation.

Parameters

string $namespace

A namespace or abbreviation to search for

Throws

InvalidArgumentException

When namespace is incorrect or ambiguous

Returns

string —

A registered namespace

Tags

None found

find()

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

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.

Parameters

string $name

A command name or a command alias

Throws

InvalidArgumentException

When command name is incorrect or ambiguous

Returns

SymfonyComponentConsoleCommandCommand

A Command instance

Tags

api

all()

all(string  $namespace = null) : array

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

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

Parameters

string $namespace

A namespace name

Returns

array —

An array of Command instances

Tags

api

getAbbreviations()

getAbbreviations(array  $names) : array

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

Parameters

array $names

An array of names

Returns

array —

An array of abbreviations

static

Tags

None found

asText()

asText(string  $namespace = null, boolean  $raw = false) : string

Returns a text representation of the Application.

Parameters

string $namespace

An optional namespace name

boolean $raw

Whether to return raw command list

Returns

string —

A string representing the Application

Tags

None found

asXml()

asXml(string  $namespace = null, Boolean  $asDom = false) : string|\Symfony\Component\Console\DOMDocument

Returns an XML representation of the Application.

Parameters

string $namespace

An optional namespace name

Boolean $asDom

Whether to return a DOM or an XML string

Returns

string|SymfonyComponentConsoleDOMDocument —

An XML string representing the Application

Tags

None found

renderException()

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

Renders a catched exception.

Parameters

SymfonyComponentConsoleException $e

An exception instance

SymfonyComponentConsoleOutputOutputInterface $output

An OutputInterface instance

Tags

None found

getCommandName()

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

Gets the name of the command based on input.

Parameters

SymfonyComponentConsoleInputInputInterface $input

The input interface

Returns

string —

The command name

Tags

None found

getDefaultInputDefinition()

getDefaultInputDefinition() : \Symfony\Component\Console\Input\InputDefinition

Gets the default input definition.

Returns

SymfonyComponentConsoleInputInputDefinition

An InputDefinition instance

Tags

None found

getDefaultCommands()

getDefaultCommands() : array

Gets the default commands that should always be available.

Returns

array —

An array of default Command instances

Tags

None found

getDefaultHelperSet()

getDefaultHelperSet() : \Symfony\Component\Console\Helper\HelperSet

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

Returns

SymfonyComponentConsoleHelperHelperSet

A HelperSet instance

Tags

None found

sortCommands()

sortCommands(array  $commands) : array

Sorts commands in alphabetical order.

Parameters

array $commands

An associative array of commands to sort

Returns

array —

A sorted array of commands

Tags

None found

getAbbreviationSuggestions()

getAbbreviationSuggestions(array  $abbrevs) : string

Returns abbreviated suggestions in string format.

Parameters

array $abbrevs

Abbreviated suggestions to convert

Returns

string —

A formatted string of abbreviated suggestions

Tags

None found

extractNamespace()

extractNamespace(string  $name, string  $limit = null) : string

Returns the namespace part of the command name.

Parameters

string $name

The full name of the command

string $limit

The maximum number of parts of the namespace

Returns

string —

The namespace of the command

Tags

None found

findAlternativeCommands()

findAlternativeCommands(string  $name, array  $abbrevs) : array

Finds alternative commands of $name

Parameters

string $name

The full name of the command

array $abbrevs

The abbreviations

Returns

array —

A sorted array of similar commands

Tags

None found

findAlternativeNamespace()

findAlternativeNamespace(string  $name, array  $abbrevs) : array

Finds alternative namespace of $name

Parameters

string $name

The full name of the namespace

array $abbrevs

The abbreviations

Returns

array —

A sorted array of similar namespace

Tags

None found

findAlternatives()

findAlternatives(string  $name, array|\Symfony\Component\Console\Traversable  $collection, array  $abbrevs, \Symfony\Component\Console\Closure|string|array  $callback = null) : array

Finds alternative of $name among $collection, if nothing is found in $collection, try in $abbrevs

Parameters

string $name

The string

array|SymfonyComponentConsoleTraversable $collection

The collecion

array $abbrevs

The abbreviations

SymfonyComponentConsoleClosure|string|array $callback

The callable to transform collection item before comparison

Returns

array —

A sorted array of similar string

Tags

None found