Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[RFC] Proposal to simplify the autowiring of container parameters #23718

Copy link
Copy link
Closed
@javiereguiluz

Description

@javiereguiluz
Issue body actions
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DependencyInjectionRFCRFC = Request For Comments (proposals about features that you want to be discussed)RFC = 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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.