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:
All attributes define lazy injections
Attributes will have the lowest precedence, e.g. ->params[FakeConstructAttributeClass::class]['fakeService'] = ... will overwrite the attribute.
Limitations:
You cannot pass params to #[Instance] attributes. If you want to overwrite constructor parameters of an injected instance, you would have to change to an #[Service] attribute or reside to inject by code.
You cannot pass params to #[Service('service', 'method')] attributes. You would have to reside to the method using code, or define an additional service and then use #[Service('new.service)].
Design choices:
->params[FakeConstructAttributeClass::class]['fakeService'] = ...
will overwrite the attribute.Limitations:
params
to#[Instance]
attributes. If you want to overwrite constructor parameters of an injected instance, you would have to change to an#[Service]
attribute or reside to inject by code.params
to#[Service('service', 'method')]
attributes. You would have to reside to the method using code, or define an additional service and then use#[Service('new.service)]
.I will continue with the implementation if there is no objection towards this direction. I will take at least a week before continuing.