Description
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:
-
It allows provide self-descriptive configurators, simplifies learning DSL of the extension (by playing with autocomplete options).
-
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.
-
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.