Skip to content

[Console] Add support for invokable commands and input attributes #20553

Open
@chalasr

Description

@chalasr
Member
Q A
Feature PR symfony/symfony#59340
PR author(s) @yceruto
Merged in 7.3

We created this issue to not forget to document this new feature. We would really appreciate if you can help us with this task. If you are not sure how to do it, please ask us and we will help you.

To fix this issue, please create a PR against the 7.3 branch in the symfony-docs repository.

Thank you! 😃

Activity

chalasr

chalasr commented on Jan 11, 2025

@chalasr
MemberAuthor

needs to be taken into account when tackling this:

added this to the 7.3 milestone on Jan 11, 2025
yceruto

yceruto commented on Jan 25, 2025

@yceruto
Member
tacman

tacman commented on Feb 13, 2025

@tacman
Contributor

Until the documentation is written, is there an example of a trivial command using input attributes? Thx.

chalasr

chalasr commented on Feb 13, 2025

@chalasr
MemberAuthor

@tacman You can find one in the code PR: symfony/symfony#59340

tacman

tacman commented on Feb 13, 2025

@tacman
Contributor

Here's a working example:

symfony new --version=next --webapp test-console7.3 && cd test-console7.3 
bin/console make:command lucky:number  

cat > src/Command/LuckyNumberCommand.php <<'END'
<?php

namespace App\Command;

use Symfony\Component\Console\Attribute\Argument;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'lucky:number')]
class LuckyNumberCommand extends Command
{
    public function __construct(
    )
    {
        parent::__construct();
    }

    public function __invoke(SymfonyStyle $io,
                             #[Argument] string $name,
                             #[Option] bool $formal=true): void
    {
        $io->title(sprintf('%s %s!', $formal ? 'Hello' : 'Hey', ucfirst($name)));
        $io->success(sprintf('Today\'s Lucky Number: %d', rand(0, 400)));
    }
}
END
bin/console lucky:number bob
yceruto

yceruto commented on Mar 4, 2025

@yceruto
Member

I've started looking into this and have a question: should we replace the old signature everywhere or just add a new section for the new one?

OskarStark

OskarStark commented on Mar 4, 2025

@OskarStark
Contributor

I would just add a new one

alamirault

alamirault commented on Mar 18, 2025

@alamirault
Contributor

I think the new attribute approach will be the new standard right ?
(readonly and final classes, no inheritance, same DX than for controllers)

Newcomers to symfony shouldn't see it first ? (to see how powerfull is it)

yceruto

yceruto commented on Mar 19, 2025

@yceruto
Member

I’d go with @alamirault’s view on this, but let’s wait a bit to hear other opinions.

OskarStark

OskarStark commented on Mar 19, 2025

@OskarStark
Contributor

It is not feature complete now compared to the old way, so I would still propose the old version first.

@chalasr what do you think?

2 remaining items

TomasVotruba

TomasVotruba commented on Mar 21, 2025

@TomasVotruba

Love this feature, amazing work @yceruto 👏

@chalasr thanks for sharing your slides, that got me into this :)

FYI, I'm working on @rectorphp rule, so people can use the day Symfony 7.3 is out:
rectorphp/rector-symfony#707

It's WIP, so any documentation PR would be helpful to understand the new concept 🙏

yceruto

yceruto commented on Mar 29, 2025

@yceruto
Member

Also #20838

javiereguiluz

javiereguiluz commented on Apr 16, 2025

@javiereguiluz
Member

My vote would be to update ALL code examples to use the new and modern way of creating commands. I'd also add a secondary section in the main /console page showing how to define commands and add argument/options (and get their values in code) using the previous base class and methods.

xabbuh

xabbuh commented on Apr 17, 2025

@xabbuh
Member

I think I agree with Yonel and Javier.

yceruto

yceruto commented on Apr 17, 2025

@yceruto
Member

I think we have a consensus, so I will do my best to propose the doc improvements before the release!

garak

garak commented on Apr 26, 2025

@garak
Contributor

I guess that this needs attention too: https://symfony.com/doc/7.3/console.html#testing-commands

yceruto

yceruto commented on Apr 28, 2025

@yceruto
Member

@garak Good point! However, nothing changes when it comes to E2E testing for console commands (which is exactly what the code in the link you shared covers: https://symfony.com/doc/7.3/console.html#testing-commands).

Note that for all cases, an invokable command will be wrapped into a Command instance (via setCode() method), so this line won't resolve the invokable command service but its Command instance instead:

$command = $application->find('app:hello');
linked a pull request that will close this issue on May 1, 2025
yceruto

yceruto commented on May 1, 2025

@yceruto
Member

Let's start with something #20932 help wanted

added
hasPRA Pull Request has already been submitted for this issue.
on May 1, 2025
chalasr

chalasr commented on May 9, 2025

@chalasr
MemberAuthor

Maybe to take into account: symfony/symfony#60389

chalasr

chalasr commented on May 9, 2025

@chalasr
MemberAuthor
chalasr

chalasr commented on Jun 13, 2025

@chalasr
MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConsolehasPRA Pull Request has already been submitted for this issue.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @javiereguiluz@garak@tacman@TomasVotruba@OskarStark

      Issue actions

        [Console] Add support for invokable commands and input attributes · Issue #20553 · symfony/symfony-docs