Support invokable console commands#91
Open
maryo wants to merge 4 commits into
Open
Conversation
Services no longer need to extend Symfony's Command class to be registered as console commands. A service is now treated as one if it extends Command, carries the "console.command" tag, or is annotated with #[AsCommand] - the latter two are wrapped into a synthetic "<service>.command" Command instance via Command::setCode(), mirroring Symfony's own AddConsoleCommandPass. Also adds support for #[AsCommand]'s "usages" (extra usage examples shown in --help).
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #91 +/- ##
==========================================
- Coverage 96.07% 94.65% -1.43%
==========================================
Files 5 5
Lines 102 131 +29
==========================================
+ Hits 98 124 +26
- Misses 4 7 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Author
|
After looking at the Codecov report, I bumped the minimum required version of |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Symfony 7.3 introduced invokable commands - plain classes with a public
__invoke()method that don't extendCommandat all. This PR adds support for that.What changed
ConsoleExtensionno longer only looks for services viafindByType(Command::class). A service is now registered as a console command if it meets at least one of these conditions:Symfony\Component\Console\Command\Command(unchanged, existing behavior)console.commandtag#[AsCommand]Services that don't extend
Commandare wrapped into a syntheticconsole.<service>.commandCommandinstance viaCommand::setCode(), the same way Symfony's ownAddConsoleCommandPassdoes it for the framework bundle. If a service is tagged/annotated but neither extendsCommandnor has a public__invoke()method, aNette\DI\ServiceCreationExceptionis thrown.Also adds support for
#[AsCommand]'susagesoption (extra usage examples shown in--help).This bumped the minimum required
symfony/consoleversion to^7.4.0(from^7.2.0), since that's when theusagesparameter was added to the attribute.Dropped the
Command::$defaultNameproperty fallback - the currently installedCommandclass (and the whole^7.4.0 || ^8.0.0range we now require) no longer declares that property at all, having replaced it first withgetDefaultName()and later with#[AsCommand], so this branch could only ever fire for a hand-rolled Command subclass still using the pre-Symfony-5.0 convention, which isn't realistic to encounter alongside such a recent symfony/console requirement.Symfony also supports
#[AsCommand]on individual methods, so the method doesn't have to be named__invokeand a single class can expose multiple commands this way. I didn't want to add that complexity here, so I only added support for__invoke.