@@ -192,68 +192,6 @@ method::
192
192
You can also test a whole console application by using
193
193
:class: `Symfony\\ Component\\ Console\\ Tester\\ ApplicationTester `.
194
194
195
- .. _calling-existing-command :
196
-
197
- Calling an Existing Command
198
- ---------------------------
199
-
200
- If a command depends on another one being run before it, instead of asking the
201
- user to remember the order of execution, you can call it directly yourself.
202
- This is also useful if you want to create a "meta" command that just runs a
203
- bunch of other commands (for instance, all commands that need to be run when
204
- the project's code has changed on the production servers: clearing the cache,
205
- generating Doctrine2 proxies, dumping Assetic assets, ...).
206
-
207
- Calling a command from another one is straightforward::
208
-
209
- protected function execute(InputInterface $input, OutputInterface $output)
210
- {
211
- $command = $this->getApplication()->find('demo:greet');
212
-
213
- $arguments = array(
214
- 'command' => 'demo:greet',
215
- 'name' => 'Fabien',
216
- '--yell' => true,
217
- );
218
-
219
- $greetInput = new ArrayInput($arguments);
220
- $returnCode = $command->run($greetInput, $output);
221
-
222
- // ...
223
- }
224
-
225
- First, you :method: `Symfony\\ Component\\ Console\\ Application::find ` the
226
- command you want to execute by passing the command name. Then, you need to create
227
- a new :class: `Symfony\\ Component\\ Console\\ Input\\ ArrayInput ` with the arguments
228
- and options you want to pass to the command.
229
-
230
- Eventually, calling the ``run() `` method actually executes the command and
231
- returns the returned code from the command (return value from command's
232
- ``execute() `` method).
233
-
234
- .. tip ::
235
-
236
- If you want to suppress the output of the executed command, pass a
237
- :class: `Symfony\\ Component\\ Console\\ Output\\ NullOutput ` as the second
238
- argument to ``$command->run() ``.
239
-
240
- .. caution ::
241
-
242
- Note that all the commands will run in the same process and some of Symfony's
243
- built-in commands may not work well this way. For instance, the ``cache:clear ``
244
- and ``cache:warmup `` commands change some class definitions, so running
245
- something after them is likely to break.
246
-
247
- .. note ::
248
-
249
- Most of the time, calling a command from code that is not executed on the
250
- command line is not a good idea for several reasons. First, the command's
251
- output is optimized for the console. But more important, you can think of
252
- a command as being like a controller; it should use the model to do
253
- something and display feedback to the user. So, instead of calling a
254
- command from the Web, refactor your code and move the logic to a new
255
- class.
256
-
257
195
Learn More
258
196
----------
259
197
0 commit comments