Description
Q | A |
---|---|
Bug report? | no |
Feature request? | no |
BC Break report? | no |
RFC? | yes |
Symfony version | 3.4 |
The Problem
These days I'm updating some legacy Symfony applications to use Flex and modern Symfony practices. The first step was autowiring services. I loved the change. Everything is much better now and the automation (not magic) added by autowiring is fantastic to improve your productivity and code quality.
However, there's a minor thing about autowiring that bugged me: autowiring scalars.
public function __construct(SomeClass $service1, $argument1, $argument2, $argument3)
{
$this->service1 = $service1;
$this->argument1 = $argument1;
$this->argument2 = $argument2;
$this->argument3 = $argument3;
}
I understand that this cannot be solved without adding magic. But in my case, 100% of scalar values are container parameters. And I have literally hundreds of lines of service config like this:
Acme\Blah\Blah\MyClass:
arguments:
$argument1: '%kernel.project_dir%'
$argument2: '%app.container_param%'
$argument3: '%app.another_param%'
It's so boring to do this. And so useless! I'd like to propose another way to solve this.
The Proposal
What if we allow to inject ParameterBag
to inject all container parameters?
The above example would now look like this:
public function __construct(SomeClass $service1, ParameterBag $params)
{
$this->service1 = $service1;
$this->argument1 = $params->get('kernel.project_dir');
$this->argument2 = $params->get('app.container_param');
$this->argument3 = $params->get('app.another_param');
}
And no YAML/XML config would be needed for services like this.
Comments
- What about performance? The number of container params has been reduced drastically in modern Symofny versions (by removing the
*.class
params) and params is an array which is easily cacheable by PHP 7. Impact in memory consumption should be minimal or null? To be confirmed... - What if you have thousands of container params? Don't use this and keep using the existing solution to inject params individually.