Skip to content

Commit 84c922a

Browse files
committed
feature #5319 [Console] Command Lifecycle explications (94noni)
This PR was squashed before being merged into the 2.3 branch (closes #5319). Discussion ---------- [Console] Command Lifecycle explications | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | 2.3+ | Fixed tickets | #4996, #4366 Hi I took the doc form https://github.com/symfony/symfony-demo/blob/master/src/AppBundle/Command/AddUserCommand.php by @javiereguiluz as a minimal explanations for this, which IMO could be beneficial for devs Commits ------- f45c392 [Console] Command Lifecycle explications
2 parents 3189caf + f45c392 commit 84c922a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

components/console/introduction.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,31 @@ This prints::
112112

113113
HELLO FABIEN
114114

115+
Command Lifecycle
116+
~~~~~~~~~~~~~~~~~
117+
118+
Commands have three lifecycle methods:
119+
120+
:method:`Symfony\\Component\\Console\\Command\\Command::initialize`
121+
This method is executed before the ``interact()`` and the ``execute()``
122+
methods. It's main purpose is to initialize the variables used in the
123+
rest of the command methods.
124+
125+
:method:`Symfony\\Component\\Console\\Command\\Command::interact`
126+
This method is executed after ``initialize()`` and before ``execute()``.
127+
Its purpose is to check if some of the options/arguments are missing
128+
and interactively ask the user for those values. This is the last place
129+
where you can ask for missing options/arguments otherwise the command
130+
will throw an error.
131+
132+
:method:`Symfony\\Component\\Console\\Command\\Command::execute`
133+
This method is executed after ``interact()`` and ``initialize()``.
134+
It contains the logic you want the command executes.
135+
136+
Note that ``execute()`` is the only required method of the three.
137+
138+
The ``initialize()`` and ``interact()`` methods are completely optional.
139+
115140
.. _components-console-coloring:
116141

117142
Coloring the Output

0 commit comments

Comments
 (0)