Skip to content

Commit 7443201

Browse files
committed
Wouter's Review (WIP)
1 parent 1a9a5d7 commit 7443201

File tree

7 files changed

+59
-77
lines changed

7 files changed

+59
-77
lines changed

components/console/helpers/debug_formatter.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ this:
1010
.. image:: /_images/components/console/debug_formatter.png
1111
:alt: Console output, with the first line showing "RUN Running figlet", followed by lines showing the output of the command prefixed with "OUT" and "RES Finished the command" as last line in the output.
1212

13-
Using the debug_formatter
13+
Using the Debug Formatter
1414
-------------------------
1515

16-
The formatter is included in the default helper set and you can get it by
17-
calling :method:`Symfony\\Component\\Console\\Command\\Command::getHelper`::
16+
The debug formatter helper can be instantiated directly as shown::
1817

19-
$debugFormatter = $this->getHelper('debug_formatter');
18+
$debugFormatter = new DebugFormatterHelper();
2019

21-
The formatter accepts strings and returns a formatted string, which you then
20+
It accepts strings and returns a formatted string, which you then
2221
output to the console (or even log the information or do anything else).
2322

2423
All methods of this helper have an identifier as the first argument. This is a

components/console/helpers/formatterhelper.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
Formatter Helper
22
================
33

4-
The Formatter helper provides functions to format the output with colors.
5-
You can do more advanced things with this helper than you can in
6-
:doc:`/console/coloring`.
4+
The :class:`Symfony\\Component\\Console\\Helper\\FormatterHelper` helper provides
5+
functions to format the output with colors. You can do more advanced things with
6+
this helper than you can in :doc:`/console/coloring`::
77

8-
The :class:`Symfony\\Component\\Console\\Helper\\FormatterHelper` is included
9-
in the default helper set and you can get it by calling
10-
:method:`Symfony\\Component\\Console\\Command\\Command::getHelper`::
11-
12-
$formatter = $this->getHelper('formatter');
8+
$formatter = new FormatterHelper();
139

1410
The methods return a string, which you'll usually render to the console by
1511
passing it to the

components/console/helpers/processhelper.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ a very verbose verbosity (e.g. ``-vv``)::
1111

1212
use Symfony\Component\Process\Process;
1313

14-
$helper = $this->getHelper('process');
14+
$helper = new ProcessHelper();
1515
$process = new Process(['figlet', 'Symfony']);
1616

1717
$helper->run($output, $process);

components/console/helpers/questionhelper.rst

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ Question Helper
22
===============
33

44
The :class:`Symfony\\Component\\Console\\Helper\\QuestionHelper` provides
5-
functions to ask the user for more information. It is included in the default
6-
helper set and you can get it by calling
7-
:method:`Symfony\\Component\\Console\\Command\\Command::getHelper`::
5+
functions to ask the user for more information::
86

9-
$helper = $this->getHelper('question');
7+
$helper = new QuestionHelper();
108

