Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Add support for SignalableCommandInterface with invokable commands #60389

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 1 commit into from
May 9, 2025

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented May 9, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Currently, it's not possible to use the SignalableCommandInterface with invokable commands.
This PR adds support for that.

#[AsCommand(name: 'app:example')]
class ExampleCommand implements SignalableCommandInterface
{
    public function __invoke(InputInterface $input, OutputInterface $output): int
    {
        // ...

        return Command::SUCCESS;
    }

    public function getSubscribedSignals(): array
    {
        return [\SIGINT, \SIGTERM];
    }

    public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false
    {
        // handle signal

        return 0;
    }
}

@HypeMC HypeMC requested a review from chalasr as a code owner May 9, 2025 05:14
@carsonbot carsonbot added this to the 7.3 milestone May 9, 2025
@HypeMC HypeMC force-pushed the invokable-signalable-command branch from b2deac6 to 9767999 Compare May 9, 2025 05:28
@HypeMC HypeMC force-pushed the invokable-signalable-command branch from 9767999 to ae770b7 Compare May 9, 2025 12:10
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor CS fixes and GTM, thanks.

@nicolas-grekas nicolas-grekas added the ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" label May 9, 2025
@yceruto
Copy link
Member

yceruto commented May 9, 2025

I believe there’s another way to keep this working without implementing the interface: https://github.com/symfony/symfony-docs/pull/20932/files#diff-738612fa300cdcb8d044a240dcec7bc9db05a1671f30e3d1b987bbf200037b8c but I might be missing something, could you please confirm?

@nicolas-grekas
Copy link
Member

@yceruto it should work indeed, but the proposed approach makes sense to me also.

@chalasr
Copy link
Member

chalasr commented May 9, 2025

@yceruto I remember we discussed this at the time and you had some arguments against SignalableCommandInterface, namely that the listener approach is better design-wise because less intrusive. I agreed with that and still do but I think either we decide that we don't need this interface at all and then it should be deprecated, or invokable commands should support it

@yceruto
Copy link
Member

yceruto commented May 9, 2025

Yes, I'd prefer a single way to do this for invokable command, less variants to document and maintain.

@HypeMC HypeMC force-pushed the invokable-signalable-command branch from ae770b7 to bb97a2a Compare May 9, 2025 13:05
@HypeMC
Copy link
Contributor Author

HypeMC commented May 9, 2025

@yceruto Just a heads up, symfony/event-dispatcher is an optional dependency of the console component. If not installed, users won't be able to handle signals. I'm not saying that's good or bad, just something to be aware of.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this very issue/PR shows that this style would be expected to work. It might also make the feature easier to use when the component is used standalone.

@yceruto
Copy link
Member

yceruto commented May 9, 2025

I understand the intention of supporting all features used in normal commands, but in standalone usage, the invokable command approach doesn’t work naturally, people need to call explicitly setCode(), name, description, etc. In those cases, you’ll probably extend the Command class directly to avoid the extra burden.

Copy link
Member

@chalasr chalasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the sake of feature parity between invokable and non-invokable commands

@HypeMC
Copy link
Contributor Author

HypeMC commented May 9, 2025

My argument was more about why not to deprecate or remove the SignalableCommandInterface and since it has its use case (when the component is used standalone), it feels counter-intuitive for it not to work with invokable commands.

@yceruto
Copy link
Member

yceruto commented May 9, 2025

In those standalone cases without an event-dispatcher, you can still use signalable invokable commands by extending Command and implementing SignalableCommandInterface. The additional requirement is the manual extension, which is fine in my opinion since these are edge cases.

@yceruto
Copy link
Member

yceruto commented May 9, 2025

If the goal is to simplify support for invokable commands, I suggest keeping this PR as-is with the Command implements SignalableCommandInterface approach part only. This way, if someone wants to use this feature with an invokable command, they only need to extend the Command class, just as we currently recommend for interact() and other advanced use cases.

@chalasr
Copy link
Member

chalasr commented May 9, 2025

@yceruto If you mean we should not support SomeInvokableCommand implements SignalableCommandInterface then I disagree, reducing differences/limitations using invokable commands is worthwhile IMHO. The fact that extending Command is easier with console standalone shouldn't be taken as given I think, might improves later. Adding interact() or initialize() equivalents at some point is not excluded to me, if there's some traction.

@yceruto
Copy link
Member

yceruto commented May 9, 2025

I’m fine with invokable commands not supporting all CLI features out of the box. If extending the Command class is the way to enable them, that’s totally acceptable to me, just like we already do with invokable controllers and AbstractController. I don’t mind extending a class if that’s all it takes to unlock the functionality I need.

@nicolas-grekas
Copy link
Member

Sure, but why make it more complex than expected? I don't see your point sorry.

@chalasr
Copy link
Member

chalasr commented May 9, 2025

Shortcut methods and unsupported features are different stories. Stop me if I'm wrong but there is no AbstractController feature that cannot be implemented the same way by controllers not extending it

@yceruto
Copy link
Member

yceruto commented May 9, 2025

My goal is to make it simpler for developers, but I’m probably wrong in this case, sorry for the noise and confusion @nicolas-grekas.

@nicolas-grekas
Copy link
Member

No worries, it's always useful to have a discussion. Here I don't see much added complexity (I see this as simpler actually, on the Command side) so I'm fine with the change.

@chalasr
Copy link
Member

chalasr commented May 9, 2025

Thank you @HypeMC.

@chalasr chalasr merged commit 00a39e7 into symfony:7.3 May 9, 2025
10 of 11 checks passed
@HypeMC HypeMC deleted the invokable-signalable-command branch May 9, 2025 14:48
@fabpot fabpot mentioned this pull request May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Console Feature ❄️ Feature Freeze Important Pull Requests to finish before the next Symfony "feature freeze" Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants