Documentation

CustomDefaultCommandApplication extends Application
in package

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();

Table of Contents

Properties

$autoExit  : mixed
$catchExceptions  : mixed
$commands  : mixed
$defaultCommand  : mixed
$definition  : mixed
$dispatcher  : mixed
$helperSet  : mixed
$name  : mixed
$runningCommand  : mixed
$singleCommand  : mixed
$terminal  : mixed
$version  : mixed
$wantHelps  : mixed

Methods

__construct()  : mixed
Overwrites the constructor in order to set a different default command.
add()  : Command|null
Adds a command object.
addCommands()  : mixed
Adds an array of command objects.
all()  : array<string|int, Command>
Gets the commands (registered in the given namespace if provided).
areExceptionsCaught()  : bool
Gets whether to catch exceptions or not during commands execution.
doRun()  : int
Runs the current application.
extractNamespace()  : string
Returns the namespace part of the command name.
find()  : Command
Finds a command by name or alias.
findNamespace()  : string
Finds a registered namespace by a name or an abbreviation.
get()  : Command
Returns a registered command by name or alias.
getAbbreviations()  : array<string|int, mixed>
Returns an array of possible abbreviations given a set of names.
getDefinition()  : InputDefinition
Gets the InputDefinition related to this Application.
getHelp()  : string
Gets the help message.
getHelperSet()  : HelperSet
Get the helper set associated with the command.
getLongVersion()  : string
Returns the long version of the application.
getName()  : string
Gets the name of the application.
getNamespaces()  : array<string|int, string>
Returns an array of all unique namespaces used by currently registered commands.
getTerminalDimensions()  : array<string|int, mixed>
Tries to figure out the terminal dimensions based on the current environment.
getVersion()  : string
Gets the application version.
has()  : bool
Returns true if the command exists, false otherwise.
isAutoExitEnabled()  : bool
Gets whether to automatically exit after a command execution or not.
register()  : Command
Registers a new command.
renderException()  : mixed
Renders a caught exception.
run()  : int
Runs the current application.
setAutoExit()  : mixed
Sets whether to automatically exit after a command execution or not.
setCatchExceptions()  : mixed
Sets whether to catch exceptions or not during commands execution.
setDefaultCommand()  : self
Sets the default Command name.
setDefinition()  : mixed
Set an input definition to be used with this application.
setDispatcher()  : mixed
setHelperSet()  : mixed
Set a helper set to be used with the command.
setName()  : mixed
Sets the application name.
setTerminalDimensions()  : $this
Sets terminal dimensions.
setVersion()  : mixed
Sets the application version.
configureIO()  : mixed
Configures the input and output instances based on the user arguments and options.
doRunCommand()  : int
Runs the current command.
getCommandName()  : string
Gets the name of the command based on input.
getDefaultCommands()  : array<string|int, Command>
Gets the default commands that should always be available.
getDefaultHelperSet()  : HelperSet
Gets the default helper set with the helpers that should always be available.
getDefaultInputDefinition()  : InputDefinition
Gets the default input definition.
getTerminalHeight()  : int|null
Tries to figure out the terminal height in which this application runs.
getTerminalWidth()  : int|null
Tries to figure out the terminal width in which this application runs.
extractAllNamespaces()  : array<string|int, string>
Returns all namespaces of the command name.
findAlternatives()  : array<string|int, string>
Finds alternative of $name among $collection, if nothing is found in $collection, try in $abbrevs.
getAbbreviationSuggestions()  : string
Returns abbreviated suggestions in string format.
splitStringByWidth()  : mixed

Properties

$catchExceptions

private mixed $catchExceptions = true

Methods

__construct()

Overwrites the constructor in order to set a different default command.

public __construct() : mixed

add()

Adds a command object.

public add(Command $command) : Command|null

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

Parameters
$command : Command

A Command object

Return values
Command|null

The registered command if enabled or null

addCommands()

Adds an array of command objects.

public addCommands(array<string|int, Command$commands) : mixed

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

Parameters
$commands : array<string|int, Command>

An array of commands

all()

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

public all([string $namespace = null ]) : array<string|int, Command>

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

Parameters
$namespace : string = null

A namespace name

Return values
array<string|int, Command>

An array of Command instances

areExceptionsCaught()

Gets whether to catch exceptions or not during commands execution.

public areExceptionsCaught() : bool
Return values
bool

Whether to catch exceptions or not during commands execution

extractNamespace()

Returns the namespace part of the command name.

public extractNamespace(string $name[, string $limit = null ]) : string

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

Parameters
$name : string

The full name of the command

$limit : string = null

The maximum number of parts of the namespace

Return values
string

The namespace of the command

find()

Finds a command by name or alias.

public find(string $name) : Command

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

Parameters
$name : string

A command name or a command alias

Tags
throws
CommandNotFoundException

When command name is incorrect or ambiguous

Return values
Command

A Command instance

findNamespace()

Finds a registered namespace by a name or an abbreviation.

public findNamespace(string $namespace) : string
Parameters
$namespace : string

A namespace or abbreviation to search for

Tags
throws
CommandNotFoundException

When namespace is incorrect or ambiguous

Return values
string

A registered namespace

get()

Returns a registered command by name or alias.

public get(string $name) : Command
Parameters
$name : string

The command name or alias

Tags
throws
CommandNotFoundException

When given command name does not exist

Return values
Command

A Command object

getAbbreviations()

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

public static getAbbreviations(array<string|int, mixed> $names) : array<string|int, mixed>
Parameters
$names : array<string|int, mixed>

