Skip to content

Commit 6a7a25f

Browse files
author
Loïc Chardonnet
committed
Fixed @wouterj's feedback
1 parent e137951 commit 6a7a25f

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

components/console/commands_as_services.rst

+24-29
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
.. index::
22
single: Console; Commands as Services
33

4-
How to define Commands as Services
4+
How to Define Commands as Services
55
==================================
66

77
.. versionadded:: 2.4
8-
Support for registering commands in the service container was added in
8+
Support for registering commands in the service container was introduced in
99
version 2.4.
1010

1111
By default, Symfony will take a look in the ``Command`` directory of your
1212
bundles and automatically register your commands. For the ones implementing
1313
the ``ContainerAwareCommand`` interface, Symfony will even inject the container.
14-
1514
While making life easier, this default implementation has some drawbacks in some
1615
situations:
1716

18-
* what if you want your command to be defined elsewhere than in the ``Command``
19-
folder?
20-
* what if you want to register conditionally your command, depending on the
17+
* What if you want your command to be defined elsewhere than in the ``Command``
18+
directory?
19+
* what if you want to conditionally register your command, depending on the
2120
current environment or on the availability of some dependencies?
22-
* what if you need to access dependencies before the ``setContainer`` is called
23-
(for example in the ``configure`` method)?
21+
* what if you need to access dependencies before the ``setContainer()`` is
22+
called (for example in the ``configure()`` method)?
2423
* what if you want to reuse a command many times, but with different
2524
dependencies or parameters?
2625

@@ -46,45 +45,41 @@ defining it with the ``console.command`` tag:
4645
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4746
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4847
49-
<service id="acme_hello.command.my_command"
50-
class="Acme\HelloBundle\Command\MyCommand">
51-
<tag name="console.command" />
52-
</service>
48+
<services>
49+
<service id="acme_hello.command.my_command"
50+
class="Acme\HelloBundle\Command\MyCommand">
51+
<tag name="console.command" />
52+
</service>
53+
</services>
5354
</container>
5455
5556
.. code-block:: php
5657
5758
// app/config/config.php
58-
5959
$container
6060
->register('acme_hello.command.my_command', 'Acme\HelloBundle\Command\MyCommand')
6161
->addTag('console.command')
6262
;
6363
64-
Here are some use cases.
65-
66-
Use dependencies and parameters in configure
67-
--------------------------------------------
64+
Use Case: Using Dependencies and Parameters to Set Default Values for Options
65+
-----------------------------------------------------------------------------
6866

69-
For example, imagine you want to provide a default value for the ``name``
70-
argument. You could:
67+
Imagine you want to provide a default value for the ``name``option. You could
68+
pass one of the following as the 5th argument of ``addOption()``:
7169

72-
* hard code a string and pass it as the 4th argument of ``addArgument``;
73-
* allow the user to set the default value in the configuration;
74-
* retrieve the default value from a service (a repository for example).
70+
* an hardcoded string;
71+
* a value coming from the configuration (allows the user to change it easily);
72+
* a value computed by a service (e.g. a repository).
7573

7674
With a ``ContainerAwareCommand`` you wouldn't be able to retrieve the
77-
configuration parameter, because the ``configure`` method is called in the
78-
command's constructor. The only solution is to inject them through its
79-
constructor:
75+
configuration parameter, because the ``configure()`` method is called in the
76+
constructor. The only solution is to inject them through it::
8077

81-
<?php
8278
// src/Acme/DemoBundle/Command/GreetCommand.php
8379
namespace Acme\DemoBundle\Command;
8480

8581
use Acme\DemoBundle\Entity\NameRepository;
8682
use Symfony\Component\Console\Command\Command;
87-
use Symfony\Component\Console\Input\InputArgument;
8883
use Symfony\Component\Console\Input\InputInterface;
8984
use Symfony\Component\Console\Input\InputOption;
9085
use Symfony\Component\Console\Output\OutputInterface;
@@ -105,13 +100,13 @@ constructor:
105100
$this
106101
->setName('demo:greet')
107102
->setDescription('Greet someone')
108-
->addArgument('name', InputArgument::OPTIONAL, 'Who do you want to greet?', $defaultName)
103+
->addOption('name', '-n', InputOption::VALUE_REQUIRED, 'Who do you want to greet?', $defaultName)
109104
;
110105
}
111106

112107
protected function execute(InputInterface $input, OutputInterface $output)
113108
{
114-
$name = $input->getArgument('name');
109+
$name = $input->getOption('name');
115110

116111
$output->writeln($name);
117112
}

components/console/introduction.rst

+1
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ Learn More!
527527
* :doc:`/components/console/usage`
528528
* :doc:`/components/console/single_command_tool`
529529
* :doc:`/components/console/events`
530+
* :doc:`/components/console/commands_as_services`
530531

531532
.. _Packagist: https://packagist.org/packages/symfony/console
532533
.. _ANSICON: https://github.com/adoxa/ansicon/releases

0 commit comments

Comments
 (0)