| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\Attribute; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Attribute\Reflection\ReflectionMember; |
| 15 |
use Symfony\Component\Console\Completion\CompletionInput; |
| 16 |
use Symfony\Component\Console\Completion\Suggestion; |
| 17 |
use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 18 |
use Symfony\Component\Console\Exception\LogicException; |
| 19 |
use Symfony\Component\Console\Input\InputArgument; |
| 20 |
use Symfony\Component\Console\Input\InputInterface; |
| 21 |
use Symfony\Component\String\UnicodeString; |
| 22 |
|
| 23 |
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] |
| 24 |
class Argument |
| 25 |
{ |
| 26 |
private const ALLOWED_TYPES = ['string', 'bool', 'int', 'float', 'array']; |
| 27 |
|
| 28 |
private string|bool|int|float|array|null $default = null; |
| 29 |
private array|\Closure $suggestedValues; |
| 30 |
private ?int $mode = null; |
| 31 |
/** |
| 32 |
* @var string|class-string<\BackedEnum> |
| 33 |
*/ |
| 34 |
private string $typeName = ''; |
| 35 |
private ?InteractiveAttributeInterface $interactiveAttribute = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* Represents a console command <argument> definition. |
| 39 |
* |
| 40 |
* If unset, the `name` value will be inferred from the parameter definition. |
| 41 |
* |
| 42 |
* @param array<string|Suggestion>|callable(CompletionInput):list<string|Suggestion> $suggestedValues The values used for input completion |
| 43 |
*/ |
| 44 |
public function __construct( |
| 45 |
public string $description = '', |
| 46 |
public string $name = '', |
| 47 |
array|callable $suggestedValues = [], |
| 48 |
) { |
| 49 |
$this->suggestedValues = \is_callable($suggestedValues) ? $suggestedValues(...) : $suggestedValues; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @internal |
| 54 |
*/ |
| 55 |
public static function tryFrom(\ReflectionParameter|\ReflectionProperty $member): ?self |
| 56 |
{ |
| 57 |
$reflection = new ReflectionMember($member); |
| 58 |
|
| 59 |
if (!$self = $reflection->getAttribute(self::class)) { |
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
$type = $reflection->getType(); |
| 64 |
$name = $reflection->getName(); |
| 65 |
|
| 66 |
if (!$type instanceof \ReflectionNamedType) { |
| 67 |
throw new LogicException(\sprintf('The %s "$%s" of "%s" must have a named type. Untyped, Union or Intersection types are not supported for command arguments.', $reflection->getMemberName(), $name, $reflection->getSourceName())); |
| 68 |
} |
| 69 |
|
| 70 |
$self->typeName = $type->getName(); |
| 71 |
$isBackedEnum = is_subclass_of($self->typeName, \BackedEnum::class); |
| 72 |
|
| 73 |
if (!\in_array($self->typeName, self::ALLOWED_TYPES, true) && !$isBackedEnum) { |
| 74 |
throw new LogicException(\sprintf('The type "%s" on %s "$%s" of "%s" is not supported as a command argument. Only "%s" types and backed enums are allowed.', $self->typeName, $reflection->getMemberName(), $name, $reflection->getSourceName(), implode('", "', self::ALLOWED_TYPES))); |
| 75 |
} |
| 76 |
|
| 77 |
if (!$self->name) { |
| 78 |
$self->name = (new UnicodeString($name))->kebab(); |
| 79 |
} |
| 80 |
|
| 81 |
$self->default = $reflection->hasDefaultValue() ? $reflection->getDefaultValue() : null; |
| 82 |
|
| 83 |
$isOptional = $reflection->hasDefaultValue() || $reflection->isNullable(); |
| 84 |
$self->mode = $isOptional ? InputArgument::OPTIONAL : InputArgument::REQUIRED; |
| 85 |
if ('array' === $self->typeName) { |
| 86 |
$self->mode |= InputArgument::IS_ARRAY; |
| 87 |
} |
| 88 |
|
| 89 |
if (\is_array($self->suggestedValues) && !\is_callable($self->suggestedValues) && 2 === \count($self->suggestedValues) && ($instance = $reflection->getSourceThis()) && $instance::class === $self->suggestedValues[0] && \is_callable([$instance, $self->suggestedValues[1]])) { |
| 90 |
// In case that the callback is declared as a static method `[Foo::class, 'methodName']` - yet it is not callable, |
| 91 |
// while non-static method `[Foo $instance, 'methodName']` would be callable, we transform the callback on the fly into a non-static version. |
| 92 |
$self->suggestedValues = [$instance, $self->suggestedValues[1]]; |
| 93 |
} |
| 94 |
|
| 95 |
if ($isBackedEnum && !$self->suggestedValues) { |
| 96 |
$self->suggestedValues = array_column($self->typeName::cases(), 'value'); |
| 97 |
} |
| 98 |
|
| 99 |
$self->interactiveAttribute = Ask::tryFrom($member, $self->name); |
| 100 |
|
| 101 |
if ($self->interactiveAttribute && $isOptional) { |
| 102 |
throw new LogicException(\sprintf('The %s "$%s" argument of "%s" cannot be both interactive and optional.', $reflection->getMemberName(), $self->name, $reflection->getSourceName())); |
| 103 |
} |
| 104 |
|
| 105 |
return $self; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @internal |
| 110 |
*/ |
| 111 |
public function toInputArgument(): InputArgument |
| 112 |
{ |
| 113 |
$suggestedValues = \is_callable($this->suggestedValues) ? ($this->suggestedValues)(...) : $this->suggestedValues; |
| 114 |
|
| 115 |
return new InputArgument($this->name, $this->mode, $this->description, $this->default, $suggestedValues); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @internal |
| 120 |
*/ |
| 121 |
public function resolveValue(InputInterface $input): mixed |
| 122 |
{ |
| 123 |
$value = $input->getArgument($this->name); |
| 124 |
|
| 125 |
if (is_subclass_of($this->typeName, \BackedEnum::class) && (\is_string($value) || \is_int($value))) { |
| 126 |
return $this->typeName::tryFrom($value) ?? throw InvalidArgumentException::fromEnumValue($this->name, $value, $this->suggestedValues); |
| 127 |
} |
| 128 |
|
| 129 |
return $value; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @internal |
| 134 |
*/ |
| 135 |
public function getInteractiveAttribute(): ?InteractiveAttributeInterface |
| 136 |
{ |
| 137 |
return $this->interactiveAttribute; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @internal |
| 142 |
*/ |
| 143 |
public function isRequired(): bool |
| 144 |
{ |
| 145 |
return InputArgument::REQUIRED === (InputArgument::REQUIRED & $this->mode); |
| 146 |
} |
| 147 |
} |