@@ -4,9 +4,16 @@ Question Helper
4
4
The :class: `Symfony\\ Component\\ Console\\ Helper\\ QuestionHelper ` provides
5
5
functions to ask the user for more information. It is included in the default
6
6
helper set and you can get it by calling
7
- :method: `Symfony\\ Component\\ Console\\ Command \\ Command::getHelper `::
7
+ :method: `Symfony\\ Component\\ Console\\ Application::getHelperSet `::
8
8
9
- $helper = $this->getHelper('question');
9
+ use Symfony\Component\Console\Application;
10
+
11
+ public function __invoke(Application $application): int
12
+ {
13
+ $helper = $application->getHelperSet()->get('question');
14
+
15
+ // ...
16
+ }
10
17
11
18
The Question Helper has a single method
12
19
:method: `Symfony\\ Component\\ Console\\ Helper\\ QuestionHelper::ask ` that needs an
@@ -27,6 +34,7 @@ Suppose you want to confirm an action before actually executing it. Add
27
34
the following to your command::
28
35
29
36
// ...
37
+ use Symfony\Component\Console\Application;
30
38
use Symfony\Component\Console\Attribute\AsCommand;
31
39
use Symfony\Component\Console\Command\Command;
32
40
use Symfony\Component\Console\Input\InputInterface;
@@ -36,9 +44,9 @@ the following to your command::
36
44
#[AsCommand(name: 'app:my-command')]
37
45
class MyCommand
38
46
{
39
- public function __invoke(InputInterface $input, OutputInterface $output): int
47
+ public function __invoke(InputInterface $input, OutputInterface $output, Application $application ): int
40
48
{
41
- $helper = $this->getHelper ('question');
49
+ $helper = $application->getHelperSet()->get ('question');
42
50
$question = new ConfirmationQuestion('Continue with this action?', false);
43
51
44
52
if (!$helper->ask($input, $output, $question)) {
@@ -91,7 +99,7 @@ if you want to know a bundle name, you can add this to your command::
91
99
use Symfony\Component\Console\Question\Question;
92
100
93
101
// ...
94
- public function execute (InputInterface $input, OutputInterface $output): int
102
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
95
103
{
96
104
// ...
97
105
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
@@ -121,10 +129,10 @@ but ``red`` could be set instead (could be more explicit)::
121
129
use Symfony\Component\Console\Question\ChoiceQuestion;
122
130
123
131
// ...
124
- public function execute (InputInterface $input, OutputInterface $output): int
132
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
125
133
{
126
134
// ...
127
- $helper = $this->getHelper ('question');
135
+ $helper = $application->getHelperSet()->get ('question');
128
136
$question = new ChoiceQuestion(
129
137
'Please select your favorite color (defaults to red)',
130
138
// choices can also be PHP objects that implement __toString() method
@@ -184,10 +192,10 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
184
192
use Symfony\Component\Console\Question\ChoiceQuestion;
185
193
186
194
// ...
187
- public function execute (InputInterface $input, OutputInterface $output): int
195
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
188
196
{
189
197
// ...
190
- $helper = $this->getHelper ('question');
198
+ $helper = $application->getHelperSet()->get ('question');
191
199
$question = new ChoiceQuestion(
192
200
'Please select your favorite colors (defaults to red and blue)',
193
201
['red', 'blue', 'yellow'],
@@ -218,10 +226,10 @@ will be autocompleted as the user types::
218
226
use Symfony\Component\Console\Question\Question;
219
227
220
228
// ...
221
- public function execute (InputInterface $input, OutputInterface $output): int
229
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
222
230
{
223
231
// ...
224
- $helper = $this->getHelper ('question');
232
+ $helper = $application->getHelperSet()->get ('question');
225
233
226
234
$bundles = ['AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle'];
227
235
$question = new Question('Please enter the name of a bundle', 'FooBundle');
@@ -241,9 +249,9 @@ provide a callback function to dynamically generate suggestions::
241
249
use Symfony\Component\Console\Question\Question;
242
250
243
251
// ...
244
- public function execute (InputInterface $input, OutputInterface $output): int
252
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
245
253
{
246
- $helper = $this->getHelper ('question');
254
+ $helper = $application->getHelperSet()->get ('question');
247
255
248
256
// This function is called whenever the input changes and new
249
257
// suggestions are needed.
@@ -282,10 +290,10 @@ You can also specify if you want to not trim the answer by setting it directly w
282
290
use Symfony\Component\Console\Question\Question;
283
291
284
292
// ...
285
- public function execute (InputInterface $input, OutputInterface $output): int
293
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
286
294
{
287
295
// ...
288
- $helper = $this->getHelper ('question');
296
+ $helper = $application->getHelperSet()->get ('question');
289
297
290
298
$question = new Question('What is the name of the child?');
291
299
$question->setTrimmable(false);
@@ -308,10 +316,10 @@ the response to a question should allow multiline answers by passing ``true`` to
308
316
use Symfony\Component\Console\Question\Question;
309
317
310
318
// ...
311
- public function execute (InputInterface $input, OutputInterface $output): int
319
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
312
320
{
313
321
// ...
314
- $helper = $this->getHelper ('question');
322
+ $helper = $application->getHelperSet()->get ('question');
315
323
316
324
$question = new Question('How do you solve world peace?');
317
325
$question->setMultiline(true);
@@ -335,10 +343,10 @@ convenient for passwords::
335
343
use Symfony\Component\Console\Question\Question;
336
344
337
345
// ...
338
- public function execute (InputInterface $input, OutputInterface $output): int
346
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
339
347
{
340
348
// ...
341
- $helper = $this->getHelper ('question');
349
+ $helper = $application->getHelperSet()->get ('question');
342
350
343
351
$question = new Question('What is the database password?');
344
352
$question->setHidden(true);
@@ -372,10 +380,10 @@ convenient for passwords::
372
380
use Symfony\Component\Console\Question\ChoiceQuestion;
373
381
374
382
// ...
375
- public function execute (InputInterface $input, OutputInterface $output): int
383
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
376
384
{
377
385
// ...
378
- $helper = $this->getHelper ('question');
386
+ $helper = $application->getHelperSet()->get ('question');
379
387
QuestionHelper::disableStty();
380
388
381
389
// ...
@@ -396,10 +404,10 @@ method::
396
404
use Symfony\Component\Console\Question\Question;
397
405
398
406
// ...
399
- public function execute (InputInterface $input, OutputInterface $output): int
407
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
400
408
{
401
409
// ...
402
- $helper = $this->getHelper ('question');
410
+ $helper = $application->getHelperSet()->get ('question');
403
411
404
412
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
405
413
$question->setNormalizer(function (string $value): string {
@@ -434,10 +442,10 @@ method::
434
442
use Symfony\Component\Console\Question\Question;
435
443
436
444
// ...
437
- public function execute (InputInterface $input, OutputInterface $output): int
445
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
438
446
{
439
447
// ...
440
- $helper = $this->getHelper ('question');
448
+ $helper = $application->getHelperSet()->get ('question');
441
449
442
450
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
443
451
$question->setValidator(function (string $answer): string {
@@ -494,10 +502,10 @@ You can also use a validator with a hidden question::
494
502
use Symfony\Component\Console\Question\Question;
495
503
496
504
// ...
497
- public function execute (InputInterface $input, OutputInterface $output): int
505
+ public function __invoke (InputInterface $input, OutputInterface $output, Application $application ): int
498
506
{
499
507
// ...
500
- $helper = $this->getHelper ('question');
508
+ $helper = $application->getHelperSet()->get ('question');
501
509
502
510
$question = new Question('Please enter your password');
503
511
$question->setNormalizer(function (?string $value): string {
0 commit comments