Argument.php 5.6 KB

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