10000 [Console] Command as service by gnugat · Pull Request #3621 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[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

Merged
merged 9 commits into from
Mar 24, 2014
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Took into account @cordoval's feedback
  • Loading branch information
Loïc Chardonnet committed Mar 9, 2014
commit e8b3320e97336beb0a270484b618a01dc3158f12
22 changes: 10 additions & 12 deletions components/console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

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

situations:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... this has some limitations ...

Copy link
Member

Choose a reason for hiding this comment

The 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* Your command must live in the ``Command`` directory.

* Conditionally register your command, depending on the current environment or
on the availability of some dependencies;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* There's no way to conditionally register your service based on the environment
  or availability of some dependencies;

* Access dependencies before the ``setContainer()`` is called (for example in
the ``configure()`` method);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* You can't access the container in the ``configure()`` method (because ``setContainer``
  hasn't been called yet);

* Reuse a command many times, but with different dependencies or parameters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • You can't use the same class to create many commands (i.e. each with different configuration).


To solve those problems, you can register your command as a service by simply
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... solve these ...

... as a service and tag it with console.command:

defining it with the ``console.command`` tag:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In these cases we tag ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more. 10000

-1 never use the first person

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By extending ContainerAwareCommand, only the first is possible, because you can't
access the container inside the configure() method. Instead, inject any parameter or
service you need into the constructor. For example, suppose you have some NameRepository service that you'll use to get your default value:

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;
Expand Down Expand Up @@ -114,6 +112,6 @@ constructor. The only solution is to inject them through it::

Copy link
Member

Choose a reason for hiding this comment

The 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 NameRepository. Great, you now have a dynamic default value!

.. 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful not to actually do any work in configure (e.g. make database queries), as your code will be run, even if you're using the console to execute a different command.

0