-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Console] Add support for interactive invokable commands with #[Interact] and #[Ask] attributes
#61748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[Console] Add support for interactive invokable commands with #[Interact] and #[Ask] attributes
#61748
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Console\Attribute; | ||
|
|
||
| use Symfony\Component\Console\Attribute\Reflection\ReflectionMember; | ||
| use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Question\Question; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
|
||
| #[\Attribute(\Attribute::TARGET_PARAMETER | \Attribute::TARGET_PROPERTY)] | ||
| class Ask implements InteractiveAttributeInterface | ||
| { | ||
| public ?\Closure $validator; | ||
| private \Closure $closure; | ||
|
|
||
| /** | ||
| * @param string $question The question to ask the user | ||
| * @param string|bool|int|float|null $default The default answer to return if the user enters nothing | ||
| * @param bool $hidden Whether the user response must be hidden or not | ||
| * @param bool $multiline Whether the user response should accept newline characters | ||
| * @param bool $trimmable Whether the user response must be trimmed or not | ||
| * @param int|null $timeout The maximum time the user has to answer the question in seconds | ||
| * @param callable|null $validator The validator for the question | ||
| * @param int|null $maxAttempts The maximum number of attempts allowed to answer the question. | ||
| * Null means an unlimited number of attempts | ||
| */ | ||
| public function __construct( | ||
| public string $question, | ||
| public string|bool|int|float|null $default = null, | ||
| public bool $hidden = false, | ||
| public bool $multiline = false, | ||
| public bool $trimmable = true, | ||
| public ?int $timeout = null, | ||
| ?callable $validator = null, | ||
| public ?int $maxAttempts = null, | ||
| ) { | ||
| $this->validator = $validator ? $validator(...) : null; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| public static function tryFrom(\ReflectionParameter|\ReflectionProperty $member, string $name): ?self | ||
| { | ||
| $reflection = new ReflectionMember($member); | ||
|
|
||
| if (!$self = $reflection->getAttribute(self::class)) { | ||
| return null; | ||
| } | ||
|
|
||
| $self->closure = function (SymfonyStyle $io, InputInterface $input) use ($self, $reflection, $name) { | ||
| if (($reflection->isProperty() && isset($this->{$reflection->getName()})) || ($reflection->isParameter() && null !== $input->getArgument($name))) { | ||
| return; | ||
| } | ||
|
|
||
| $question = new Question($self->question, $self->default); | ||
| $question->setHidden($self->hidden); | ||
| $question->setMultiline($self->multiline); | ||
| $question->setTrimmable($self->trimmable); | ||
| $question->setTimeout($self->timeout); | ||
|
|
||
| if (!$self->validator && $reflection->isProperty()) { | ||
| $self->validator = function (mixed $value) use ($reflection): mixed { | ||
| return $this->{$reflection->getName()} = $value; | ||
| }; | ||
| } | ||
|
|
||
| $question->setValidator($self->validator); | ||
| $question->setMaxAttempts($self->maxAttempts); | ||
|
|
||
| if ($reflection->isBackedEnumType()) { | ||
| /** @var class-string<\BackedEnum> $backedType */ | ||
| $backedType = $reflection->getType()->getName(); | ||
| $question->setNormalizer(fn (string|int $value) => $backedType::tryFrom($value) ?? throw InvalidArgumentException::fromEnumValue($reflection->getName(), $value, array_map(fn (\BackedEnum $enum): string|int => $enum->value, $backedType::cases()))); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $value = $io->askQuestion($question); | ||
|
|
||
| if (null === $value && !$reflection->isNullable()) { | ||
| return; | ||
| } | ||
|
|
||
| if ($reflection->isProperty()) { | ||
| $this->{$reflection->getName()} = $value; | ||
| } else { | ||
| $input->setArgument($name, $value); | ||
| } | ||
| }; | ||
|
|
||
| return $self; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| public function getFunction(object $instance): \ReflectionFunction | ||
| { | ||
| return new \ReflectionFunction($this->closure->bindTo($instance, $instance::class)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Console\Attribute; | ||
|
|
||
| use Symfony\Component\Console\Exception\LogicException; | ||
|
|
||
| #[\Attribute(\Attribute::TARGET_METHOD)] | ||
| class Interact implements InteractiveAttributeInterface | ||
| { | ||
| private \ReflectionMethod $method; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| public static function tryFrom(\ReflectionMethod $method): ?self | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /** @var self|null $self */ | ||
| if (!$self = ($method->getAttributes(self::class)[0] ?? null)?->newInstance()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!$method->isPublic() || $method->isStatic()) { | ||
| throw new LogicException(\sprintf('The interactive method "%s::%s()" must be public and non-static.', $method->getDeclaringClass()->getName(), $method->getName())); | ||
| } | ||
|
|
||
| if ('__invoke' === $method->getName()) { | ||
| throw new LogicException(\sprintf('The "%s::__invoke()" method cannot be used as an interactive method.', $method->getDeclaringClass()->getName())); | ||
| } | ||
|
|
||
| $self->method = $method; | ||
|
|
||
| return $self; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| public function getFunction(object $instance): \ReflectionFunction | ||
| { | ||
| return new \ReflectionFunction($this->method->getClosure($instance)); | ||
| } | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/Console/Attribute/InteractiveAttributeInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\Console\Attribute; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| interface InteractiveAttributeInterface | ||
| { | ||
| public function getFunction(object $instance): \ReflectionFunction; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.