New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DependencyInjection] Add new Autoconfigure attributes #48162
base: 6.3
Are you sure you want to change the base?
[DependencyInjection] Add new Autoconfigure attributes #48162
Conversation
@nicolas-grekas, would you check this PR? If a general idea is correct and acceptable, I will create tests. |
I'm not sure this is useful at this stage:
We might just need to allow WDYT? |
With <?php
declare(strict_types=1);
abstract class AbstractFooService
{
public function __construct(private ProcessorInterface $processor)
{
}
public function process(): void
{
$this->processor->process();
}
}
final class FooService extends AbstractFooService
{
}
final class BarService extends AbstractFooService
{
}
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->defaults()
->autowire()
->autoconfigure();
$services->set(FooService::class)
->arg('$processor', 'processor.foo');
$services->set(BarService::class)
->arg('$processor', 'processor.bar');
}; |
Thanks for the example, that gives a much better idea of the motivation for this proposal! What about this instead? #[Autowire(bind: '$processor', service: 'processor.foo')]
final class FooService extends AbstractFooService
{
}
#[Autowire(bind: '$processor', service: 'processor.bar')]
final class BarService extends AbstractFooService
{
} The |
Looks awesome. Do you mind allowing The final result will be like this: #[Autowire(bind: '$processor', service: 'processor.foo')]
#[Autowire(call: 'setProcessor', service: 'processor.foo')]
#[Autowire(property: 'processor', service: 'processor.foo')]
#[TaggedIterator(bind: '$iterator', 'tag.name')]
#[TaggedIterator(call: 'setIterator', 'tag.name')]
#[TaggedIterator(property: 'iterator', 'tag.name')]
#[TaggedLocator(bind: '$locator', 'tag.name')]
#[TaggedLocator(call: 'setLocator', 'tag.name')]
#[TaggedLocator(property: 'locator', 'tag.name')]|
final class FooService
{
#[Autowire(service: 'processor.foo')]
public FooProcessor $processor2;
#[TaggedIterator('tag.name')]
public iterable $iterator2;
#[TaggedLocator('tag.name')]
public ServiceProviderInterface $locator2;
public function __construct(
#[Autowire(service: 'processor.foo')]
private FooProcessor $processor,
#[TaggedIterator('tag.name')]
private iterable $iterator,
#[TaggedLocator('tag.name')]
private ServiceProviderInterface $locator,
) {
}
#[Autowire(service: 'processor.foo')]
public function setProcessor(FooProcessor $processor): void
{
...
}
#[TaggedIterator('tag.name')]
public function setIterator(iterable $iterator): void
{
...
}
#[TaggedLocator('tag.name')]
public function setLocator(ServiceProviderInterface $locator): void
{
...
}
} |
@nicolas-grekas, would you please check the proposal above? If ok, I will close this PR and create three smaller PRs. |
Thanks for the update. I'm not in favor of the proliferation of attributes. I'm therefor looking to add as little as possible. As I mentioned, I think we can improve all this by adding one attribute, which we might name just Its signature would be the following:
Note that WDYT? |
Love this idea! |
New Autoconfigure attributes for more convenient configuration of services.