class CommandInfo (View source)

Given a class and method name, parse the annotations in the DocBlock comment, and provide accessor methods for all of the elements that are needed to create a Symfony Console Command.

Note that the name of this class is now somewhat of a misnomer, as we now use it to hold annotation data for hooks as well as commands. It would probably be better to rename this to MethodInfo at some point.

Constants

SERIALIZATION_SCHEMA_VERSION

Serialization schema version. Incremented every time the serialization schema changes.

Properties

protected ReflectionMethod $reflection
protected bool $docBlockIsParsed
protected bool $parsingInProgress
protected string $name
protected string $description
protected string $help
protected DefaultsWithDescriptions $options
protected DefaultsWithDescriptions $arguments
protected array $exampleUsage
protected AnnotationData $otherAnnotations
protected array $aliases
protected InputOption[] $inputOptions
protected string $methodName
protected string $returnType
protected string[] $injectedClasses
protected bool[] $parameterMap
protected bool $simpleOptionParametersAllowed

Methods

__construct(string|mixed $classNameOrInstance, string $methodName, array $cache = []) deprecated

Create a new CommandInfo class for a particular method of a class.

static 
create($classNameOrInstance, $methodName)

No description

static 
deserialize($cache)

No description

cachedFileIsModified($cache)

No description

constructFromClassAndMethod($classNameOrInstance, $methodName)

No description

string
getMethodName()

Recover the method name provided to the constructor.

string
getName()

Return the primary name for this command.

setName(string $name)

Set the primary name for this command.

valid()

Return whether or not this method represents a valid command or hook.

invalidate()

If higher-level code decides that this CommandInfo is not interesting or useful (if it is not a command method or a hook method), then we will mark it as invalid to prevent it from being created as a command.

getParameterMap()

No description

getReturnType()

No description

getInjectedClasses()

No description

setInjectedClasses($injectedClasses)

No description

setReturnType($returnType)

No description

getRawAnnotations()

Get any annotations included in the docblock comment for the implementation method of this command that are not already handled by the primary methods of this class.

replaceRawAnnotations($annotationData)

Replace the annotation data.

getAnnotations()

Get any annotations included in the docblock comment, also including default values such as @command. We add in the default @command annotation late, and only in a copy of the annotation data because we use the existance of a @command to indicate that this CommandInfo is a command, and not a hook or anything else.

array|null
getAnnotationList(string $name)

Return a specific named annotation for this command as a list.

string|null
getAnnotation(string $name)

Return a specific named annotation for this command as a string.

bool
hasAnnotation(string $annotation)

Check to see if the specified annotation exists for this command.

addAnnotation($name, $content)

Save any tag that we do not explicitly recognize in the 'otherAnnotations' map.

removeAnnotation($name)

Remove an annotation that was previoudly set.

string
getDescription()

Get the synopsis of the command (~first line).

setDescription(string $description)

Set the command description.

hasHelp()

Determine if help was provided for this command info

getHelp()

Get the help text of the command (the description)

setHelp(string $help)

Set the help text for this command.

string[]
getAliases()

Return the list of aliases for this command.

setAliases(string|string[] $aliases)

Set aliases that can be used in place of the command's primary name.

bool
getHidden()

Get hidden status for the command.

setHidden(bool $hidden)

Set hidden status. List command omits hidden commands.

string[]
getExampleUsages()

Return the examples for this command. This is @usage instead of

setExampleUsage(string $usage, string $description)

Add an example usage for this command.

replaceExampleUsages($usages)

Overwrite all example usages

string[]
getTopics()

Return the topics for this command.

ReflectionParameter[]
getParameters()

Return the list of refleaction parameters.

arguments()

Descriptions of commandline arguements for this command.

options()

Descriptions of commandline options for this command.

InputOption[]
inputOptions()

Get the inputOptions for the options associated with this CommandInfo object, e.g. via @option annotations, or from $options = ['someoption' => 'defaultvalue'] in the command method parameter list.

addImplicitNoOptions()

No description

createInputOptions()

No description

string
findMatchingOption(string $optionName)

An option might have a name such as 'silent|s'. In this instance, we will allow the @option or @default tag to reference the option only by name (e.g. 'silent' or 's' instead of 'silent|s').

addArgumentDescription($name, $description, $suggestedValues = [])

No description

addOption($name, $description, $suggestedValues = [], $defaultValue = null)

No description

addOptionDescription($name, $description, $suggestedValues = [], $defaultValue = null) deprecated

No description

