|
| 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\Completion\CompletionInput; |
| 15 | +use Symfony\Component\Console\Completion\Suggestion; |
| 16 | +use Symfony\Component\Console\Exception\LogicException; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | + |
| 20 | +#[\Attribute(\Attribute::TARGET_PARAMETER)] |
| 21 | +class Option |
| 22 | +{ |
| 23 | + private const ALLOWED_TYPES = ['string', 'bool', 'int', 'float', 'array']; |
| 24 | + |
| 25 | + private ?int $mode = null; |
| 26 | + private string $typeName = ''; |
| 27 | + |
| 28 | + /** |
| 29 | + * Represents a console command --option definition. |
| 30 | + * |
| 31 | + * If unset, the `name` and `default` values will be inferred from the parameter definition. |
| 32 | + * |
| 33 | + * @param array|string|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
| 34 | + * @param scalar|array|null $default The default value (must be null for self::VALUE_NONE) |
| 35 | + * @param array|callable-string(CompletionInput):list<string|Suggestion> $suggestedValues The values used for input completion |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + public string $name = '', |
| 39 | + public array|string|null $shortcut = null, |
| 40 | + public string $description = '', |
| 41 | + public string|bool|int|float|array|null $default = null, |
| 42 | + public array|string $suggestedValues = [], |
| 43 | + ) { |
| 44 | + if (\is_string($suggestedValues) && !\is_callable($suggestedValues)) { |
| 45 | + throw new \TypeError(\sprintf('Argument 5 passed to "%s()" must be either an array or a callable-string.', __METHOD__)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @internal |
| 51 | + */ |
| 52 | + public static function tryFrom(\ReflectionParameter $parameter): ?self |
| 53 | + { |
| 54 | + /** @var self $self */ |
| 55 | + if (null === $self = ($parameter->getAttributes(self::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null)?->newInstance()) { |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + $type = $parameter->getType(); |
| 60 | + $name = $parameter->getName(); |
| 61 | + |
| 62 | + if (!$type instanceof \ReflectionNamedType) { |
| 63 | + throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported for command options.', $name)); |
| 64 | + } |
| 65 | + |
| 66 | + $self->typeName = $type->getName(); |
| 67 | + |
| 68 | + if (!\in_array($self->typeName, self::ALLOWED_TYPES, true)) { |
| 69 | + throw new LogicException(\sprintf('The type "%s" of parameter "$%s" is not supported as a command option. Only "%s" types are allowed.', $self->typeName, $name, implode('", "', self::ALLOWED_TYPES))); |
| 70 | + } |
| 71 | + |
| 72 | + if (!$self->name) { |
| 73 | + $self->name = $name; |
| 74 | + } |
| 75 | + |
| 76 | + if ('bool' === $self->typeName) { |
| 77 | + $self->mode = InputOption::VALUE_NONE | InputOption::VALUE_NEGATABLE; |
| 78 | + } else { |
| 79 | + $self->mode = null !== $self->default || $parameter->isDefaultValueAvailable() ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_REQUIRED; |
| 80 | + if ('array' === $self->typeName) { |
| 81 | + $self->mode |= InputOption::VALUE_IS_ARRAY; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (InputOption::VALUE_NONE === (InputOption::VALUE_NONE & $self->mode)) { |
| 86 | + $self->default = null; |
| 87 | + } else { |
| 88 | + $self->default ??= $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null; |
| 89 | + } |
| 90 | + |
| 91 | + if (\is_array($self->suggestedValues) && !\is_callable($self->suggestedValues) && 2 === \count($self->suggestedValues) && ($instance = $parameter->getDeclaringFunction()->getClosureThis()) && $instance::class === $self->suggestedValues[0] && \is_callable([$instance, $self->suggestedValues[1]])) { |
| 92 | + $self->suggestedValues = [$instance, $self->suggestedValues[1]]; |
| 93 | + } |
| 94 | + |
| 95 | + return $self; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @internal |
| 100 | + */ |
| 101 | + public function toInputOption(): InputOption |
| 102 | + { |
| 103 | + $suggestedValues = \is_callable($this->suggestedValues) ? ($this->suggestedValues)(...) : $this->suggestedValues; |
| 104 | + |
| 105 | + return new InputOption($this->name, $this->shortcut, $this->mode, $this->description, $this->default, $suggestedValues); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @internal |
| 110 | + */ |
| 111 | + public function resolveValue(InputInterface $input): mixed |
| 112 | + { |
| 113 | + if ('bool' === $this->typeName) { |
| 114 | + return $input->hasOption($this->name) && null !== $input->getOption($this->name) ? $input->getOption($this->name) : ($this->default ?? false); |
| 115 | + } |
| 116 | + |
| 117 | + return $input->hasOption($this->name) ? $input->getOption($this->name) : null; |
| 118 | + } |
| 119 | +} |
0 commit comments