@@ -9,15 +9,90 @@ The Symfony framework provides lots of commands through the ``bin/console`` scri
9
9
created with the :doc: `Console component </components/console >`. You can also
10
10
use it to create your own commands.
11
11
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
+ ~~~~~~~~~~~~~~~~~~~
14
43
15
44
Console commands run in the :ref: `environment <config-dot-env >` defined in the ``APP_ENV ``
16
45
variable of the ``.env `` file, which is ``dev `` by default. It also reads the ``APP_DEBUG ``
17
46
value to turn "debug" mode on or off (it defaults to ``1 ``, which is on).
18
47
19
48
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
+ .. versionadded :: 5.4
63
+
64
+ Console completion for Bash was introduced in Symfony 5.4.
65
+
66
+ If you are using the Bash shell, you can install Symfony's completion
67
+ script to get auto completion when typing commands in the terminal. All
68
+ commands support name and option completion, and some can even complete
69
+ values.
70
+
71
+ .. image :: /_images/components/console/completion.gif
72
+
73
+ First, make sure you installed and setup the "bash completion" package for
74
+ your OS (typically named ``bash-completion ``). Then, install the Symfony
75
+ completion bash script *once * by running the ``completion `` command in a
76
+ Symfony app installed on your computer:
77
+
78
+ .. code-block :: terminal
79
+
80
+ $ php bin/console completion bash | sudo tee /etc/bash_completion.d/console-events-terminate
81
+ # after the installation, restart the shell
82
+
83
+ Now you are all set to use the auto completion for all Symfony Console
84
+ applications on your computer. By default, you can get a list of complete
85
+ options by pressing the Tab key.
86
+
87
+ .. tip ::
88
+
89
+ Many PHP tools are built using the Symfony Console component (e.g.
90
+ Composer, PHPstan and Behat). If they are using version 5.4 or higher,
91
+ you can also install their completion script to enable console completion:
92
+
93
+ .. code-block :: terminal
94
+
95
+ $ php vendor/bin/phpstan completion bash | sudo tee /etc/bash_completion.d/phpstan
21
96
22
97
Creating a Command
23
98
------------------
@@ -40,11 +115,6 @@ want a command to create a user::
40
115
{
41
116
protected static $defaultName = 'app:create-user';
42
117
43
- protected function configure(): void
44
- {
45
- // ...
46
- }
47
-
48
118
protected function execute(InputInterface $input, OutputInterface $output): int
49
119
{
50
120
// ... put here the code to create the user
@@ -67,37 +137,41 @@ want a command to create a user::
67
137
}
68
138
69
139
Configuring the Command
70
- -----------------------
140
+ ~~~~~~~~~~~~~~~~~~~~~~~
71
141
72
142
You can optionally define a description, help message and the
73
- :doc: `input options and arguments </console/input >`::
143
+ :doc: `input options and arguments </console/input >` by overriding the
144
+ ``configure() `` method::
74
145
75
- // ...
76
- // the command description shown when running "php bin/console list"
77
- protected static $defaultDescription = 'Creates a new user.';
146
+ // src/Command/CreateUserCommand.php
78
147
79
148
// ...
80
- protected function configure(): void
149
+ class CreateUserCommand extends Command
81
150
{
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('...')
151
+ // the command description shown when running "php bin/console list"
152
+ protected static $defaultDescription = 'Creates a new user.';
86
153
87
- // the command help shown when running the command with the "--help" option
88
- ->setHelp('This command allows you to create a user...')
89
- ;
154
+ // ...
155
+ protected function configure(): void
156
+ {
157
+ $this
158
+ // the command help shown when running the command with the "--help" option
159
+ ->setHelp('This command allows you to create a user...')
160
+ ;
161
+ }
90
162
}
91
163
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.
164
+ .. tip ::
165
+
166
+ Defining the ``$defaultDescription `` static property instead of using the
167
+ ``setDescription() `` method allows to get the command description without
168
+ instantiating its class. This makes the ``php bin/console list `` command run
169
+ much faster.
96
170
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.
171
+ If you want to always run the ``list `` command fast, add the ``--short `` option
172
+ to it (``php bin/console list --short ``). This will avoid instantiating command
173
+ classes, but it won't show any description for commands that use the
174
+ ``setDescription() `` method instead of the static property.
101
175
102
176
The ``configure() `` method is called automatically at the end of the command
103
177
constructor. If your command defines its own constructor, set the properties
@@ -132,7 +206,7 @@ available in the ``configure()`` method::
132
206
}
133
207
134
208
Registering the Command
135
- -----------------------
209
+ ~~~~~~~~~~~~~~~~~~~~~~~
136
210
137
211
In PHP 8 and newer versions, you can register the command by adding the
138
212
``AsCommand `` attribute to it::
@@ -143,6 +217,8 @@ In PHP 8 and newer versions, you can register the command by adding the
143
217
use Symfony\Component\Console\Attribute\AsCommand;
144
218
use Symfony\Component\Console\Command\Command;
145
219
220
+ // the "name" and "description" arguments of AsCommand replace the
221
+ // static $defaultName and $defaultDescription properties
146
222
#[AsCommand(
147
223
name: 'app:create-user',
148
224
description: 'Creates a new user.',
@@ -159,8 +235,8 @@ If you can't use PHP attributes, register the command as a service and
159
235
:ref: `default services.yaml configuration <service-container-services-load-example >`,
160
236
this is already done for you, thanks to :ref: `autoconfiguration <services-autoconfigure >`.
161
237
162
- Executing the Command
163
- ---------------------
238
+ Running the Command
239
+ ~~~~~~~~~~~~~~~~~~~
164
240
165
241
After configuring and registering the command, you can run it in the terminal:
166
242
@@ -442,7 +518,7 @@ call ``setAutoExit(false)`` on it to get the command result in ``CommandTester``
442
518
443
519
$application = new Application();
444
520
$application->setAutoExit(false);
445
-
521
+
446
522
$tester = new ApplicationTester($application);
447
523
448
524
.. note ::
0 commit comments