spiral / framework

High-Performance PHP Framework
https://spiral.dev
MIT License
1.8k stars 87 forks source link

Parse command input parameter #1124

Open leon0399 opened 1 month ago

leon0399 commented 1 month ago
Q A
Bugfix?
Breaks BC?
New feature? ✔️
Issues N/A
Docs PR spiral/docs#...

ToDo

This feature is a logical continuation of https://spiral.dev/docs/cookbook-console-validation/current. If this PR is merged, it will now be allowed to configure attributes and options inside the Filter class without the need to duplicate fields and signature.

Currently, this PR is just a showcase of its features and implementation examples. I am willing to rework this PR as needed.

class UserRegisterFilter extends Filter
{
+    #[Argument(description: 'User username')]
    public string $username;

+    #[Argument(description: 'User email address')]
+    #[Question('Provide a valid email for the user')]
    public string $email;

+    #[Option(description: 'Mark as admin')]
    public bool $admin = false;

+    #[Option(key: 'send-verification-email', description: 'Send a verification email to the user')]
    public bool $sendVerificationEmail = false;
}

+#[AsCommand(user:register)]
final class UserRegister extends Command
{
-    protected const SIGNATURE = <<<CMD
-        user:register
-        {username : User username}
-        {email : User email address}
-        {--a|admin : Mark as admin}
-        {--s|send-verification-email : Send a verification email to the user}
-CMD;

-    public function perform(UserRegisterFilter $input): int
+    public function perform(#[AsInput] UserRegisterFilter $input): int
    {
        // ...

        return self::SUCCESS;
    }
}
butschster commented 1 month ago

hey! Looks awesome. I like your idea to use Attributes for mapping input into a DTO. You can continue. I see some todo here.