Skip to content

[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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 20 additions & 6 deletions cookbook/console/console_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Typo: wan

the ``console.command`` tag:

.. configuration-block::

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

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

The 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
-------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion reference/constraints/Callback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ your validation function is ``Vendor\Package\Validator::validate()``::

class Validator
{
public function validate($object, ExecutionContextInterface $context)
public static function validate($object, ExecutionContextInterface $context)
{
// ...
}
Expand Down