Copied!

Base class for all commands.

CloneableInstantiable
Constants
public Symfony\Component\Console\Command\Command::FAILURE = 1
public Symfony\Component\Console\Command\Command::INVALID = 2
public Symfony\Component\Console\Command\Command::SUCCESS = 0
Methods
public __construct(?string $name = NULL)
 
  • param string|null $name The name of the command; passing null means it must be set in configure()
  • throws \LogicException When the command name is empty
public addArgument(string $name, ?int $mode = NULL, string $description = '', ?mixed $default = NULL, Closure|array $suggestedValues = []) : static
 

Adds an argument.

  • param mixed $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
  • param mixed $default The default value (for InputArgument::OPTIONAL mode only)
  • param array|callable $suggestedValues The values used for input completion
  • return $this
  • throws \InvalidArgumentException When argument mode is not valid
public addOption(string $name, array|string|?null $shortcut = NULL, ?int $mode = NULL, string $description = '', ?mixed $default = NULL, Closure|array $suggestedValues = []) : static
 

Adds an option.

  • param mixed $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
  • param mixed $mode The option mode: One of the InputOption::VALUE_* constants
  • param mixed $default The default value (must be null for InputOption::VALUE_NONE)
  • param array|callable $suggestedValues The values used for input completion
  • return $this
  • throws \InvalidArgumentException If option mode is invalid or incompatible
public addUsage(string $usage) : static
 

Add a command usage example, it'll be prefixed with the command name.

  • return $this
public complete(Symfony\Component\Console\Completion\CompletionInput $input, Symfony\Component\Console\Completion\CompletionSuggestions $suggestions) : void
 

Supplies suggestions when resolving possible completion options for input (e.g. option or argument).

public getAliases() : array
 

Returns the aliases for the command.

public getApplication() : ?Symfony\Component\Console\Application
 

Gets the application instance for this command.

public static getDefaultDescription() : ?string
public static getDefaultName() : ?string
public getDefinition() : Symfony\Component\Console\Input\InputDefinition
 

Gets the InputDefinition attached to this Command.

public getDescription() : string
 

Returns the description for the command.

public getHelp() : string
 

Returns the help for the command.

public getHelper(string $name) : Symfony\Component\Console\Helper\HelperInterface
 

Gets a helper instance by name.

  • throws \LogicException if no HelperSet is defined
  • throws \InvalidArgumentException if the helper is not defined
public getHelperSet() : ?Symfony\Component\Console\Helper\HelperSet
 

Gets the helper set.

public getName() : ?string
 

Returns the command name.

public getNativeDefinition() : Symfony\Component\Console\Input\InputDefinition
 

Gets the InputDefinition to be used to create representations of this Command.

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 getProcessedHelp() : string
 

Returns the processed help for the command replacing the %command.name% and %command.full_name% patterns with the real values dynamically.

public getSynopsis(bool $short = false) : string
 

Returns the synopsis for the command.

  • param bool $short Whether to show the short version of the synopsis (with options folded) or not
public getUsages() : array
 

Returns alternative usages of the command.

public ignoreValidationErrors() : void
 

Ignores validation errors.

This is mainly useful for the help command.

public isEnabled() : bool
 

Checks whether the command is enabled or not in the current environment.

Override this to check for x or y and return false if the command cannot run properly under the current conditions.

public isHidden() : bool
 
  • return bool whether the command should be publicly shown or not
public mergeApplicationDefinition(bool $mergeArgs = true) : void
 

Merges the application definition with the command definition.

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

  • param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments
  • internal
public run(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output) : int
 

Runs the command.

The code to execute is either defined directly with the setCode() method or by overriding the execute() method in a sub-class.

  • return int The command exit code
  • throws \ExceptionInterface When input binding fails. Bypass this by calling {@link ignoreValidationErrors()}.
  • see \setCode()
  • see \execute()
public setAliases(iterable $aliases) : static
 

Sets the aliases for the command.

  • param string[] $aliases An array of aliases for the command
  • return $this
  • throws \InvalidArgumentException When an alias is invalid
public setApplication(?Symfony\Component\Console\Application $application) : void
public setCode(callable $code) : static
 

Sets the code to execute when running this command.

If this method is used, it overrides the code defined in the execute() method.

  • param callable $code A callable(InputInterface $input, OutputInterface $output)
  • return $this
  • throws \InvalidArgumentException
  • see \execute()
public setDefinition(Symfony\Component\Console\Input\InputDefinition|array $definition) : static
 

Sets an array of argument and option instances.

  • return $this
public setDescription(string $description) : static
 

Sets the description for the command.

  • return $this
public setHelp(string $help) : static
 

Sets the help for the command.

  • return $this
public setHelperSet(Symfony\Component\Console\Helper\HelperSet $helperSet) : void
public setHidden(bool $hidden = true) : static
 
  • param bool $hidden Whether or not the command should be hidden from the list of commands
  • return $this
public setName(string $name) : static
 

Sets the name of the command.

This method can set both the namespace and the name if you separate them by a colon (:)

$command->setName('foo:bar');
  • return $this
  • throws \InvalidArgumentException When the name is invalid
public setProcessTitle(string $title) : static
 

Sets the process title of the command.

This feature should be used only when creating a long process command, like a daemon.

  • return $this
Methods
protected configure()
 

Configures the current command.

  • return void
protected execute(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output) : int
 

Executes the current command.

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.

  • return int 0 if everything went fine, or an exit code
  • throws \LogicException When this abstract method is not implemented
  • see \setCode()
protected initialize(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output)
 

Initializes the command after the input has been bound and before the input is validated.

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.

  • see \InputInterface::bind()
  • see \InputInterface::validate()
  • return void
protected interact(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output)
 

Interacts with the user.

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.

  • return void
Properties
private array $aliases = []
private ?Symfony\Component\Console\Application $application = NULL
private ?Closure $code = NULL
private Symfony\Component\Console\Input\InputDefinition $definition
private string $description = ''
private ?Symfony\Component\Console\Input\InputDefinition $fullDefinition = NULL
private string $help = ''
private ?Symfony\Component\Console\Helper\HelperSet $helperSet = NULL
private bool $hidden = false
private bool $ignoreValidationErrors = false
private ?string $name = NULL
private ?string $processTitle = NULL
private array $synopsis = []
private array $usages = []
Methods
private validateName(string $name) : void
 

Validates a command name.

It must be non-empty and parts can optionally be separated by ":".

  • throws \InvalidArgumentException When the name is invalid
Methods
public static getDefaultDescription() : ?string
public static getDefaultName() : ?string
© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration