Skip to content

Navigation Menu

Sign in
Appearance settings

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

[DependencyInjection] Service defaults by trait #24327

Copy link
Copy link
Closed
@soullivaneuh

Description

@soullivaneuh
Issue body actions
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version 3.4

Description

It would be great to define default service definition by trait usage.

This could be a great alternative for some auto-wiring setter like parameters, instead of manual definition.

Concrete case

Here is my KernelRootDirTrait:

trait KernelRootDirTrait
{
    /**
     * @var string
     */
    private $kernelRootDir;

    /**
     * @return string
     */
    public function getKernelRootDir(): string
    {
        return $this->kernelRootDir;
    }

    /**
     * @param string $kernelRootDir
     */
    public function setKernelRootDir(string $kernelRootDir)
    {
        $this->kernelRootDir = $kernelRootDir;
    }
}

And the service:

class MyService
{
    use KernelRootDirTrait;
}

If we could define the default by trait usage like this:

services:
    _trait:
        AppBundle\PropertyTrait\KernelRootDirTrait:
            calls:
                - [setKernelRootDir, ['%kernel.root_dir%']]

With that, the MyService service will automatically have the %kernerl.root_dir% parameter. :-)

The interest of trait is the re-usability of them. This will remove lot of manual parameter definition lines.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    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.