-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This comment was marked as off-topic.
This comment was marked as off-topic.
0c19d7b to
811122e
Compare
811122e to
70bd523
Compare
src/Symfony/Component/DependencyInjection/Loader/Config/ServicesConfig.php
Outdated
Show resolved
Hide resolved
4f5e73e to
89084e0
Compare
e66a18c to
886decb
Compare
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\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 the Symfony\Config namespace. It's loaded by PhpFileLoader.
src/Symfony/Component/DependencyInjection/Loader/Config/ServicesConfig.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/ConfiguratorTrait.php
Show resolved
Hide resolved
| /** | ||
| * Creates a parameter. | ||
| */ | ||
| function param(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 a functions.php file
src/Symfony/Component/DependencyInjection/Loader/Configurator/functions.php
Show resolved
Hide resolved
886decb to
dc55ebf
Compare
|
Great, the new app helpers are cool! I stumbled across 2 things while testing:
|
1912e56 to
469ea85
Compare
|
|
||
| final protected function loadExtensionConfig(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 to
0f18bf6
Compare
|
I removed AppServicesConfig and AppRoutesConfig: it brings other DX issues. We can add them in a separate PR later. |
aa9cec4 to
5a68445
Compare
…P configs using yaml-like arrays
5a68445 to
38306ed
Compare
| use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar; | ||
| use Symfony\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 the Symfony\Config namespace?
| use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar; | |
| use Symfony\Config\ServicesConfig; | |
| namespace Symfony\Config; | |
| use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar; |
|
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.
|
|
Thank you @nicolas-grekas. |
… 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 | MIT This 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 | 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

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 🙏