Skip to content

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Sep 29, 2025

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

Another step on the path explored in #58771

This allows returning PHP arrays or config-builders from config files.

Several styles are supported, some examples below.

What this does not support is config trees under the import, parameters or services namespaces (that's for another PR).

Arrays:

return [
    'framework' => [...],
];
return static function () {
    yield 'framework' => [...];
];

Config builders (best DX together with #61879):

return new FrameworkConfig([...]);
return static function () {
    return new FrameworkConfig([...]);
];
// $env is always available in config files
if ('dev' === $env) {
    return new FrameworkConfig([...]);
}
return static function (string $env) {
    if ('dev' === $env) {
        yield new FrameworkConfig([...]);
    }
};

@nicolas-grekas nicolas-grekas merged commit d95fb96 into symfony:7.4 Sep 29, 2025
11 of 12 checks passed
@nicolas-grekas nicolas-grekas deleted the config-struct branch September 29, 2025 15:18
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.

3 participants