8000 [RFC] [DependencyInjection] Custom container configurators (fluent PHP format) · Issue #25630 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
[RFC] [DependencyInjection] Custom container configurators (fluent PHP format) #25630
Closed
@unkind

Description

@unkind
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 4.1 (?)

So far we have extensions for custom and simplified configurations, e.g.

return function (ContainerConfigurator $c) {
    $c->extension('monolog', [
        'handlers' => [
            'main' => [
                'type'  => 'rotating_file',
                'path'  => '%kernel.logs_dir%/%kernel.environment%.log',
                'level' => 'debug',
                // max number of log files to keep
                // defaults to zero, which means infinite files
                'max_files' => 10,
            ],
        ],
    ]);
};

which is fine in many cases, but it doesn't use all capabilities of PHP. I'd be happy to have native custom configurator like the following, for example:

return function (MonologContainerConfigurator $c) {
    $c->handlers()->register(
            'main',
            MonologHandlerType::rotatingFile()
                ->path('%kernel.logs_dir%/%kernel.environment%.log')
                ->maxFiles(10)
        )
        ->level(LoggerInterface::DEBUG);
};

I see at least 3 benefits:

  1. It allows provide self-descriptive configurators, simplifies learning DSL of the extension (by playing with autocomplete options).

  2. Such configurators prevent many typos because we work with methods, not custom arrays. So far only XML has self-validation, but this format is painful for many people.

  3. It helps to introduce your own DSL in your project (e.g. I can add ->anonymous() helper like I suggested in [DependencyInjection] Anonymous services in PHP DSL #24632). @nicolas-grekas mentioned it as possible solution for me:

    It should be quite easy to define your own DSL, by creating a different set of configurator classes, and type-hinting for them in the closure.

    Unfortunately, Symfony doesn't support it out-of-box.

The idea is simple: bundle/extension should be able to register it's own container configurator factory:

$container->registerFluentContainerConfigurator(
    MonologContainerConfigurator::class,
    function (ContainerConfigurator $c) {
        return new MonologContainerConfigurator($c);
    }
);

Then we can detect closure argument type and inject appropriate container configurator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependencyInjectionFeatureRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0