Skip to content

Add attribute support for service injection #1984

Closed
@Haehnchen

Description

@Haehnchen
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class MyService
{
    public function __construct(
        #[Autowire(service: 'some_service')]
        private $service1,

        #[Autowire(expression: 'service("App\\Mail\\MailerConfiguration").getMailerMethod()')]
        private $service2,

        #[Autowire('%env(json:file:resolve:AUTH_FILE)%')]
        private $parameter1,

        #[Autowire('%kernel.project_dir%/config/dir')]
        private $parameter2,
    ) {}

    // ...
}
// src/Handler/HandlerCollection.php
namespace App\Handler;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

class HandlerCollection
{
    private $handlers;

    public function __construct(
        #[TaggedIterator('app.handler')] iterable $handlers
    ) {
        $this->handlers = $handlers;
    }
}
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator;

class HandlerCollection
{
    private $handlers;

    public function __construct(
        #[TaggedLocator('app.handler')] ContainerInterface $handlers
    ) {
        $this->handlers = $handlers;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions