Skip to content

Conversation

@nicolas-grekas
Copy link
Member

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

Another PR on the path explored in #58771

This PR allows returning plain arrays to configure services and routes, using the exact same shape as yaml files.

eg for routes.php:

return [
    'a' => ['path' => '/a'],
    'when@dev' => [
        'x' => ['path' => '/x'],
    ],
];

and for services.php:

return [
    'parameters' => [
        'foo' => 'bar',
    ],
    'services' => [
        '_defaults' => [
            'public' => true,
        ],
        Bar::class => null,
        'my_service' => [
            'class' => Bar::class,
            'arguments' => ['%foo%'],
        ],
    ],
];

The final step will be to add support for explicit array shapes that static analyzers can understand. PR on its way.

…sing PHP arrays that follow the same shape as corresponding yaml files
@nicolas-grekas nicolas-grekas merged commit 9731ad4 into symfony:7.4 Sep 30, 2025
9 of 12 checks passed
@fabpot fabpot deleted the di-array-config branch October 1, 2025 06:18
fabpot added a commit that referenced this pull request Oct 9, 2025
…help writing PHP configs using yaml-like arrays (nicolas-grekas)

This PR was merged into the 7.4 branch.

Discussion
----------

[DependencyInjection][Routing] Define array-shapes to help writing PHP configs using yaml-like arrays

| 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`:
```php
use Symfony\Config\RoutesConfig;

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

And one for `services.php`:
```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%`:
```php
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` 🙏

Commits
-------

38306ed [DependencyInjection][Routing] Define array-shapes to help writing PHP configs using yaml-like arrays
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.

2 participants