Skip to content

Commit f3007bf

Browse files
committed
minor #8115 Added an introductory article for Symfony Flex (javiereguiluz)
This PR was squashed before being merged into the 3.3 branch (closes #8115). Discussion ---------- Added an introductory article for Symfony Flex We need some official doc about Flex as soon as possible. Later we'll add more articles about it as needed. Thanks! Commits ------- dba7c3d Added an introductory article for Symfony Flex
2 parents a804701 + dba7c3d commit f3007bf

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

setup/flex.rst

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
.. index:: Flex
2+
3+
Using Symfony Flex to Manage Symfony Applications
4+
=================================================
5+
6+
`Symfony Flex`_ is the new way to install and manage Symfony applications. Flex
7+
is not a new Symfony version, but a tool that replaces and improves the
8+
`Symfony Installer`_.
9+
10+
Symfony Flex **automates the most common tasks of Symfony applications**, such
11+
as installing and removing bundles and other dependencies. Symfony Flex works
12+
for Symfony 3.3 and newer versions. Starting from Symfony 4.0, Flex will be used
13+
by default, but it will still be optional to use.
14+
15+
How Does Flex Work
16+
------------------
17+
18+
Internally, Symfony Flex is a Composer plugin that modifies the behavior of the
19+
``require`` and ``update`` commands. When installing or updating dependencies
20+
in a Flex-enabled application, Symfony can perform tasks before and after the
21+
execution of Composer tasks.
22+
23+
Consider the following example:
24+
25+
.. code-block:: terminal
26+
27+
$ cd my-project/
28+
$ composer require mailer
29+
30+
If you execute that command in a Symfony application which doesn't use Flex,
31+
you'll see a Composer error explaining that ``mailer`` is not a valid package
32+
name. However, if the application has Symfony Flex installed, that command ends
33+
up installing and enabling the SwiftmailerBundle, which is the best way to
34+
integrate Swiftmailer, the official mailer for Symfony applications.
35+
36+
When Symfony Flex is installed in the application and you execute ``composer require``,
37+
the application makes a request to Symfony Flex servers before trying to install
38+
the package with Composer:
39+
40+
* If there's no information about that package, Flex server returns nothing and
41+
the package installation follows the usual procedure based on Composer;
42+
* If there's special information about that package, Flex returns it in a file
43+
called "recipe" and the application uses it to decide which package to install
44+
and which automated tasks to run after the installation.
45+
46+
In the above example, Symfony Flex asks about the ``mailer`` package and the
47+
Symfony Flex server detects that ``mailer`` is in fact an alias for SwiftmailerBundle
48+
and returns the "recipe" for it.
49+
50+
Symfony Flex Recipes
51+
~~~~~~~~~~~~~~~~~~~~
52+
53+
Recipes are defined in a ``manifest.json`` file and can contain any number of
54+
other files and directories. For example, this is the ``manifest.json`` for SwiftmailerBundle:
55+
56+
.. code-block:: javascript
57+
58+
{
59+
"bundles": {
60+
"Symfony\\Bundle\\SwiftmailerBundle\\SwiftmailerBundle": ["all"]
61+
},
62+
"copy-from-recipe": {
63+
"config/": "%CONFIG_DIR%/"
64+
},
65+
"env": {
66+
"MAILER_URL": "smtp://localhost:25?encryption=&auth_mode="
67+
},
68+
"aliases": ["mailer", "mail"]
69+
}
70+
71+
The ``alias`` option allows Flex to install packages using short and easy to
72+
remember names (``composer require mailer`` vs ``composer require symfony/swiftmailer-bundle``).
73+
The ``bundles`` option tells Flex in which environments should this bundle be
74+
enabled automatically (``all`` in this case). The ``env`` option makes Flex to
75+
add new environment variables to the application. Finally, the ``copy-from-recipe``
76+
option allows the recipe to copy files and directories into your application.
77+
78+
The instructions defined in this ``manifest.json`` file are also used by Symfony
79+
Flex when uninstalling dependencies (e.g. ``composer remove mailer``) to undo
80+
all changes. This means that Flex can remove the SwiftmailerBundle from the
81+
application, delete the ``MAILER_URL`` environment variable and any other file
82+
and directory created by this recipe.
83+
84+
Symfony Flex recipes are contributed by the community and they are stored in
85+
two public repositories:
86+
87+
* `Main recipe repository`_, is a curated list of recipes for high quality and
88+
maintained packages. Symfony Flex only looks in this repository by default.
89+
* `Contrib recipe repository`_, contains all the recipes created by the community.
90+
All of them are guaranteed to work, but their associated packages could be
91+
unmaintained. Symfony Flex ignores these recipes by default, but you can execute
92+
this command to start using them in your project:
93+
94+
.. code-block:: terminal
95+
96+
$ cd your-project/
97+
$ composer config extra.symfony.allow-contrib true
98+
99+
Read the `Symfony Recipes documentation`_ to learn everything about how to
100+
create recipes for your own packages.
101+
102+
Using Symfony Flex in New Applications
103+
--------------------------------------
104+
105+
Symfony has published a new "skeleton" project, which is a minimal Symfony
106+
project recommended to create new applications. This "skeleton" already includes
107+
Symfony Flex as a dependency, so you can create a new Flex-enabled Symfony
108+
application executing the following command:
109+
110+
.. code-block:: terminal
111+
112+
$ composer create-project symfony/skeleton my-project
113+
114+
.. note::
115+
116+
The use of the Symfony Installer to create new applications is no longer
117+
recommended since Symfony 3.3. Use Composer ``create-project`` command instead.
118+
119+
Upgrading Existing Applications to Flex
120+
---------------------------------------
121+
122+
Using Symfony Flex is optional, even in Symfony 4, where Flex will be used by
123+
default. However, Flex is so convenient and improves your productivity so much
124+
that it's strongly recommended to upgrade your existing applications to it.
125+
126+
The only caveat is that Symfony Flex requires that applications use the
127+
following directory structure, which is the same used by default in Symfony 4:
128+
129+
.. code-block:: text
130+
131+
your-project/
132+
├── Makefile
133+
├── config/
134+
│   ├── bundles.php
135+
│   ├── container.yaml
136+
│   ├── packages/
137+
│   └── routing.yaml
138+
├── public/
139+
│   └── index.php
140+
├── src/
141+
│   ├── ...
142+
│   └── Kernel.php
143+
├── templates/
144+
└── vendor/
145+
146+
This means that installing the ``symfony/flex`` dependency in your application
147+
is not enough. You must also upgrade the directory structure to the one showed
148+
above. Sadly, there's no automatic tool to make this upgrade, so you must follow
149+
these manual steps:
150+
151+
1. Create a new empty Symfony application (``composer create-project symfony/skeleton my-project-flex``)
152+
2. Copy the ``require`` and ``require-dev`` dependencies defined in your original
153+
project's ``composer.json`` file to the ``composer.json`` file of the new project.
154+
3. Install the dependencies in the new project executing ``composer install``. This
155+
will make Symfony Flex generate all the configuration files in ``config/packages/``
156+
4. Review the generated ``config/packages/*.yaml`` files and make any needed
157+
changes according to the configuration defined in the ``app/config/config_*.yml``
158+
file of your original project. Beware that this is the most time-consuming
159+
and error-prone step of the upgrade process.
160+
5. Move the original parameters defined in ``app/config/parameters.*.yml`` to the
161+
new ``config/container.yaml`` and ``.env`` files depending on your needs.
162+
6. Move the original source code from ``src/{App,...}Bundle/`` to ``src/`` and
163+
update the namespaces of every PHP file (advanced IDEs can do this automatically).
164+
7. Move the original templates from ``app/Resources/views/`` to ``templates/``
165+
8. Make any other change needed by your application. For example, if your original
166+
``web/app_*.php`` front controllers were customized, add those changes to the
167+
new ``public/index.php`` controller.
168+
169+
.. _`Symfony Flex`: https://github.com/symfony/flex
170+
.. _`Symfony Installer`: https://github.com/symfony/symfony-installer
171+
.. _`Main recipe repository`: https://github.com/symfony/recipes
172+
.. _`Contrib recipe repository`: https://github.com/symfony/recipes-contrib
173+
.. _`Symfony Recipes documentation`: https://github.com/symfony/recipes/blob/master/README.rst

0 commit comments

Comments
 (0)