| 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\InvalidOptionException; |
| 18 |
use Symfony\Component\Console\Exception\LogicException; |
| 19 |
use Symfony\Component\Console\Input\InputInterface; |
| 20 |
use Symfony\Component\Console\Input\InputOption; |
| 21 |
use Symfony\Component\String\UnicodeString; |
| 22 |
|
| 23 |
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] |
| 24 |
class Option |
| 25 |
{ |
| 26 |
private const ALLOWED_TYPES = ['string', 'bool', 'int', 'float', 'array']; |
| 27 |
private const ALLOWED_UNION_TYPES = ['bool|string', 'bool|int', 'bool|float']; |
| 28 |
|
| 29 |
private string|bool|int|float|array|null $default = null; |
| 30 |
private array|\Closure $suggestedValues; |
| 31 |
private ?int $mode = null; |
| 32 |
/** |
| 33 |
* @var string|class-string<\BackedEnum> |
| 34 |
*/ |
| 35 |
private string $typeName = ''; |
| 36 |
private bool $allowNull = false; |
| 37 |
private string $memberName = ''; |
| 38 |
private string $sourceName = ''; |
| 39 |
|
| 40 |
/** |
| 41 |
* Represents a console command --option definition. |
| 42 |
* |
| 43 |
* If unset, the `name` value will be inferred from the parameter definition. |
| 44 |
* |
| 45 |
* @param array|string|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts |
| 46 |
* @param array<string|Suggestion>|callable(CompletionInput):list<string|Suggestion> $suggestedValues The values used for input completion |
| 47 |
*/ |
| 48 |
public function __construct( |
| 49 |
public string $description = '', |
| 50 |
public string $name = '', |
| 51 |
public array|string|null $shortcut = null, |
| 52 |
array|callable $suggestedValues = [], |
| 53 |
) { |
| 54 |
$this->suggestedValues = \is_callable($suggestedValues) ? $suggestedValues(...) : $suggestedValues; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @internal |
| 59 |
*/ |
| 60 |
public static function tryFrom(\ReflectionParameter|\ReflectionProperty $member): ?self |
| 61 |
{ |
| 62 |
$reflection = new ReflectionMember($member); |
| 63 |
|
| 64 |
if (!$self = $reflection->getAttribute(self::class)) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
$self->memberName = $reflection->getMemberName(); |
| 69 |
$self->sourceName = $reflection->getSourceName(); |
| 70 |
|
| 71 |
$name = $reflection->getName(); |
| 72 |
$type = $reflection->getType(); |
| 73 |
|
| 74 |
if (!$reflection->hasDefaultValue()) { |
| 75 |
throw new LogicException(\sprintf('The option %s "$%s" of "%s" must declare a default value.', $self->memberName, $name, $self->sourceName)); |
| 76 |
} |
| 77 |
|
| 78 |
if (!$self->name) { |
| 79 |
$self->name = (new UnicodeString($name))->kebab(); |
| 80 |
} |
| 81 |
|
| 82 |
$self->default = $reflection->getDefaultValue(); |
| 83 |
$self->allowNull = $reflection->isNullable(); |
| 84 |
|
| 85 |
if ($type instanceof \ReflectionUnionType) { |
| 86 |
return $self->handleUnion($type); |
| 87 |
} |
| 88 |
|
| 89 |
if (!$type instanceof \ReflectionNamedType) { |
| 90 |
throw new LogicException(\sprintf('The %s "$%s" of "%s" must have a named type. Untyped or Intersection types are not supported for command options.', $self->memberName, $name, $self->sourceName)); |
| 91 |
} |
| 92 |
|
| 93 |
$self->typeName = $type->getName(); |
| 94 |
$isBackedEnum = is_subclass_of($self->typeName, \BackedEnum::class); |
| 95 |
|
| 96 |
if (!\in_array($self->typeName, self::ALLOWED_TYPES, true) && !$isBackedEnum) { |
| 97 |
throw new LogicException(\sprintf('The type "%s" on %s "$%s" of "%s" is not supported as a command option. Only "%s" types and BackedEnum are allowed.', $self->typeName, $self->memberName, $name, $self->sourceName, implode('", "', self::ALLOWED_TYPES))); |
| 98 |
} |
| 99 |
|
| 100 |
if ('bool' === $self->typeName && $self->allowNull && \in_array($self->default, [true, false], true)) { |
| 101 |
throw new LogicException(\sprintf('The option %s "$%s" of "%s" must not be nullable when it has a default boolean value.', $self->memberName, $name, $self->sourceName)); |
| 102 |
} |
| 103 |
|
| 104 |
if ($self->allowNull && null !== $self->default) { |
| 105 |
throw new LogicException(\sprintf('The option %s "$%s" of "%s" must either be not-nullable or have a default of null.', $self->memberName, $name, $self->sourceName)); |
| 106 |
} |
| 107 |
|
| 108 |
if ('bool' === $self->typeName) { |
| 109 |
$self->mode = InputOption::VALUE_NONE; |
| 110 |
if (false !== $self->default) { |
| 111 |
$self->mode |= InputOption::VALUE_NEGATABLE; |
| 112 |
} |
| 113 |
} elseif ('array' === $self->typeName) { |
| 114 |
$self->mode = InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY; |
| 115 |
} else { |
| 116 |
$self->mode = InputOption::VALUE_REQUIRED; |
| 117 |
} |
| 118 |
|
| 119 |
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]])) { |
| 120 |
$self->suggestedValues = [$instance, $self->suggestedValues[1]]; |
| 121 |
} |
| 122 |
|
| 123 |
if ($isBackedEnum && !$self->suggestedValues) { |
| 124 |
$self->suggestedValues = array_column($self->typeName::cases(), 'value'); |
| 125 |
} |
| 126 |
|
| 127 |
return $self; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @internal |
| 132 |
*/ |
| 133 |
public function toInputOption(): InputOption |
| 134 |
{ |
| 135 |
$default = InputOption::VALUE_NONE === (InputOption::VALUE_NONE & $this->mode) ? null : $this->default; |
| 136 |
$suggestedValues = \is_callable($this->suggestedValues) ? ($this->suggestedValues)(...) : $this->suggestedValues; |
| 137 |
|
| 138 |
return new InputOption($this->name, $this->shortcut, $this->mode, $this->description, $default, $suggestedValues); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @internal |
| 143 |
*/ |
| 144 |
public function resolveValue(InputInterface $input): mixed |
| 145 |
{ |
| 146 |
$value = $input->getOption($this->name); |
| 147 |
|
| 148 |
if (null === $value && \in_array($this->typeName, self::ALLOWED_UNION_TYPES, true)) { |
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
if (is_subclass_of($this->typeName, \BackedEnum::class) && (\is_string($value) || \is_int($value))) { |
| 153 |
return $this->typeName::tryFrom($value) ?? throw InvalidOptionException::fromEnumValue($this->name, $value, $this->suggestedValues); |
| 154 |
} |
| 155 |
|
| 156 |
if ('array' === $this->typeName && $this->allowNull && [] === $value) { |
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
if ('bool' !== $this->typeName) { |
| 161 |
return $value; |
| 162 |
} |
| 163 |
|
| 164 |
if ($this->allowNull && null === $value) { |
| 165 |
return null; |
| 166 |
} |
| 167 |
|
| 168 |
return $value ?? $this->default; |
| 169 |
} |
| 170 |
|
| 171 |
private function handleUnion(\ReflectionUnionType $type): self |
| 172 |
{ |
| 173 |
$types = array_map( |
| 174 |
static fn (\ReflectionType $t) => $t instanceof \ReflectionNamedType ? $t->getName() : null, |
| 175 |
$type->getTypes(), |
| 176 |
); |
| 177 |
|
| 178 |
sort($types); |
| 179 |
|
| 180 |
$this->typeName = implode('|', array_filter($types)); |
| 181 |
|
| 182 |
if (!\in_array($this->typeName, self::ALLOWED_UNION_TYPES, true)) { |
| 183 |
throw new LogicException(\sprintf('The union type for %s "$%s" of "%s" is not supported as a command option. Only "%s" types are allowed.', $this->memberName, $this->name, $this->sourceName, implode('", "', self::ALLOWED_UNION_TYPES))); |
| 184 |
} |
| 185 |
|
| 186 |
if (false !== $this->default) { |
| 187 |
throw new LogicException(\sprintf('The option %s "$%s" of "%s" must have a default value of false.', $this->memberName, $this->name, $this->sourceName)); |
| 188 |
} |
| 189 |
|
| 190 |
$this->mode = InputOption::VALUE_OPTIONAL; |
| 191 |
|
| 192 |
return $this; |
| 193 |
} |
| 194 |
} |