Symfony 4: Automate your Workflow - Fabien Potencier

Planet PHP

Guest
Symfony 4's most "innovative" feature is the way it drives the day-to-day application management. No more tedious copy/paste from README files. No more boilerplate code. Automation to the max. On a curated list of Composer packages.

Symfony Flex#


Symfony 4 is powered by Symfony Flex, a deceptively simple but powerful Composer plugin. The default Symfony skeleton only lists two main dependencies: symfony/flex and symfony/framework-bundle. Everything else is optional.

As you can imagine, most projects need more than FrameworkBundle. And installing and configuring all packages and bundles by hand is error-prone, tedious, and just plain boring. Even for the few dependencies that Symfony Standard Edition depends on.

This is where Symfony Flex shines.

When you install a Composer package, Flex hooks into the installation process to automate its integration into your project via pre-defined and community-driven recipes. It works for any Composer packages, not just Symfony bundles. If a recipe does not exist, you can still configure it the old way, like you currently do.

Take sensiolabs/security-checker as an example. It works for any PHP projects. But when installed on a project using Symfony Flex, it knows how to run the checker whenever you run composer install.

The automation is implemented in a recipe that has a JSON manifest that describes how to integrate the package into a Symfony application. Here is the one for sensiolabs/security-checker:

{
"copy-from-recipe": {
"etc/": "%ETC_DIR%/"
},
"composer-scripts": {
"vendor/bin/security-checker security:check": "php-script"
}
}

Flex currently implements 9 "configurators" that helps automate the integration of any PHP package into a Symfony application: copy-from-recipe, copy-from-package, bundles, env, container, makefile, composer-scripts, gitignore, and post-install-output.

Bundles#


The bundles configurator enables bundles in your project by adding them to the bundles.php file (and it removes them when you uninstall the dependency):

{
"bundles": {
"Symfony\\Bundle\\DebugBundle\\DebugBundle": ["dev", "test"],
"Symfony\\Bundle\\MonologBundle\\MonologBundle": ["all"]
}
}
Configuration#


Configuration can be added via the copy-from-recipe and copy-from-package configurators: the former copies files from the recipe while the later copies files from the Composer package itself.

{
"copy-from-package": {
"bin/check.php": "%BIN_DIR%/check.php"
},
"copy-from-recipe": {
"etc/": "%ETC_DIR%/",
"src/": "%SRC_DIR%/"
}
}
Environment Variables#


The env configurator knows how to add environment variables to the project's .env and .env.dist files (and removes them when uninstalling the dependency):

{
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1"
}
}
Makefile Tasks#


The makefile configurator adds new tasks to the project's Makefile (and removes them when unintalling the dependency):

{
"makefile": [
"serve:",
"\t@echo \"\\033[32;49mServer listening on http://127.0.0.1:8000\\033[39m\"",
"\t@echo \"Quit the server with CTRL-C.\"",
"\t@echo \"Run \\033[32mcomposer require symfony/web-server-bundle\\033[39m for a better web server\"",
"\tphp -S 127.0.0.1:8000 -t web",
".PHONY: serve"
]
}
Composer Scripts#


The composer.json file defined in symfony/skeleton contains the following snippet:

{
"scripts": {
"auto-scripts": [
],
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
}
}

The composer-scripts configurator manages the special @auto-scripts section by adding commands and scripts defined in recipes. Those a

Truncated by Planet PHP, read more at the original (another 5743 bytes)

Читать дальше...
 
Последнее редактирование модератором:
Сверху