addOptionOrArgumentDescription(DefaultsWithDescriptions $set, $variableName, $description, $suggestedValues = [], $defaultFromParameter = null)

No description

splitOutDefault($description)

No description

string
findOptionAmongAlternatives(string $optionName)

No description

string|null
findExistingOption(string $optionName)

No description

array
determineAgumentClassifications()

Examine the parameters of the method for this command, and build a list of commandline arguments for them.

addParameterToResult($result, $param)

Examine the provided parameter, and determine whether it is a parameter that will be filled in with a positional commandline argument.

array
determineOptionsFromParameters()

Examine the parameters of the method for this command, and determine the disposition of the options from them.

lastParameterIsOptionsArray()

Determine if the last argument contains $options.

bool
isAssoc(array $arr)

Helper; determine if an array is associative or not. An array is not associative if its keys are numeric, and numbered sequentially from zero. All other arrays are considered to be associative.

string
convertName(string $camel)

Convert from a method name to the corresponding command name. A method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will become 'foo:bar-baz-boz'.

convertArgumentName($camel)

Convert an argument name from snake_case or camelCase to a hyphenated-string.

camelToSnake($camel, $splitter = '_')

Convert a camelCase string to a snake_case string.

requireConsistentState()

Guard against invalid usage of CommandInfo during parsing.

parseDocBlock()

Parse the docBlock comment for this command, and set the fields of this class with the data thereby obtained.

static 
convertListToCommaSeparated($text)

Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', convert the data into the last of these forms.

Details

__construct(string|mixed $classNameOrInstance, string $methodName, array $cache = []) deprecated

deprecated Use CommandInfo::create() or CommandInfo::deserialize() instead. In the future, this constructor will be protected.

Create a new CommandInfo class for a particular method of a class.

Parameters

string|mixed $classNameOrInstance

The name of a class, or an instance of it, or an array of cached data.

string $methodName

The name of the method to get info about.

array $cache

Cached data

static create($classNameOrInstance, $methodName)

No description

Parameters

$classNameOrInstance
$methodName

static deserialize($cache)

No description

Parameters

$cache

cachedFileIsModified($cache)

No description

Parameters

$cache

protected constructFromClassAndMethod($classNameOrInstance, $methodName)

No description

Parameters

$classNameOrInstance
$methodName

string getMethodName()

Recover the method name provided to the constructor.

Return Value

string

string getName()

Return the primary name for this command.

Return Value

string

setName(string $name)

Set the primary name for this command.

Parameters

string $name

valid()

Return whether or not this method represents a valid command or hook.

invalidate()

If higher-level code decides that this CommandInfo is not interesting or useful (if it is not a command method or a hook method), then we will mark it as invalid to prevent it from being created as a command.

We still cache a placeholder record for invalid methods, so that we do not need to re-parse the method again later simply to determine that it is invalid.

getParameterMap()

No description

getReturnType()

No description

getInjectedClasses()

No description

setInjectedClasses($injectedClasses)

No description

Parameters

$injectedClasses

setReturnType($returnType)

No description

Parameters

$returnType

AnnotationData getRawAnnotations()

Get any annotations included in the docblock comment for the implementation method of this command that are not already handled by the primary methods of this class.

Return Value

AnnotationData

replaceRawAnnotations($annotationData)

Replace the annotation data.

Parameters

$annotationData

AnnotationData getAnnotations()

Get any annotations included in the docblock comment, also including default values such as @command. We add in the default @command annotation late, and only in a copy of the annotation data because we use the existance of a @command to indicate that this CommandInfo is a command, and not a hook or anything else.

Return Value

AnnotationData

array|null getAnnotationList(string $name)

Return a specific named annotation for this command as a list.

Parameters

string $name

The name of the annotation.

Return Value

array|null

string|null getAnnotation(string $name)

Return a specific named annotation for this command as a string.

Parameters

string $name

The name of the annotation.

Return Value

string|null

bool hasAnnotation(string $annotation)

Check to see if the specified annotation exists for this command.

Parameters

string $annotation

The name of the annotation.

Return Value

bool

addAnnotation($name, $content)

Save any tag that we do not explicitly recognize in the 'otherAnnotations' map.

Parameters

$name
$content

removeAnnotation($name)

Remove an annotation that was previoudly set.

Parameters

$name

string getDescription()

Get the synopsis of the command (~first line).

Return Value

string

setDescription(string $description)

Set the command description.

Parameters

string $description

The description to set.

hasHelp()

Determine if help was provided for this command info

getHelp()

Get the help text of the command (the description)

setHelp(string $help)

Set the help text for this command.

