Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection][Routing] Define array-shapes to help writing PHP configs using yaml-like arrays#61490
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
0c19d7b to811122eCompare811122e to70bd523Comparesrc/Symfony/Component/DependencyInjection/Loader/Config/ServicesConfig.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
4f5e73e to89084e0Comparee66a18c to886decbCompare| * file that was distributed with this source code. | ||
| */ | ||
| namespaceSymfony\Config; |
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 file is adding the functions of the PHP-DSL to theSymfony\Config namespace. It's loaded byPhpFileLoader.
src/Symfony/Component/DependencyInjection/Loader/Config/ServicesConfig.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/ConfiguratorTrait.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
| /** | ||
| * Creates a parameter. | ||
| */ | ||
| functionparam(string$name):ParamConfigurator |
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.
functions moved to afunctions.php file
src/Symfony/Component/DependencyInjection/Loader/Configurator/functions.phpShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
886decb todc55ebfComparealexandre-daubois commentedOct 6, 2025
Great, the new app helpers are cool! I stumbled across 2 things while testing:
|
1912e56 to469ea85Compare| finalprotectedfunctionloadExtensionConfig(string$namespace,array$config,string$file ='?'):void | ||
| { | ||
| if (\in_array($namespace, ['imports','services','parameters'],true)) { |
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 added this code to 7.4 in a previous PR but I'm now moving this to PhpFileLoader
469ea85 to0f18bf6Comparenicolas-grekas commentedOct 7, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I removed AppServicesConfig and AppRoutesConfig: it brings other DX issues. We can add them in a separate PR later. |
aa9cec4 to5a68445Compare…P configs using yaml-like arrays
5a68445 to38306edCompareUh oh!
There was an error while loading.Please reload this page.
| useSymfony\Component\DependencyInjection\Tests\Fixtures\Bar; | ||
| useSymfony\Config\ServicesConfig; |
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.
It would be the same, but should it use theSymfony\Config namespace?
| useSymfony\Component\DependencyInjection\Tests\Fixtures\Bar; | |
| useSymfony\Config\ServicesConfig; | |
| namespaceSymfony\Config; | |
| useSymfony\Component\DependencyInjection\Tests\Fixtures\Bar; |
alexandre-daubois commentedOct 8, 2025
For the record, this issue:https://youtrack.jetbrains.com/issue/WI-81098/Comment-added-after-nested-int-range-declaration-in-PhpDocs-array-shape-breaks-parsing is required to have the full autocomplete. Currently, this breaks on FrameworkBundle config for example where highlight stops at workflow's transitions weight. ![]() |
fabpot commentedOct 9, 2025
Thank you@nicolas-grekas. |
b0d728c intosymfony:7.4Uh oh!
There was an error while loading.Please reload this page.
… format for semantic configuration (nicolas-grekas)This PR was merged into the 7.4 branch.Discussion----------[Config][DependencyInjection] Deprecate the fluent PHP format for semantic configuration| Q | A| ------------- | ---| Branch? | 7.4| Bug fix? | no| New feature? | yes| Deprecations? | no| Issues | -| License | MITThis PR deprecates the fluent PHP format for semantic configuration introduced in Symfony 5.4 by `@Nyholm` (see#40600).It aims to replace it with the new array-based PHP config format (see#61490).The fluent PHP config format was a great experiment:- It helped us improve the Config component and the code generation of fluent config builders.- It confirmed the community’s interest in PHP-based configuration.- And it showed us its limits.Those limits are structural. Writing fluent config is difficult and full of edge cases. Its rigidity comes from having to match one canonical interpretation of the semantic config tree. Automatic code generation can’t capture the custom logic that before-normalizers introduce, yet those normalizers are essential for flexibility and backward compatibility. This rigidity makes fluent config fragile. How do we deal with this fragility as config tree authors? At the moment, we don't care. Maybe this format is too niche for you to have experienced this issue, but we cannot guarantee that simple upgrades won't break your fluent PHP config.The new array-based PHP format builds directly on the same code used for loading YAML configs.That means:- trivial conversion between YAML and PHP arrays,- identical flexibility and behavior,- and support for auto-completion and static analysis through generated array shapes.The generated array shapes are rigid too, but that rigidity is non-breaking: even if your config no longer matches the canonical shape, your app keeps working. Static analyzers might warn you: that’s an invitation to update, not a failure.I'm submitting this PR a bit l late for 7.4 but I think it's important to merge now. Deprecating the fluent PHP config format will:- prevent new code from relying on a fragile approach,- make room in the documentation for the array-based format,- and consolidate Symfony’s configuration story around a robust PHP-based format.Fluent PHP for semantic config served us well but it's time to retire it. ```diff -return function (AcmeConfig $config) { - $config->color('red'); -} +return new AcmeConfig([ + 'color' => 'red', +]); ```PS: there's another fluent config format for services and routes (see#23834 and#24180). This other format is handwritten. It doesn't have the issues listed above and it is *not* deprecated. It's actually the recommended way *for bundles* to declare their config (instead of XML, see#60568).Commits-------332b4ac [Config][DependencyInjection] Deprecate the fluent PHP format for semantic configuration
… 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 | MITThis 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<?phpnamespace Symfony\Component\DependencyInjection\Loader\Configurator;return App::config([ 'services' => [ 'App\\' => [ 'resource' => '../src/' ], ],]);```and `config/routes.php` would start as:```php<?phpnamespace 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

Uh oh!
There was an error while loading.Please reload this page.
Related to#58771.
This PR adds array-shape helpers:
An example for
routes.php:And one for
services.php:The
ServicesConfigandRoutesConfigwrappers around the arrays are totally optional: the style in#61894 remains possible.Yet, those wrappers provide array-shapes that can be used by static analyzers and IDEs.
This also works when nesting behind
when@%env%:Note that as far as IDEs are concerned, autocompletion doesn't work yet for the complex shapes used in
ServicesConfig- neither in vscode nor in phpstorm /cc@pronskiy 🙏