An array of names

Return values
array<string|int, mixed>

An array of abbreviations

getHelp()

Gets the help message.

public getHelp() : string
Return values
string

A help message

getHelperSet()

Get the helper set associated with the command.

public getHelperSet() : HelperSet
Return values
HelperSet

The HelperSet instance associated with this command

getLongVersion()

Returns the long version of the application.

public getLongVersion() : string
Return values
string

The long application version

getName()

Gets the name of the application.

public getName() : string
Return values
string

The application name

getNamespaces()

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

public getNamespaces() : array<string|int, string>

It does not return the global namespace which always exists.

Return values
array<string|int, string>

An array of namespaces

getTerminalDimensions()

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

public getTerminalDimensions() : array<string|int, mixed>

since version 3.2, to be removed in 4.0. Create a Terminal instance instead.

Return values
array<string|int, mixed>

Array containing width and height

getVersion()

Gets the application version.

public getVersion() : string
Return values
string

The application version

has()

Returns true if the command exists, false otherwise.

public has(string $name) : bool
Parameters
$name : string

The command name or alias

Return values
bool

true if the command exists, false otherwise

isAutoExitEnabled()

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

public isAutoExitEnabled() : bool
Return values
bool

Whether to automatically exit after a command execution or not

register()

Registers a new command.

public register(string $name) : Command
Parameters
$name : string

The command name

Return values
Command

The newly created command

renderException()

Renders a caught exception.

public renderException(Exception $e, OutputInterface $output) : mixed
Parameters
$e : Exception

An exception instance

$output : OutputInterface

An OutputInterface instance

run()

Runs the current application.

public run([InputInterface $input = null ][, OutputInterface $output = null ]) : int
Parameters
$input : InputInterface = null

An Input instance

$output : OutputInterface = null

An Output instance

Tags
throws
Exception

When running fails. Bypass this when .

Return values
int

0 if everything went fine, or an error code

setAutoExit()

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

public setAutoExit(bool $boolean) : mixed
Parameters
$boolean : bool

Whether to automatically exit after a command execution or not

setCatchExceptions()

Sets whether to catch exceptions or not during commands execution.

public setCatchExceptions(bool $boolean) : mixed
Parameters
$boolean : bool

Whether to catch exceptions or not during commands execution

setDefaultCommand()

Sets the default Command name.

public setDefaultCommand(string $commandName[, bool $isSingleCommand = false ]) : self
Parameters
$commandName : string

The Command name

$isSingleCommand : bool = false

Set to true if there is only one command in this application

Return values
self

setDispatcher()

public setDispatcher(EventDispatcherInterface $dispatcher) : mixed
Parameters
$dispatcher : EventDispatcherInterface

setHelperSet()

Set a helper set to be used with the command.

public setHelperSet(HelperSet $helperSet) : mixed
Parameters
$helperSet : HelperSet

The helper set

setName()

Sets the application name.

public setName(string $name) : mixed
Parameters
$name : string

The application name

setTerminalDimensions()

Sets terminal dimensions.

public setTerminalDimensions(int $width, int $height) : $this

since version 3.2, to be removed in 4.0. Set the COLUMNS and LINES env vars instead.

Can be useful to force terminal dimensions for functional tests.

Parameters
$width : int

The width

$height : int

The height

Return values
$this

setVersion()

Sets the application version.

public setVersion(string $version) : mixed
Parameters
$version : string

The application version

doRunCommand()

Runs the current command.

protected doRunCommand(Command $command, InputInterface $input, OutputInterface $output) : int

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

Parameters
$command : Command

A Command instance

$input : InputInterface

An Input instance

$output : OutputInterface

An Output instance

Return values
int

0 if everything went fine, or an error code

getCommandName()

Gets the name of the command based on input.

protected getCommandName(InputInterface $input) : string
Parameters
$input : InputInterface

The input interface

Return values
string

The command name

getDefaultCommands()

Gets the default commands that should always be available.

protected getDefaultCommands() : array<string|int, Command>
Return values
array<string|int, Command>

An array of default Command instances

getDefaultHelperSet()

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

protected getDefaultHelperSet() : HelperSet
Return values
HelperSet

A HelperSet instance

getTerminalHeight()

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

protected getTerminalHeight() : int|null

since version 3.2, to be removed in 4.0. Create a Terminal instance instead.

Return values
int|null

getTerminalWidth()

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

protected getTerminalWidth() : int|null

since version 3.2, to be removed in 4.0. Create a Terminal instance instead.

Return values
int|null

extractAllNamespaces()

Returns all namespaces of the command name.

private extractAllNamespaces(string $name) : array<string|int, string>
Parameters
$name : string

The full name of the command

Return values
array<string|int, string>

The namespaces of the command

findAlternatives()

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

private findAlternatives(string $name, array<string|int, mixed>|Traversable $collection) : array<string|int, string>
Parameters
$name : string

The string

$collection : array<string|int, mixed>|Traversable

The collection

Return values
array<string|int, string>

A sorted array of similar string

getAbbreviationSuggestions()

Returns abbreviated suggestions in string format.

private getAbbreviationSuggestions(array<string|int, mixed> $abbrevs) : string
Parameters
$abbrevs : array<string|int, mixed>

Abbreviated suggestions to convert

Return values
string

A formatted string of abbreviated suggestions

splitStringByWidth()

private splitStringByWidth(mixed $string, mixed $width) : mixed
Parameters
$string : mixed
$width : mixed

        
On this page

Search results