auraphp / Aura.Di

Dependency Injection System
MIT License
349 stars 63 forks source link

initial design of using attributes for injection #209

Closed frederikbosch closed 5 months ago

frederikbosch commented 5 months ago
use Aura\Di\Attribute\Instance;
use Aura\Di\Attribute\Service;
use Aura\Di\Attribute\Value;

class FakeConstructAttributeClass
{
    private FakeInterface $fakeSetter;

    public function __construct(
        #[Service('fake.service')]
        private FakeInterface $fakeService,
        #[Service('fake.service', 'getFoo')]
        private FakeInterface $fakeServiceGet,
        #[Instance(FakeInterfaceClass1::class)]
        private FakeInterface $fakeInstance,
        #[Value('fake.value')]
        private string $string,
    ) {
    }

    public function setFake(
        #[Service('fake.setter')]
        FakeInterface $fakeSetter
    ) {
        $this->fakeSetter = $fakeSetter;
    }
}

Design choices:

Limitations:

I will continue with the implementation if there is no objection towards this direction. I will take at least a week before continuing.