Skip to content

Commit

Permalink
feature #5319 [Console] Command Lifecycle explications (94noni)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wouterj committed Aug 14, 2015
2 parents 3189caf + f45c392 commit 84c922a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions components/console/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,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
Expand Down

0 comments on commit 84c922a

Please sign in to comment.