119
The Question Helper has a single method
1210
:method:`Symfony\\Component\\Console\\Helper\\QuestionHelper::ask` that needs an
@@ -38,7 +36,7 @@ the following to your command::
3836
{
3937
public function __invoke(InputInterface $input, OutputInterface $output): int
4038
{
41-
$helper = $this->getHelper('question');
39+
$helper = new QuestionHelper();
4240
$question = new ConfirmationQuestion('Continue with this action?', false);
4341

4442
if (!$helper->ask($input, $output, $question)) {
@@ -91,7 +89,7 @@ if you want to know a bundle name, you can add this to your command::
9189
use Symfony\Component\Console\Question\Question;
9290

9391
// ...
94-
public function execute(InputInterface $input, OutputInterface $output): int
92+
public function __invoke(InputInterface $input, OutputInterface $output): int
9593
{
9694
// ...
9795
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
@@ -121,10 +119,10 @@ but ``red`` could be set instead (could be more explicit)::
121119
use Symfony\Component\Console\Question\ChoiceQuestion;
122120

123121
// ...
124-
public function execute(InputInterface $input, OutputInterface $output): int
122+
public function __invoke(InputInterface $input, OutputInterface $output): int
125123
{
126124
// ...
127-
$helper = $this->getHelper('question');
125+
$helper = new QuestionHelper();
128126
$question = new ChoiceQuestion(
129127
'Please select your favorite color (defaults to red)',
130128
// choices can also be PHP objects that implement __toString() method
@@ -184,10 +182,10 @@ this use :method:`Symfony\\Component\\Console\\Question\\ChoiceQuestion::setMult
184182
use Symfony\Component\Console\Question\ChoiceQuestion;
185183

186184
// ...
187-
public function execute(InputInterface $input, OutputInterface $output): int
185+
public function __invoke(InputInterface $input, OutputInterface $output): int
188186
{
189187
// ...
190-
$helper = $this->getHelper('question');
188+
$helper = new QuestionHelper();
191189
$question = new ChoiceQuestion(
192190
'Please select your favorite colors (defaults to red and blue)',
193191
['red', 'blue', 'yellow'],
@@ -218,10 +216,10 @@ will be autocompleted as the user types::
218216
use Symfony\Component\Console\Question\Question;
219217

220218
// ...
221-
public function execute(InputInterface $input, OutputInterface $output): int
219+
public function __invoke(InputInterface $input, OutputInterface $output): int
222220
{
223221
// ...
224-
$helper = $this->getHelper('question');
222+
$helper = new QuestionHelper();
225223

226224
$bundles = ['AcmeDemoBundle', 'AcmeBlogBundle', 'AcmeStoreBundle'];
227225
$question = new Question('Please enter the name of a bundle', 'FooBundle');
@@ -241,9 +239,9 @@ provide a callback function to dynamically generate suggestions::
241239
use Symfony\Component\Console\Question\Question;
242240

243241
// ...
244-
public function execute(InputInterface $input, OutputInterface $output): int
242+
public function __invoke(InputInterface $input, OutputInterface $output): int
245243
{
246-
$helper = $this->getHelper('question');
244+
$helper = new QuestionHelper();
247245

248246
// This function is called whenever the input changes and new
249247
// suggestions are needed.
@@ -282,10 +280,10 @@ You can also specify if you want to not trim the answer by setting it directly w
282280
use Symfony\Component\Console\Question\Question;
283281

284282
// ...
285-
public function execute(InputInterface $input, OutputInterface $output): int
283+
public function __invoke(InputInterface $input, OutputInterface $output): int
286284
{
287285
// ...
288-
$helper = $this->getHelper('question');
286+
$helper = new QuestionHelper();
289287

290288
$question = new Question('What is the name of the child?');
291289
$question->setTrimmable(false);
@@ -308,10 +306,10 @@ the response to a question should allow multiline answers by passing ``true`` to
308306
use Symfony\Component\Console\Question\Question;
309307

310308
// ...
311-
public function execute(InputInterface $input, OutputInterface $output): int
309+
public function __invoke(InputInterface $input, OutputInterface $output): int
312310
{
313311
// ...
314-
$helper = $this->getHelper('question');
312+
$helper = new QuestionHelper();
315313

316314
$question = new Question('How do you solve world peace?');
317315
$question->setMultiline(true);
@@ -335,10 +333,10 @@ convenient for passwords::
335333
use Symfony\Component\Console\Question\Question;
336334

337335
// ...
338-
public function execute(InputInterface $input, OutputInterface $output): int
336+
public function __invoke(InputInterface $input, OutputInterface $output): int
339337
{
340338
// ...
341-
$helper = $this->getHelper('question');
339+
$helper = new QuestionHelper();
342340

343341
$question = new Question('What is the database password?');
344342
$question->setHidden(true);
@@ -372,10 +370,10 @@ convenient for passwords::
372370
use Symfony\Component\Console\Question\ChoiceQuestion;
373371

374372
// ...
375-
public function execute(InputInterface $input, OutputInterface $output): int
373+
public function __invoke(InputInterface $input, OutputInterface $output): int
376374
{
377375
// ...
378-
$helper = $this->getHelper('question');
376+
$helper = new QuestionHelper();
379377
QuestionHelper::disableStty();
380378

381379
// ...
@@ -396,10 +394,10 @@ method::
396394
use Symfony\Component\Console\Question\Question;
397395

398396
// ...
399-
public function execute(InputInterface $input, OutputInterface $output): int
397+
public function __invoke(InputInterface $input, OutputInterface $output): int
400398
{
401399
// ...
402-
$helper = $this->getHelper('question');
400+
$helper = new QuestionHelper();
403401

404402
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
405403
$question->setNormalizer(function (string $value): string {
@@ -434,10 +432,10 @@ method::
434432
use Symfony\Component\Console\Question\Question;
435433

436434
// ...
437-
public function execute(InputInterface $input, OutputInterface $output): int
435+
public function __invoke(InputInterface $input, OutputInterface $output): int
438436
{
439437
// ...
440-
$helper = $this->getHelper('question');
438+
$helper = new QuestionHelper();
441439

442440
$question = new Question('Please enter the name of the bundle', 'AcmeDemoBundle');
443441
$question->setValidator(function (string $answer): string {
@@ -494,10 +492,10 @@ You can also use a validator with a hidden question::
494492
use Symfony\Component\Console\Question\Question;
495493

496494
// ...
497-
public function execute(InputInterface $input, OutputInterface $output): int
495+
public function __invoke(InputInterface $input, OutputInterface $output): int
498496
{
499497
// ...
500-
$helper = $this->getHelper('question');
498+
$helper = new QuestionHelper();
501499

502500
$question = new Question('Please enter your password');
503501
$question->setNormalizer(function (?string $value): string {

console.rst

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Commands are defined in classes, for example, you may want a command to create a
145145

146146
Additionally, you can extend the :class:`Symfony\\Component\\Console\\Command\\Command` class to
147147
leverage advanced features like lifecycle hooks: :method:`Symfony\\Component\\Console\\Command\\Command::initialize`,
148-
:method:`Symfony\\Component\\Console\\Command\\Command::interact`, and built-in helpers.
148+
and :method:`Symfony\\Component\\Console\\Command\\Command::interact`.
149149

150150
Configuring the Command
151151
~~~~~~~~~~~~~~~~~~~~~~~
@@ -225,15 +225,14 @@ You can register the command by adding the ``AsCommand`` attribute to it::
225225
namespace App\Command;
226226

227227
use Symfony\Component\Console\Attribute\AsCommand;
228-
use Symfony\Component\Console\Command\Command;
229228

230229
#[AsCommand(
231230
name: 'app:create-user',
232231
description: 'Creates a new user.',
233232
hidden: false,
234233
aliases: ['app:add-user']
235234
)]
236-
class CreateUserCommand extends Command
235+
class CreateUserCommand
237236
{
238237
// ...
239238
}
@@ -253,16 +252,16 @@ After configuring and registering the command, you can run it in the terminal:
253252
$ php bin/console app:create-user
254253
255254
As you might expect, this command will do nothing as you didn't write any logic
256-
yet. Add your own logic inside the ``execute()`` method.
255+
yet. Add your own logic inside the ``__invoke()`` method.
257256

258257
Console Output
259258
--------------
260259

261-
The ``execute()`` method has access to the output stream to write messages to
260+
The ``__invoke()`` method has access to the output stream to write messages to
262261
the console::
263262

264263
// ...
265-
protected function execute(InputInterface $input, OutputInterface $output): int
264+
public function __invoke(OutputInterface $output): int
266265
{
267266
// outputs multiple lines to the console (adding "\n" at the end of each line)
268267
$output->writeln([
@@ -313,9 +312,10 @@ method, which returns an instance of
313312
// ...
314313
use Symfony\Component\Console\Output\ConsoleOutputInterface;
315314

316-
class MyCommand extends Command
315+
#[AsCommand(name: 'app:my-command')]
316+
class MyCommand
317317
{
318-
protected function execute(InputInterface $input, OutputInterface $output): int
318+
public function __invoke(OutputInterface $output): int
319319
{
320320
if (!$output instanceof ConsoleOutputInterface) {
321321
throw new \LogicException('This command accepts only an instance of "ConsoleOutputInterface".');
@@ -374,29 +374,20 @@ Console Input
374374

375375
Use input options or arguments to pass information to the command::
376376

377-
use Symfony\Component\Console\Input\InputArgument;
378-
379-
// ...
380-
protected function configure(): void
381-
{
382-
$this
383-
// configure an argument
384-
->addArgument('username', InputArgument::REQUIRED, 'The username of the user.')
385-
// ...
386-
;
387-
}
377+
use Symfony\Component\Console\Attribute\Argument;
388378

389-
// ...
390-
public function execute(InputInterface $input, OutputInterface $output): int
379+
// The #[Argument] attribute configures $username as a
380+
// required input argument and its value is automatically
381+
// passed to this parameter
382+
public function __invoke(#[Argument('The username of the user.')] string $username, OutputInterface $output): int
391383
{
392384
$output->writeln([
393385
'User Creator',
394386
'============',
395387
'',
396388
]);
397389

398-
// retrieve the argument value using getArgument()
399-
$output->writeln('Username: '.$input->getArgument('username'));
390+
$output->writeln('Username: '.$username);
400391

401392
return Command::SUCCESS;
402393
}
@@ -426,23 +417,22 @@ as a service, you can use normal dependency injection. Imagine you have a
426417

427418
// ...
428419
use App\Service\UserManager;
429-
use Symfony\Component\Console\Command\Command;
420+
use Symfony\Component\Console\Attribute\Argument;
421+
use Symfony\Component\Console\Attribute\AsCommand;
430422

431-
class CreateUserCommand extends Command
423+
#[AsCommand(name: 'app:create-user')]
424+
class CreateUserCommand
432425
{
433426
public function __construct(
434-
private UserManager $userManager,
435-
){
436-
parent::__construct();
427+
private UserManager $userManager
428+
) {
437429
}
438430

439-
// ...
440-
441-
protected function execute(InputInterface $input, OutputInterface $output): int
431+
public function __invoke(#[Argument] string $username, OutputInterface $output): int
442432
{
443433
// ...
444434

445-
$this->userManager->create($input->getArgument('username'));
435+
$this->userManager->create($username);
446436

447437
$output->writeln('User successfully generated!');
448438

console/calling_commands.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ arguments and options you want to pass to the command. The command name must be
1414
the first argument.
1515

1616
Eventually, calling the ``doRun()`` method actually runs the command and returns
17-
the returned code from the command (return value from command ``execute()``
17+
the returned code from the command (return value from command ``__invoke()``
1818
method)::
1919

2020
// ...
2121
use Symfony\Component\Console\Attribute\AsCommand;
22-
use Symfony\Component\Console\Command;
2322
use Symfony\Component\Console\Input\ArrayInput;
2423
use Symfony\Component\Console\Output\OutputInterface;
2524

console/style.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ which allow to create *semantic* commands and forget about their styling.
4242
Basic Usage
4343
-----------
4444

45-
In your `__invoke` method, add an argument of type :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle`.
45+
In your ``__invoke()`` method, add an argument of type :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle`.
4646
Then, you can start using any of its helpers, such as ``title()``, which
4747
displays the title of the command::
4848

0 commit comments

Comments
 (0)