Parameters

string $help

The help text.

string[] getAliases()

Return the list of aliases for this command.

Return Value

string[]

setAliases(string|string[] $aliases)

Set aliases that can be used in place of the command's primary name.

Parameters

string|string[] $aliases

bool getHidden()

Get hidden status for the command.

Return Value

bool

setHidden(bool $hidden)

Set hidden status. List command omits hidden commands.

Parameters

bool $hidden

string[] getExampleUsages()

Return the examples for this command. This is @usage instead of

Return Value

string[]

Examples

because the later is defined by the phpdoc standard to
be example method calls.

setExampleUsage(string $usage, string $description)

Add an example usage for this command.

Parameters

string $usage

An example of the command, including the command name and all of its example arguments and options.

string $description

An explanation of what the example does.

replaceExampleUsages($usages)

Overwrite all example usages

Parameters

$usages

string[] getTopics()

Return the topics for this command.

Return Value

string[]

ReflectionParameter[] getParameters()

Return the list of refleaction parameters.

Return Value

ReflectionParameter[]

DefaultsWithDescriptions arguments()

Descriptions of commandline arguements for this command.

DefaultsWithDescriptions options()

Descriptions of commandline options for this command.

InputOption[] inputOptions()

Get the inputOptions for the options associated with this CommandInfo object, e.g. via @option annotations, or from $options = ['someoption' => 'defaultvalue'] in the command method parameter list.

Return Value

InputOption[]

protected addImplicitNoOptions()

No description

protected createInputOptions()

No description

string findMatchingOption(string $optionName)

An option might have a name such as 'silent|s'. In this instance, we will allow the @option or @default tag to reference the option only by name (e.g. 'silent' or 's' instead of 'silent|s').

Parameters

string $optionName

Return Value

string

addArgumentDescription($name, $description, $suggestedValues = [])

No description

Parameters

$name
$description
$suggestedValues

addOption($name, $description, $suggestedValues = [], $defaultValue = null)

No description

Parameters

$name
$description
$suggestedValues
$defaultValue

addOptionDescription($name, $description, $suggestedValues = [], $defaultValue = null) deprecated

deprecated Use addOption() instead.

No description

Parameters

$name
$description
$suggestedValues
$defaultValue

protected addOptionOrArgumentDescription(DefaultsWithDescriptions $set, $variableName, $description, $suggestedValues = [], $defaultFromParameter = null)

No description

Parameters

DefaultsWithDescriptions $set
$variableName
$description
$suggestedValues
$defaultFromParameter

protected splitOutDefault($description)

No description

Parameters

$description

protected string findOptionAmongAlternatives(string $optionName)

No description

Parameters

string $optionName

Return Value

string

protected string|null findExistingOption(string $optionName)

No description

Parameters

string $optionName

Return Value

string|null

protected array determineAgumentClassifications()

Examine the parameters of the method for this command, and build a list of commandline arguments for them.

Return Value

array

protected addParameterToResult($result, $param)

Examine the provided parameter, and determine whether it is a parameter that will be filled in with a positional commandline argument.

Parameters

$result
$param

protected array determineOptionsFromParameters()

Examine the parameters of the method for this command, and determine the disposition of the options from them.

Return Value

array

protected lastParameterIsOptionsArray()

Determine if the last argument contains $options.

Two forms indicate options:

  • $options = []
  • $options = ['flag' => 'default-value']

Any other form, including array $foo, is not options.

protected bool isAssoc(array $arr)

Helper; determine if an array is associative or not. An array is not associative if its keys are numeric, and numbered sequentially from zero. All other arrays are considered to be associative.

Parameters

array $arr

The array

Return Value

bool

protected string convertName(string $camel)

Convert from a method name to the corresponding command name. A method 'fooBar' will become 'foo:bar', and 'fooBarBazBoz' will become 'foo:bar-baz-boz'.

Parameters

string $camel

method name.

Return Value

string

protected convertArgumentName($camel)

Convert an argument name from snake_case or camelCase to a hyphenated-string.

Parameters

$camel

protected camelToSnake($camel, $splitter = '_')

Convert a camelCase string to a snake_case string.

Parameters

$camel
$splitter

protected requireConsistentState()

Guard against invalid usage of CommandInfo during parsing.

protected parseDocBlock()

Parse the docBlock comment for this command, and set the fields of this class with the data thereby obtained.

static protected convertListToCommaSeparated($text)

Given a list that might be 'a b c' or 'a, b, c' or 'a,b,c', convert the data into the last of these forms.

Parameters

$text