diff --git a/components/console/introduction.rst b/components/console/introduction.rst index eda67260924..8f68a5c7d66 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -110,6 +110,31 @@ This prints:: HELLO FABIEN +Command Lifecycle +~~~~~~~~~~~~~~~~~ + +Commands have three lifecycle methods: + +:method:`Symfony\\Component\\Console\\Command\\Command::initialize` + This method is executed before the ``interact()`` and the ``execute()`` + methods. It's main purpose is to initialize the variables used in the + rest of the command methods. + +:method:`Symfony\\Component\\Console\\Command\\Command::interact` + This method is executed after ``initialize()`` and before ``execute()``. + Its purpose is to check if some of the options/arguments are missing + and interactively ask the user for those values. This is the last place + where you can ask for missing options/arguments otherwise the command + will throw an error. + +:method:`Symfony\\Component\\Console\\Command\\Command::execute` + This method is executed after ``interact()`` and ``initialize()``. + It contains the logic you want the command executes. + +Note that ``execute()`` is the only required method of the three. + +The ``initialize()`` and ``interact()`` methods are completely optional. + .. _components-console-coloring: Coloring the Output