-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Console] command as service #3620
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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: | ||
* if you need your commands to be defined somewhere else than ``Command`` | ||
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. List items shouldn't be prefix with a single space in reStructuredText. And I think a new line before the list is required 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. At last, this item should end with a semi colon and the last item with a full stop |
||
* 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`` | ||
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. Lowercase imagine |
||
option. You could hard code a string and pass it as the 4th argument of | ||
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. pass it [...] to |
||
``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. remove the comma here |
||
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.
Typo: wan