Skip to content

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Aug 21, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Related to #58771.

This PR adds array-shape helpers:

An example for routes.php:

use Symfony\Config\RoutesConfig;

return new RoutesConfig([
    'controllers' => [
        'resource' => 'attributes',
        'type' => 'tagged_services',
    ],
]);

And one for services.php:

use App\Bar;
use Symfony\Config\ServicesConfig;

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

The ServicesConfig and RoutesConfig 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 behind when@%env%:

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

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 🙏

@carsonbot

This comment was marked as off-topic.

@nicolas-grekas nicolas-grekas force-pushed the di-array-shape branch 2 times, most recently from 0c19d7b to 811122e Compare August 21, 2025 14:36
@carsonbot carsonbot closed this Aug 21, 2025
@nicolas-grekas nicolas-grekas added this to the 7.4 milestone Sep 16, 2025
@nicolas-grekas nicolas-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 arrays Sep 30, 2025
@nicolas-grekas nicolas-grekas marked this pull request as ready for review September 30, 2025 14:28
* file that was distributed with this source code.
*/

namespace Symfony\Config;
Copy link
Member Author

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.

/**
* Creates a parameter.
*/
function param(string $name): ParamConfigurator
Copy link
Member Author

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

@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 like ServicesConfig. 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 in AppServicesConfig. When I tried to use it in my config file located in config/packages/my_config.php, I have the error The 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-grekas nicolas-grekas force-pushed the di-array-shape branch 5 times, most recently from 1912e56 to 469ea85 Compare October 7, 2025 14:20

final protected function loadExtensionConfig(string $namespace, array $config, string $file = '?'): void
{
if (\in_array($namespace, ['imports', 'services', 'parameters'], true)) {
Copy link
Member Author

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
Member Author

nicolas-grekas commented Oct 7, 2025

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

@nicolas-grekas nicolas-grekas force-pushed the di-array-shape branch 2 times, most recently from aa9cec4 to 5a68445 Compare October 7, 2025 15:43
Comment on lines +3 to +4
use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar;
use Symfony\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 the Symfony\Config namespace?

Suggested change
use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar;
use Symfony\Config\ServicesConfig;
namespace Symfony\Config;
use Symfony\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

fabpot commented Oct 9, 2025

Thank you @nicolas-grekas.

@fabpot fabpot merged commit b0d728c into symfony:7.4 Oct 9, 2025
10 of 12 checks passed
@nicolas-grekas nicolas-grekas deleted the di-array-shape branch October 9, 2025 13:02
nicolas-grekas added a commit that referenced this pull request Oct 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       | 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
nicolas-grekas added a commit that referenced this pull request Oct 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       | 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
This was referenced Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants