Skip to content

Commit c2dcae4

Browse files
committed
Merge branch '6.0' into 6.1
* 6.0: [Console] Remove some unneeded versionadded directives No longer mention a deprecated interface Document console completion Fix invalid interface mention in security.rst
2 parents 4c9291d + 8cfd997 commit c2dcae4

File tree

5 files changed

+199
-36
lines changed

5 files changed

+199
-36
lines changed
150 KB
Loading

console.rst

Lines changed: 105 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,86 @@ The Symfony framework provides lots of commands through the ``bin/console`` scri
99
created with the :doc:`Console component </components/console>`. You can also
1010
use it to create your own commands.
1111

12-
The Console: APP_ENV & APP_DEBUG
13-
---------------------------------
12+
Running Commands
13+
----------------
14+
15+
Each Symfony application comes with a large set of commands. You can use
16+
the ``list`` command to view all available commands in the application:
17+
18+
.. code-block:: terminal
19+
20+
$ php bin/console list
21+
...
22+
23+
Available commands:
24+
about Display information about the current project
25+
completion Dump the shell completion script
26+
help Display help for a command
27+
list List commands
28+
assets
29+
assets:install Install bundle's web assets under a public directory
30+
cache
31+
cache:clear Clear the cache
32+
...
33+
34+
If you find the command you need, you can run it with the ``--help`` option
35+
to view the command's documentation:
36+
37+
.. code-block:: terminal
38+
39+
$ php bin/console assets:install --help
40+
41+
APP_ENV & APP_DEBUG
42+
~~~~~~~~~~~~~~~~~~~
1443

1544
Console commands run in the :ref:`environment <config-dot-env>` defined in the ``APP_ENV``
1645
variable of the ``.env`` file, which is ``dev`` by default. It also reads the ``APP_DEBUG``
1746
value to turn "debug" mode on or off (it defaults to ``1``, which is on).
1847

1948
To run the command in another environment or debug mode, edit the value of ``APP_ENV``
20-
and ``APP_DEBUG``.
49+
and ``APP_DEBUG``. You can also define this env vars when running the
50+
command, for instance:
51+
52+
.. code-block:: terminal
53+
54+
# clears the cache for the prod environment
55+
$ APP_ENV=prod php bin/console cache:clear
56+
57+
.. _console-completion-setup:
58+
59+
Console Completion
60+
~~~~~~~~~~~~~~~~~~
61+
62+
If you are using the Bash shell, you can install Symfony's completion
63+
script to get auto completion when typing commands in the terminal. All
64+
commands support name and option completion, and some can even complete
65+
values.
66+
67+
.. image:: /_images/components/console/completion.gif
68+
69+
First, make sure you installed and setup the "bash completion" package for
70+
your OS (typically named ``bash-completion``). Then, install the Symfony
71+
completion bash script *once* by running the ``completion`` command in a
72+
Symfony app installed on your computer:
73+
74+
.. code-block:: terminal
75+
76+
$ php bin/console completion bash | sudo tee /etc/bash_completion.d/console-events-terminate
77+
# after the installation, restart the shell
78+
79+
Now you are all set to use the auto completion for all Symfony Console
80+
applications on your computer. By default, you can get a list of complete
81+
options by pressing the Tab key.
82+
83+
.. tip::
84+
85+
Many PHP tools are built using the Symfony Console component (e.g.
86+
Composer, PHPstan and Behat). If they are using version 5.4 or higher,
87+
you can also install their completion script to enable console completion:
88+
89+
.. code-block:: terminal
90+
91+
$ php vendor/bin/phpstan completion bash | sudo tee /etc/bash_completion.d/phpstan
2192
2293
Creating a Command
2394
------------------
@@ -40,11 +111,6 @@ want a command to create a user::
40111
{
41112
protected static $defaultName = 'app:create-user';
42113

43-
protected function configure(): void
44-
{
45-
// ...
46-
}
47-
48114
protected function execute(InputInterface $input, OutputInterface $output): int
49115
{
50116
// ... put here the code to create the user
@@ -67,37 +133,41 @@ want a command to create a user::
67133
}
68134

69135
Configuring the Command
70-
-----------------------
136+
~~~~~~~~~~~~~~~~~~~~~~~
71137

72138
You can optionally define a description, help message and the
73-
:doc:`input options and arguments </console/input>`::
139+
:doc:`input options and arguments </console/input>` by overriding the
140+
``configure()`` method::
74141

75-
// ...
76-
// the command description shown when running "php bin/console list"
77-
protected static $defaultDescription = 'Creates a new user.';
142+
// src/Command/CreateUserCommand.php
78143

79144
// ...
80-
protected function configure(): void
145+
class CreateUserCommand extends Command
81146
{
82-
$this
83-
// If you don't like using the $defaultDescription static property,
84-
// you can also define the short description using this method:
85-
// ->setDescription('...')
147+
// the command description shown when running "php bin/console list"
148+
protected static $defaultDescription = 'Creates a new user.';
86149

87-
// the command help shown when running the command with the "--help" option
88-
->setHelp('This command allows you to create a user...')
89-
;
150+
// ...
151+
protected function configure(): void
152+
{
153+
$this
154+
// the command help shown when running the command with the "--help" option
155+
->setHelp('This command allows you to create a user...')
156+
;
157+
}
90158
}
91159

92-
Defining the ``$defaultDescription`` static property instead of using the
93-
``setDescription()`` method allows to get the command description without
94-
instantiating its class. This makes the ``php bin/console list`` command run
95-
much faster.
160+
.. tip::
161+
162+
Defining the ``$defaultDescription`` static property instead of using the
163+
``setDescription()`` method allows to get the command description without
164+
instantiating its class. This makes the ``php bin/console list`` command run
165+
much faster.
96166

97-
If you want to always run the ``list`` command fast, add the ``--short`` option
98-
to it (``php bin/console list --short``). This will avoid instantiating command
99-
classes, but it won't show any description for commands that use the
100-
``setDescription()`` method instead of the static property.
167+
If you want to always run the ``list`` command fast, add the ``--short`` option
168+
to it (``php bin/console list --short``). This will avoid instantiating command
169+
classes, but it won't show any description for commands that use the
170+
``setDescription()`` method instead of the static property.
101171

102172
The ``configure()`` method is called automatically at the end of the command
103173
constructor. If your command defines its own constructor, set the properties
@@ -132,7 +202,7 @@ available in the ``configure()`` method::
132202
}
133203

134204
Registering the Command
135-
-----------------------
205+
~~~~~~~~~~~~~~~~~~~~~~~
136206

137207
In PHP 8 and newer versions, you can register the command by adding the
138208
``AsCommand`` attribute to it::
@@ -143,6 +213,8 @@ In PHP 8 and newer versions, you can register the command by adding the
143213
use Symfony\Component\Console\Attribute\AsCommand;
144214
use Symfony\Component\Console\Command\Command;
145215

216+
// the "name" and "description" arguments of AsCommand replace the
217+
// static $defaultName and $defaultDescription properties
146218
#[AsCommand(
147219
name: 'app:create-user',
148220
description: 'Creates a new user.',
@@ -159,8 +231,8 @@ If you can't use PHP attributes, register the command as a service and
159231
:ref:`default services.yaml configuration <service-container-services-load-example>`,
160232
this is already done for you, thanks to :ref:`autoconfiguration <services-autoconfigure>`.
161233

162-
Executing the Command
163-
---------------------
234+
Running the Command
235+
~~~~~~~~~~~~~~~~~~~
164236

165237
After configuring and registering the command, you can run it in the terminal:
166238

@@ -442,7 +514,7 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
442514

443515
$application = new Application();
444516
$application->setAutoExit(false);
445-
517+
446518
$tester = new ApplicationTester($application);
447519

448520
.. note::

console/input.rst

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,87 @@ The above code can be simplified as follows because ``false !== null``::
304304
$yell = ($optionValue !== false);
305305
$yellLouder = ($optionValue === 'louder');
306306

