-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Allow commands to provide a default name for compile time registration #23887
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
Conversation
chalasr
commented
Aug 14, 2017
Q | A |
---|---|
Branch? | 3.4 |
Bug fix? | no |
New feature? | yes |
BC breaks? | no |
Deprecations? | no |
Tests pass? | yes |
Fixed tickets | #23796 |
License | MIT |
Doc PR | symfony/symfony-docs#8147 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use psr4+this to register existing commands?
$commandName = $tags[0]['command']; | ||
if (!isset($tags[0]['command'])) { | ||
$defaultNameProvider = array($class, 'getDefaultName'); | ||
$commandName = $defaultNameProvider(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$class::getDefaultName()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
c7f9137
to
ba4be99
Compare
@nicolas-grekas That would require to make them autowired, right? |
Correct, bad idea :) |
2658662
to
bd71187
Compare
@@ -55,7 +56,7 @@ public function process(ContainerBuilder $container) | |||
|
|||
$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class)); | |||
|
|||
if (!isset($tags[0]['command'])) { | |||
if (!isset($tags[0]['command']) && !$r->implementsInterface(DefaultNameProviderInterface::class)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we deprecate this behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would mean deprecating non-lazy command services, deserves a dedicated discussion I think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm right. Could application as a service be a next step? So we can simply wire via add method call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should stop here IMHO. We don't do changes for the sake of it, so that if we force people to move on, the benefit must be clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Just saying we can then cleanly wire commands, the command loader etc. The benefit would be its more flexible and allows injecting the application elsewhere... that may be useful.
interface DefaultNameProviderInterface | ||
{ | ||
/** | ||
* @return string The name to which register the implementing Command |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name to use by default for calling the Command
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
bd71187
to
5d9ae6b
Compare
What about making it easier to define the default name of a command, by asking people to just fill in a Here is a commit doing this to see what it could look like: 05c922a Note that this also "fixes" an issue with inheritance, where the return value of |
This is enough to convince me 👍 |
106f353
to
7784b1b
Compare
@@ -29,6 +29,8 @@ | |||
*/ | |||
class Command | |||
{ | |||
protected static $defaultName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a docblock here to help people discover how this can be leveraged, and all good on my side
@@ -55,7 +55,7 @@ public function process(ContainerBuilder $container) | |||
|
|||
$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class)); | |||
|
|||
if (!isset($tags[0]['command'])) { | |||
if (!isset($tags[0]['command']) && !$commandName = $class::getDefaultName()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&& null === $commandName = ...
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, what should happen when $commandName === '' ? Registering the empty string is a no-go to me. As is LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an empty string would give the expected unexpected result :) "0"
could be a valid name. Anyway... this is nitpicking; fine asis for me 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supporting the 0
command name will be asked by @Tobion anyway 😂, so let's support it now.
@@ -46,6 +48,17 @@ class Command | |||
private $helperSet; | |||
|
|||
/** | |||
* @return string|null The name to use by default for calling the Command or null when no default name is set | |||
*/ | |||
public static function getDefaultName() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final?
e431d33
to
96411e8
Compare
@nicolas-grekas applied |
3a54ff8
to
eda7d42
Compare
Thank you @chalasr. |
… compile time registration (chalasr, nicolas-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [Console] Allow commands to provide a default name for compile time registration | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23796 | License | MIT | Doc PR | symfony/symfony-docs#8147 Commits ------- eda7d42 [Console] Add protected static $defaultName to set the default name of a Command 5d9ae6b [Console] Allow commands to provide a default name for compile time registration
Just in case I'm missing something. |
@mvrhov Just tried and no issue on my side. Defining this property and registering the command as a |
Oh, I understood that it doesn't need to load them anymore no matter what. I've run the |
running just |
@stof: Yeah I know. Sorry for not being clear enough. |
Due to some logic in Symfony "Command" the default name of extended commands is not used in the extending command. So we need to copy all of them. See doctrine/migrations#839 And symfony/symfony#23887
This PR was merged into the 1.8-dev branch. Discussion ---------- | Q | A | --------------- | ----- | Branch? | master | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Related tickets | | License | MIT This commit is about of a little optimisation to avoid initialisation of all command services on each executing `bin/console` command. Since Symfony 3.4 console commands can be lazy thanks to [a static name property](symfony/symfony#23887). This change does not break BC because the BC layer is already [in the base command's constructor](https://github.com/symfony/symfony/pull/23887/files#diff-8e80595663798837db1b033187835535R75). Commits ------- 91c9918 Make console commands lazy