| 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\Exception\InvalidArgumentException; |
| 16 |
use Symfony\Component\Console\Exception\LogicException; |
| 17 |
use Symfony\Component\Console\Input\InputInterface; |
| 18 |
use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 19 |
use Symfony\Component\Console\Question\Question; |
| 20 |
use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 |
|
| 22 |
#[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] |
| 23 |
class Ask implements InteractiveAttributeInterface |
| 24 |
{ |
| 25 |
public ?\Closure $normalizer; |
| 26 |
public ?\Closure $validator; |
| 27 |
private \Closure $closure; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param string $question The question to ask the user |
| 31 |
* @param string|bool|int|float|null $default The default answer to return if the user enters nothing |
| 32 |
* @param bool $hidden Whether the user response must be hidden or not |
| 33 |
* @param bool $multiline Whether the user response should accept newline characters |
| 34 |
* @param bool $trimmable Whether the user response must be trimmed or not |
| 35 |
* @param int|null $timeout The maximum time the user has to answer the question in seconds |
| 36 |
* @param callable|null $validator The validator for the question |
| 37 |
* @param int|null $maxAttempts The maximum number of attempts allowed to answer the question. |
| 38 |
* Null means an unlimited number of attempts |
| 39 |
*/ |
| 40 |
public function __construct( |
| 41 |
public string $question, |
| 42 |
public string|bool|int|float|null $default = null, |
| 43 |
public bool $hidden = false, |
| 44 |
public bool $multiline = false, |
| 45 |
public bool $trimmable = true, |
| 46 |
public ?int $timeout = null, |
| 47 |
?callable $normalizer = null, |
| 48 |
?callable $validator = null, |
| 49 |
public ?int $maxAttempts = null, |
| 50 |
) { |
| 51 |
$this->normalizer = $normalizer ? $normalizer(...) : null; |
| 52 |
$this->validator = $validator ? $validator(...) : null; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @internal |
| 57 |
*/ |
| 58 |
public static function tryFrom(\ReflectionParameter|\ReflectionProperty $member, string $name): ?self |
| 59 |
{ |
| 60 |
$reflection = new ReflectionMember($member); |
| 61 |
|
| 62 |
if (!$self = $reflection->getAttribute(self::class)) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$type = $reflection->getType(); |
| 67 |
|
| 68 |
if (!$type instanceof \ReflectionNamedType) { |
| 69 |
throw new LogicException(\sprintf('The %s "$%s" of "%s" must have a named type. Untyped, Union or Intersection types are not supported for interactive questions.', $reflection->getMemberName(), $name, $reflection->getSourceName())); |
| 70 |
} |
| 71 |
|
| 72 |
$self->closure = function (SymfonyStyle $io, InputInterface $input) use ($self, $reflection, $name, $type) { |
| 73 |
if ($reflection->isProperty() && isset($this->{$reflection->getName()})) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
if ($reflection->isParameter() && !\in_array($input->getArgument($name), [null, []], true)) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
if ('bool' === $type->getName()) { |
| 82 |
$self->default ??= false; |
| 83 |
|
| 84 |
if (!\is_bool($self->default)) { |
| 85 |
throw new LogicException(\sprintf('The "%s::$default" value for the %s "$%s" of "%s" must be a boolean.', self::class, $reflection->getMemberName(), $name, $reflection->getSourceName())); |
| 86 |
} |
| 87 |
|
| 88 |
$question = new ConfirmationQuestion($self->question, $self->default); |
| 89 |
} else { |
| 90 |
$question = new Question($self->question, $self->default); |
| 91 |
} |
| 92 |
$question->setHidden($self->hidden); |
| 93 |
$question->setMultiline($self->multiline); |
| 94 |
$question->setTrimmable($self->trimmable); |
| 95 |
$question->setTimeout($self->timeout); |
| 96 |
|
| 97 |
if (!$self->validator && $reflection->isProperty() && 'array' !== $type->getName()) { |
| 98 |
$self->validator = fn (mixed $value): mixed => $this->{$reflection->getName()} = $value; |
| 99 |
} |
| 100 |
|
| 101 |
$question->setValidator($self->validator); |
| 102 |
$question->setMaxAttempts($self->maxAttempts); |
| 103 |
|
| 104 |
if ($self->normalizer) { |
| 105 |
$question->setNormalizer($self->normalizer); |
| 106 |
} elseif (is_subclass_of($type->getName(), \BackedEnum::class)) { |
| 107 |
/** @var class-string<\BackedEnum> $backedType */ |
| 108 |
$backedType = $reflection->getType()->getName(); |
| 109 |
$question->setNormalizer(static fn (string|int $value) => $backedType::tryFrom($value) ?? throw InvalidArgumentException::fromEnumValue($reflection->getName(), $value, array_column($backedType::cases(), 'value'))); |
| 110 |
} |
| 111 |
|
| 112 |
if ('array' === $type->getName()) { |
| 113 |
$value = []; |
| 114 |
while ($v = $io->askQuestion($question)) { |
| 115 |
if ("\x4" === $v || \PHP_EOL === $v || ($question->isTrimmable() && '' === $v = trim($v))) { |
| 116 |
break; |
| 117 |
} |
| 118 |
$value[] = $v; |
| 119 |
} |
| 120 |
} else { |
| 121 |
$value = $io->askQuestion($question); |
| 122 |
} |
| 123 |
|
| 124 |
if (null === $value && !$reflection->isNullable()) { |
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
if ($reflection->isProperty()) { |
| 129 |
$this->{$reflection->getName()} = $value; |
| 130 |
} else { |
| 131 |
$input->setArgument($name, $value); |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
return $self; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @internal |
| 140 |
*/ |
| 141 |
public function getFunction(object $instance): \ReflectionFunction |
| 142 |
{ |
| 143 |
return new \ReflectionFunction($this->closure->bindTo($instance, $instance::class)); |
| 144 |
} |
| 145 |
} |