307+
Adding Argument/Option Value Completion
308+
---------------------------------------
309+
310+
If :ref:`Console completion is installed <console-completion-setup>`,
311+
command and option names will be auto completed by the shell. However, you
312+
can also implement value completion for the input in your commands. For
313+
instance, you may want to complete all usernames from the database in the
314+
``name`` argument of your greet command.
315+
316+
To achieve this, override the ``complete()`` method in the command::
317+
318+
// ...
319+
use Symfony\Component\Console\Completion\CompletionInput;
320+
use Symfony\Component\Console\Completion\CompletionSuggestions;
321+
322+
class GreetCommand extends Command
323+
{
324+
// ...
325+
326+
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
327+
{
328+
if ($input->mustSuggestArgumentValuesFor('names')) {
329+
// the user asks for completion input for the "names" option
330+
331+
// the value the user already typed, e.g. when typing "app:greet Fa" before
332+
// pressing Tab, this will contain "Fa"
333+
$currentValue = $input->getCompletionValue();
334+
335+
// get the list of username names from somewhere (e.g. the database)
336+
// you may use $currentValue to filter down the names
337+
$availableUsernames = ...;
338+
339+
// then add the retrieved names as suggested values
340+
$suggestions->suggestValues($availableUsernames);
341+
}
342+
}
343+
}
344+
345+
That's all you need! Assuming users "Fabien" and "Fabrice" exist, pressing
346+
tab after typing ``app:greet Fa`` will give you these names as a suggestion.
347+
348+
.. tip::
349+
350+
The bash shell is able to handle huge amounts of suggestions and will
351+
automatically filter the suggested values based on the existing input
352+
from the user. You do not have to implement any filter logic in the
353+
command.
354+
355+
You may use ``CompletionInput::getCompletionValue()`` to get the
356+
current input if that helps improving performance (e.g. by reducing the
357+
number of rows fetched from the database).
358+
359+
Testing the Completion script
360+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
361+
362+
The Console component comes with a special
363+
:class:`Symfony\\Component\\Console\\Test\\CommandCompletionTester`` class
364+
to help you unit test the completion logic::
365+
366+
// ...
367+
use Symfony\Component\Console\Application;
368+
369+
class GreetCommandTest extends TestCase
370+
{
371+
public function testComplete()
372+
{
373+
$application = new Application();
374+
$application->add(new GreetCommand());
375+
376+
// create a new tester with the greet command
377+
$tester = new CommandCompletionTester($application->get('app:greet'));
378+
379+
// complete the input without any existing input (the empty string represents
380+
// the position of the cursor)
381+
$suggestions = $tester->complete(['']);
382+
$this->assertSame(['Fabien', 'Fabrice', 'Wouter'], $suggestions);
383+
384+
// complete the input with "Fa" as input
385+
$suggestions = $tester->complete(['Fa']);
386+
$this->assertSame(['Fabien', 'Fabrice'], $suggestions);
387+
}
388+
}
389+
307390
.. _`docopt standard`: http://docopt.org/

page_creation.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ the debugging routes in the next section.
195195

196196
You'll learn about many more commands as you continue!
197197

198+
.. tip::
199+
200+
If you are using the Bash shell, you can set up completion support.
201+
This autocompletes commands and other input when using ``bin/console``.
202+
See :ref:`the Console document <console-completion-setup>` for more
203+
information on how to set up completion.
204+
198205
.. _web-debug-toolbar:
199206

200207
The Web Debug Toolbar: Debugging Dream

security.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,9 +2451,10 @@ However, in some cases, this process can cause unexpected authentication problem
24512451
If you're having problems authenticating, it could be that you *are* authenticating
24522452
successfully, but you immediately lose authentication after the first redirect.
24532453

2454-
In that case, review the serialization logic (e.g. ``SerializableInterface``) on
2455-
you user class (if you have any) to make sure that all the fields necessary are
2456-
serialized.
2454+
In that case, review the serialization logic (e.g. the ``__serialize()`` or
2455+
``serialize()`` methods) on you user class (if you have any) to make sure
2456+
that all the fields necessary are serialized and also exclude all the
2457+
fields not necessary to be serialized (e.g. Doctrine relations).
24572458

24582459
Comparing Users Manually with EquatableInterface
24592460
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)