Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
fabpot merged 1 commit intosymfony:7.4fromnicolas-grekas:di-array-shape
Oct 9, 2025

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedAug 21, 2025
edited
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
Issues-
LicenseMIT

Related to#58771.

This PR adds array-shape helpers:

An example forroutes.php:

useSymfony\Config\RoutesConfig;returnnewRoutesConfig(['controllers' => ['resource' =>'attributes','type' =>'tagged_services',    ],]);

And one forservices.php:

useApp\Bar;useSymfony\Config\ServicesConfig;returnnewServicesConfig(    defaults: ['autowire' =>true,'autoconfigure' =>true,    ],    services: ['App\\' => ['resource' =>'../src/']        Bar::class => null,'my_service' => ['class' => Bar::class,'arguments' => ['%foo%'],        ],    ]);

TheServicesConfig andRoutesConfig wrappers 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 behindwhen@%env%:

return ['when@dev' =>newServicesConfig([...]);];

Note that as far as IDEs are concerned, autocompletion doesn't work yet for the complex shapes used inServicesConfig - neither in vscode nor in phpstorm /cc@pronskiy 🙏

@carsonbot

This comment was marked as off-topic.

@nicolas-grekasnicolas-grekasforce-pushed thedi-array-shape branch 2 times, most recently from0c19d7b to811122eCompareAugust 21, 2025 14:36
@nicolas-grekasnicolas-grekas added this to the7.4 milestoneSep 16, 2025
@nicolas-grekasnicolas-grekas changed the title[DependencyInjection][Routing] Define array-shapes for config arrays[DependencyInjection][Routing] Define array-shapes to help writing PHP configs using yaml-like arraysSep 30, 2025
@nicolas-grekasnicolas-grekas marked this pull request as ready for reviewSeptember 30, 2025 14:28
* file that was distributed with this source code.
*/

namespaceSymfony\Config;
Copy link
MemberAuthor

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.

/**
* Creates a parameter.
*/
functionparam(string$name):ParamConfigurator
Copy link
MemberAuthor

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

GromNaN reacted with thumbs up emoji
@alexandre-daubois
Copy link
Member

Great, the new app helpers are cool! I stumbled across 2 things while testing:

  • PHPStorm isn't able to find the classes likeServicesConfig. It works, the container is compiled, but PHPStorm does no autocompletion and thinks the class doesn't exist. Is something possible to improve discoverability?
  • ../src is hard-coded inAppServicesConfig. When I tried to use it in my config file located inconfig/packages/my_config.php, I have the errorThe file "../src" does not exist. Should we warn, somewhere, that this class must only be used in a file located at the root of configs?

@nicolas-grekasnicolas-grekasforce-pushed thedi-array-shape branch 5 times, most recently from1912e56 to469ea85CompareOctober 7, 2025 14:20

finalprotectedfunctionloadExtensionConfig(string$namespace,array$config,string$file ='?'):void
{
if (\in_array($namespace, ['imports','services','parameters'],true)) {
Copy link
MemberAuthor

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

@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedOct 7, 2025
edited
Loading

I removed AppServicesConfig and AppRoutesConfig: it brings other DX issues. We can add them in a separate PR later.

@nicolas-grekasnicolas-grekasforce-pushed thedi-array-shape branch 2 times, most recently fromaa9cec4 to5a68445CompareOctober 7, 2025 15:43
Comment on lines +3 to +4
useSymfony\Component\DependencyInjection\Tests\Fixtures\Bar;
useSymfony\Config\ServicesConfig;
Copy link
Member

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?

Suggested change
useSymfony\Component\DependencyInjection\Tests\Fixtures\Bar;
useSymfony\Config\ServicesConfig;
namespaceSymfony\Config;
useSymfony\Component\DependencyInjection\Tests\Fixtures\Bar;

@alexandre-daubois
Copy link
Member

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.

Capture d’écran 2025-10-08 à 08 50 29

@fabpot
Copy link
Member

Thank you@nicolas-grekas.

@fabpotfabpot merged commitb0d728c intosymfony:7.4Oct 9, 2025
10 of 12 checks passed
@nicolas-grekasnicolas-grekas deleted the di-array-shape branchOctober 9, 2025 13:02
nicolas-grekas added a commit that referenced this pull requestOct 20, 2025
… 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
nicolas-grekas added a commit that referenced this pull requestOct 23, 2025
… 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
This was referencedOct 27, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@GromNaNGromNaNGromNaN approved these changes

@fabpotfabpotfabpot approved these changes

@alexandre-dauboisalexandre-dauboisalexandre-daubois approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

7.4

Development

Successfully merging this pull request may close these issues.

5 participants

@nicolas-grekas@carsonbot@alexandre-daubois@fabpot@GromNaN

[8]ページ先頭

©2009-2025 Movatter.jp