-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Console] Command as service #3621
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
Changes from 1 commit
947ad92
a055140
a7b916e
cdd534a
e137951
6a7a25f
11bfe50
e8b3320
c8fe610
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
| Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.4+ | Fixed tickets | N/A Command as a service can be useful to give access to services and configuration parameters in the `configure` method. A simple use case: you want to allow the user to set an option's default value in the `app/config/parameters.yml` file. Or the default value needs to be computed by a service (database retrieval for instance). With a `ContainerAwareCommand`, this wouldn't be possible because the `configure` method is called from the constructor, so the container isn't set yet.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,9 +71,12 @@ Register Commands in the Service Container | |
Support for registering commands in the service container was added in | ||
version 2.4. | ||
|
||
Instead of putting your command in the ``Command`` directory and having Symfony | ||
auto-discover it for you, you can register commands in the service container | ||
using the ``console.command`` tag: | ||
By default, Symfony will take a look in the ``Command`` directory of you | ||
bundles and automatically register your commands. For the ones implementing | ||
the ``ContainerAwareCommand`` interface, Symfony will even inject the container. | ||
|
||
If you wan to, you can instead register them as services in the container using | ||
the ``console.command`` tag: | ||
|
||
.. configuration-block:: | ||
|
||
|
@@ -111,9 +114,20 @@ using the ``console.command`` tag: | |
|
||
.. tip:: | ||
|
||
Registering your command as a service gives you more control over its | ||
location and the services that are injected into it. But, there are no | ||
functional advantages, so you don't need to register your command as a service. | ||
Command as a service can be usefull in few situations: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usefull -> useful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe even use the plural, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. commands as services isn't a best practise imo. The currently Console implementation lacks of a good support for it, it should only be used in specific cases. That's why this addition is so nice, it tells you a specific case to use it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wouterj Precisely. @cordoval You might be right about the plural, if we take a look at controllers, we can find the cookbook How to define Controllers as Services There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added it in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would reference it here though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, in fact, I might even keep the header |
||
* if you need your commands to be defined somewhere else than ``Command`` | ||
* if you need to access services or configuration parameters in the | ||
``configure`` method | ||
|
||
For example, Imagine you want to provide a default value for the ``name`` | ||
option. You could hard code a string and pass it as the 4th argument of | ||
``addArgument``, or you could allow the user to set the default value in the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in their There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1 "the configuration" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I change user to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wauter was saying that he is -1 about "the configuration" check my comment and also if you use user then use her configuration, although some genre There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cordoval I was saying that I'm -1 for your change. "The configuration" is correct. (or at least, I tried to say that...)
Sadly, this isn't your documentation and we changed our standards to be gender agnostic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gnugat to summarize: It's all perfect :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wouterj Good :) . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gnugat i know that, that is why i said the above 👶 |
||
configuration. | ||
|
||
With a ``ContainerAwareCommand`` you wouldn't be able to retrieve the | ||
configuration parameter, because the ``configure`` method is called in the | ||
command's constructor. The only solution is to inject them through its | ||
constructor. | ||
|
||
Getting Services from the Service Container | ||
------------------------------------------- | ||
|
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.
you -> your