@@ -27,8 +27,8 @@ with ``console.command``:
27
27
28
28
# app/config/config.yml
29
29
services :
30
- acme_hello .command.my_command :
31
- class : Acme\HelloBundle \Command\MyCommand
30
+ app .command.my_command :
31
+ class : AppBundle \Command\MyCommand
32
32
tags :
33
33
- { name: console.command }
34
34
@@ -42,9 +42,8 @@ with ``console.command``:
42
42
http://symfony.com/schema/dic/services/services-1.0.xsd" >
43
43
44
44
<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" >
48
47
<tag name =" console.command" />
49
48
</service >
50
49
</services >
@@ -55,8 +54,8 @@ with ``console.command``:
55
54
// app/config/config.php
56
55
$container
57
56
->register(
58
- 'acme_hello .command.my_command',
59
- 'Acme\HelloBundle \Command\MyCommand'
57
+ 'app .command.my_command',
58
+ 'AppBundle \Command\MyCommand'
60
59
)
61
60
->addTag('console.command')
62
61
;
@@ -74,31 +73,32 @@ pass one of the following as the 5th argument of ``addOption()``:
74
73
By extending ``ContainerAwareCommand ``, only the first is possible, because you
75
74
can't access the container inside the ``configure() `` method. Instead, inject
76
75
any 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 ::
78
77
79
- // src/Acme/DemoBundle /Command/GreetCommand.php
80
- namespace Acme\DemoBundle \Command;
78
+ // src/AppBundle /Command/GreetCommand.php
79
+ namespace AppBundle \Command;
81
80
82
- use Acme\DemoBundle\Entity\NameRepository;
83
81
use Symfony\Component\Console\Command\Command;
84
82
use Symfony\Component\Console\Input\InputInterface;
85
83
use Symfony\Component\Console\Input\InputOption;
86
84
use Symfony\Component\Console\Output\OutputInterface;
87
85
88
86
class GreetCommand extends Command
89
87
{
90
- protected $nameRepository ;
88
+ protected $defaultName ;
91
89
92
- public function __construct(NameRepository $nameRepository )
90
+ public function __construct($defaultName )
93
91
{
94
- $this->nameRepository = $nameRepository ;
92
+ $this->defaultName = $defaultName ;
95
93
96
94
parent::__construct();
97
95
}
98
96
99
97
protected function configure()
100
98
{
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;
102
102
103
103
$this
104
104
->setName('demo:greet')
@@ -122,7 +122,58 @@ have some ``NameRepository`` service that you'll use to get your default value::
122
122
}
123
123
124
124
Now, 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
+ <tag name =" console.command" />
159
+ </service >
160
+ </services >
161
+ </container >
162
+
163
+ .. code-block :: php
164
+
165
+ // app/config/config.php
166
+ $container->setParameter('command.default_name', 'Javier');
167
+
168
+ $container
169
+ ->register(
170
+ 'app.command.my_command',
171
+ 'AppBundle\Command\MyCommand'
172
+ )
173
+ ->addTag('console.command')
174
+ ;
175
+
176
+ Great, you now have a dynamic default value!
126
177
127
178
.. caution ::
128
179
0 commit comments