-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Replace the fluent PHP config format by the array-shape one #21511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 8.0
Are you sure you want to change the base?
Conversation
dc1e644 to
730f41f
Compare
|
PR ready. |
097460b to
776698b
Compare
|
Thanks for this! We should merge this soon to aovid merge conflicts with other PRs. However, GitHub shows errors on these 5 files, so we should fix those:
|
5372241 to
106f154
Compare
106f154 to
f86a9b6
Compare
|
PR updated after symfony/symfony#62129 |
f86a9b6 to
9fd693b
Compare
… to assist in writing and discovering app's configuration (nicolas-grekas) This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [FrameworkBundle] Auto-generate `config/reference.php` to assist in writing and discovering app's configuration | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Doc PR | symfony/symfony-docs#21511 | License | MIT This PR reverts #61490 and #61885, and builds on #61894. These reverts explain a big chunk of the attached patch. This adds a compiler pass that generates a `config/reference.php` file. This file contains two classes that define array-shapes for app's and routing configuration. Part of these shapes are auto-generated from the list of bundles found in `config/bundles.php`. The `config/reference.php` file should be loaded by a new line in the "autoload" entry of composer.json files: `"classmap": ["config/"]` - recipe update pending. This means that the file should be committed. This is on purpose: as the name suggests, this file is also a config reference for human readers. Having to commit the changes is also a nice way to convey config improvements to the community - at least for ppl that review their commits ;). It also solves a discovery problem that happens with phpstan/etc having a hard time to find the classes currently generated for config builders in the cache directory. With this PR, `config/services.php` could start as such: ```php <?php namespace Symfony\Component\DependencyInjection\Loader\Configurator; return App::config([ 'services' => [ 'App\\' => [ 'resource' => '../src/' ], ], ]); ``` and `config/routes.php` would start as: ```php <?php namespace Symfony\Component\Routing\Loader\Configurator; return Routes::config([ 'controllers' => [ 'resource' => 'attributes', 'type' => 'tagged_services', ] ]); ``` The generated shapes use advanced features that are not fully supported by phpstan / phpstorm. But the gap should be closed soon. PS: https://symfony.com/blog/new-in-symfony-7-4-deprecated-xml-configuration will need an update. Commits ------- 22349f5 [FrameworkBundle] Auto-generate `config/reference.php` to assist in writing and discovering app's configuration
2ae8fe4 to
a042e8c
Compare
|
I replaced examples that used |
a042e8c to
93757d2
Compare
| twig: | ||
| strict_variables: true | ||
| # config/packages/twig.yaml | ||
| when@test: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice update 👍🏻
Consistent with the Twig recipe.
| return App::config([ | ||
| 'services' => [ | ||
| // ... | ||
| 'Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a use statement for this class:
| 'Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler' => [ | |
| RedisSessionHandler::class => [ |
| // ['prefix' => 'my_prefix', 'ttl' => 600], | ||
| ], | ||
| ], | ||
| 'Redis' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 'Redis' => [ | |
| \Redis::class => [ |
| 'services' => [ | ||
| '_defaults' => [ | ||
| 'autowire' => true, // Automatically injects dependencies in your services. | ||
| 'autoconfigure' => true, // Automatically registers your services as commands, event subscribers, etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the default values for App::config(), you may comment them here?
| return static function (ContainerConfigurator $container): void { | ||
| $container->extension('acme_something', [ | ||
| // ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either we keep these ellipses in all formats, or we remove them. Let's be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove them from this example, that better matches the prepend example before this configuration block.
| $twig->debug('%kernel.debug%'); | ||
| }; | ||
| return App::config([ | ||
| 'twig' => $1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter missing here.
You could use the param function.
| 'twig' => $1, | |
| 'twig' => param('kernel.debug'), |
| return App::config([ | ||
| KernelEvents::EXCEPTION => 'onKernelException', | ||
| ]; | ||
| ]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a config file.
| return App::config([ | |
| KernelEvents::EXCEPTION => 'onKernelException', | |
| ]; | |
| ]); | |
| return [ | |
| KernelEvents::EXCEPTION => 'onKernelException', | |
| ]; |
| $twig->defaultPath('%kernel.project_dir%/resources/views'); | ||
| }; | ||
| return App::config([ | ||
| 'twig' => $1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 'twig' => $1, | |
| 'twig' => '%kernel.project_dir%/resources/views', |
| ; | ||
| }; | ||
| return App::config([ | ||
| 'doctrine' => $1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 'doctrine' => $1, | |
| 'doctrine' => env('DATABASE_PASSWORD'), |
| return App::config([ | ||
| 'content' => 'This is my custom problem normalizer.', | ||
| 'exception'=> [ | ||
| 'message' => $exception->getMessage(), | ||
| 'code' => $exception->getStatusCode(), | ||
| ], | ||
| ]; | ||
| ]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a config file.
| return App::config([ | |
| 'content' => 'This is my custom problem normalizer.', | |
| 'exception'=> [ | |
| 'message' => $exception->getMessage(), | |
| 'code' => $exception->getStatusCode(), | |
| ], | |
| ]; | |
| ]); | |
| return [ | |
| 'content' => 'This is my custom problem normalizer.', | |
| 'exception'=> [ | |
| 'message' => $exception->getMes 67DE sage(), | |
| 'code' => $exception->getStatusCode(), | |
| ], | |
| ]; |
| }; | ||
| return App::config([ | ||
| 'doctrine' => [ | ||
| 'orm' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
| 'orm' => [ | |
| 'orm' => [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stopped at "security/".
| 'services' => [ | ||
| AutoCommitListener::class => [ | ||
| 'tags' => [ | ||
| ['doctrine.event_listener' => ['event' => Events::onMigrationsMigrated]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we copy the Yaml format, it should be like this: what is correct?
| ['doctrine.event_listener' => ['event' => Events::onMigrationsMigrated]], | |
| [ | |
| 'name' => 'doctrine.event_listener', | |
| 'event' => Events::onMigrationsMigrated | |
| ], |
| # config/packages/doctrine.yaml | ||
| doctrine: | ||
| dbal: | ||
| url: '%env(DATABASE_URL)%' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with the PHP example:
| url: '%env(DATABASE_URL)%' | |
| url: '%env(resolve:DATABASE_URL)%' |
| 'assets' => [ | ||
| 'packages' => [ | ||
| 'avatars' => [ | ||
| 'base_urls' => ['http://static_cdn.example.com/avatars'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 'base_urls' => ['http://static_cdn.example.com/avatars'], | |
| 'base_urls' => ['https://static_cdn.example.com/avatars'], |
| assets: | ||
| packages: | ||
| avatars: | ||
| base_urls: 'http://static_cdn.example.com/avatars' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's apply security best practices.
| base_urls: 'https://static_cdn.example.com/avatars' |
| 'cache' => [ | ||
| 'pools' => [ | ||
| 'cache.mycache' => [ | ||
| 'adapter' => 'cache.adapter.redis', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a service id, but we could update the config to accept a Reference: symfony/symfony#62142
| 'headers' => [ | ||
| 'X-Powered-By' => 'ACME App', | ||
| ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Yaml example, this is inlined.
| 'headers' => [ | |
| 'X-Powered-By' => 'ACME App', | |
| ], | |
| 'headers' => ['X-Powered-By' => 'ACME App'], |
Just to highlight the difference, you can discard the suggestion.
| 'services' => [ | ||
| AvatarPackage::class => [ | ||
| 'tags' => [ | ||
| ['assets.package' => ['package' => 'avatars']], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both syntaxes work. which one should be documented?
| ['assets.package' => ['package' => 'avatars']], | |
| ['name' => 'assets.package', 'package' => 'avatars'], |
Related to
symfony/symfony#62092symfony/symfony#62129 and https://symfony.com/blog/new-in-symfony-7-4-deprecated-xml-configurationThe diff itself isn't really interesting. What's interesting is comparing yaml to php arrays.
This can give a nice overview of the new format.