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

Skip to content

[Console] Simplify using invokable commands when the component is used standalone #60394

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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from

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

Inspired by #60389 (comment) .

This PR enables using invokable command when the component is used standalone:

#[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;
    }
}

$application = new Application();
$application->addInvokable(new ExampleCommand());
$application->run();

@OskarStark
Copy link
Contributor

So we really need the extra method addInvokable() or could we handle this automatically?

@xabbuh
Copy link
Member

xabbuh commented May 10, 2025

We cannot widen the argument type in add() as that would be a BC break for child classes overriding the method.

@HypeMC HypeMC force-pushed the simplify-invokable-commands-when-standalone branch from e07ce18 to 5cf2a0d Compare May 10, 2025 08:33
@OskarStark
Copy link
Contributor

We could remove it and handle it internally, couldn't we?

Do we need to adjust addCommands() method as well?

@chalasr
Copy link
Member

chalasr commented May 11, 2025

Cool, thanks for working on this.

We could remove it and handle it internally, couldn't we?

@OskarStark If you mean removing the type declaration then it's a BC break too unfortunately, see https://3v4l.org/5dhmc.

I'd prefer the invokable concept not to leak into public API though. What about adding Application::addCommand(callable|Command $command): Command instead? It would be great if that new method could provide a way to make the command lazy also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants