-
-
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
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,12 @@ the ``ContainerAwareCommand`` interface, Symfony will even inject the container. | |
While making life easier, this default implementation has some drawbacks in some | ||
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. ... this has some limitations ... 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. And I rephrased each bullet point below so that they all sound like limitations (not things that you will be able to do after registering as as service), since we mention limitations/drawbacks here. |
||
|
||
* What if you want your command to be defined elsewhere than in the ``Command`` | ||
directory? | ||
* what if you want to conditionally register your command, depending on the | ||
current environment or on the availability of some dependencies? | ||
* what if you need to access dependencies before the ``setContainer()`` is | ||
called (for example in the ``configure()`` method)? | ||
* what if you want to reuse a command many times, but with different | ||
dependencies or parameters? | ||
* Define the command elsewhere than in the ``Command`` directory; | ||
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.
|
||
* Conditionally register your command, depending on the current environment or | ||
on the availability of some dependencies; | ||
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.
|
||
* Access dependencies before the ``setContainer()`` is called (for example in | ||
the ``configure()`` method); | ||
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.
|
||
* Reuse a command many times, but with different dependencies or parameters | ||
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.
|
||
|
||
To solve those problems, you can register your command as a service by simply | ||
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.
|
||
defining it with the ``console.command`` tag: | ||
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 these cases we tag ... 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. 10000 -1 never use the first person |
||
|
@@ -73,7 +71,7 @@ pass one of the following as the 5th argument of ``addOption()``: | |
|
||
With a ``ContainerAwareCommand`` you wouldn't be able to retrieve 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.
|
||
configuration parameter, because the ``configure()`` method is called in the | ||
constructor. The only solution is to inject them through it:: | ||
constructor. The only solution is to inject them:: | ||
|
||
// src/Acme/DemoBundle/Command/GreetCommand.php | ||
namespace Acme\DemoBundle\Command; | ||
|
@@ -114,6 +112,6 @@ constructor. The only solution is to inject them through 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. Now, just update the arguments of your service configuration like normal to inject the |
||
.. caution:: | ||
|
||
When running the console, every commands are instanciated, which means every | ||
``configure()`` methods are called. Be careful with database queries, as | ||
this could impact performances. | ||
When running the console, every command is instantiated, which means every | ||
``configure()`` method is called. Be careful with database queries, as | ||
they could impact performance. | ||
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. Be careful not to actually do any work in |
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.
I would suggest adding this to the previous paragraph