@@ -27,8 +27,8 @@ with ``console.command``:
2727
2828 # app/config/config.yml
2929 services :
30- acme_hello .command.my_command :
31- class : Acme\HelloBundle \Command\MyCommand
30+ app .command.my_command :
31+ class : AppBundle \Command\MyCommand
3232 tags :
3333 - { name: console.command }
3434
@@ -42,9 +42,8 @@ with ``console.command``:
4242 http://symfony.com/schema/dic/services/services-1.0.xsd" >
4343
4444 <services >
45- <service id =" acme_hello.command.my_command"
46- class =" Acme\HelloBundle\Command\MyCommand" >
47-
45+ <service id =" app.command.my_command"
46+ class =" AppBundle\Command\MyCommand" >
4847 <tag name =" console.command" />
4948 </service >
5049 </services >
@@ -55,8 +54,8 @@ with ``console.command``:
5554 // app/config/config.php
5655 $container
5756 ->register(
58- 'acme_hello .command.my_command',
59- 'Acme\HelloBundle \Command\MyCommand'
57+ 'app .command.my_command',
58+ 'AppBundle \Command\MyCommand'
6059 )
6160 ->addTag('console.command')
6261 ;
@@ -74,31 +73,32 @@ pass one of the following as the 5th argument of ``addOption()``:
7473By extending ``ContainerAwareCommand ``, only the first is possible, because you
7574can't access the container inside the ``configure() `` method. Instead, inject
7675any parameter or service you need into the constructor. For example, suppose you
77- have some ``NameRepository `` service that you'll use to get your default value ::
76+ store the default value in some ``%command.default_name% `` parameter ::
7877
79- // src/Acme/DemoBundle /Command/GreetCommand.php
80- namespace Acme\DemoBundle \Command;
78+ // src/AppBundle /Command/GreetCommand.php
79+ namespace AppBundle \Command;
8180
82- use Acme\DemoBundle\Entity\NameRepository;
8381 use Symfony\Component\Console\Command\Command;
8482 use Symfony\Component\Console\Input\InputInterface;
8583 use Symfony\Component\Console\Input\InputOption;
8684 use Symfony\Component\Console\Output\OutputInterface;
8785
8886 class GreetCommand extends Command
8987 {
90- protected $nameRepository ;
88+ protected $defaultName ;
9189
92- public function __construct(NameRepository $nameRepository )
90+ public function __construct($defaultName )
9391 {
94- $this->nameRepository = $nameRepository ;
92+ $this->defaultName = $defaultName ;
9593
9694 parent::__construct();
9795 }
9896
9997 protected function configure()
10098 {
101- $defaultName = $this->nameRepository->findLastOne();
99+ // try to avoid work here (e.g. database query)
100+ // this method is *always* called - see warning below
101+ $defaultName = $this->defaultName;
102102
103103 $this
104104 ->setName('demo:greet')
@@ -122,7 +122,60 @@ have some ``NameRepository`` service that you'll use to get your default value::
122122 }
123123
124124Now, just update the arguments of your service configuration like normal to
125- inject the ``NameRepository ``. Great, you now have a dynamic default value!
125+ inject the ``command.default_name `` parameter:
126+
127+ .. configuration-block ::
128+
129+ .. code-block :: yaml
130+
131+ # app/config/config.yml
132+ parameters :
133+ command.default_name : Javier
134+
135+ services :
136+ app.command.my_command :
137+ class : AppBundle\Command\MyCommand
138+ arguments : ["%command.default_name%"]
139+ tags :
140+ - { name: console.command }
141+
142+ .. code-block :: xml
143+
144+ <!-- app/config/config.xml -->
145+ <?xml version =" 1.0" encoding =" UTF-8" ?>
146+ <container xmlns =" http://symfony.com/schema/dic/services"
147+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
148+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
149+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
150+
151+ <parameters >
152+ <parameter key =" command.default_name" >Javier</parameter >
153+ </parameters >
154+
155+ <services >
156+ <service id =" app.command.my_command"
157+ class =" AppBundle\Command\MyCommand" >
158+ <argument >%command.default_name%</argument >
159+ <tag name =" console.command" />
160+ </service >
161+ </services >
162+ </container >
163+
164+ .. code-block :: php
165+
166+ // app/config/config.php
167+ $container->setParameter('command.default_name', 'Javier');
168+
169+ $container
170+ ->register(
171+ 'app.command.my_command',
172+ 'AppBundle\Command\MyCommand',
173+ )
174+ ->setArguments(array('%command.default_name%'))
175+ ->addTag('console.command')
176+ ;
177+
178+ Great, you now have a dynamic default value!
126179
127180.. caution ::
128181
0 commit comments