Closed
Description
This is follow up to:
- PR [DI] Reference tagged services in config
- post New in Symfony 3.4: Simpler injection of tagged services
It is the same, just instead of tags, it uses directly types.
Q | A |
---|---|
Bug report? | no |
Feature request? | yes |
BC Break report? | no |
RFC? | yes |
Let's say we have 2 classes:
ResolverCollector
- one servicesSingleResolverInterface
- many services
We want to collect all services of SingleResolverInterface
and pass them to ResolverCollector
(ctor or via method is not relevant).
The similar way Commands
to Console\Application
, but for own class and without tags.
Right now we have to create CollectorCompilerPass
similar to this:
final class CollectorCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $containerBuilder): void
{
$collectorDefinition = $containerBuilder->get(ResolverCollector::class);
foreach ($containerBuilder->getDefinitions() as $name => $definition) {
if (! is_a($definition->getClass(), SingleResolverInterface::class, true)) {
continue;
}
$collectorDefinition->addMethodCall('addSingleResolver', [new Reference($name)]);
}
}
}
Negatives
- bundle must exists
- or must be registered in Kernel with interface
- it's not right in config, kind of hidden configuration
- requires split configuration in PHP
Desired state
- config only
- 1 place to setup to collect services from container x not just current one like
_defaults
andinstanceof
does - not 2 places: config + PHP (Kernel, CompilerPass or Extension)
From Bundle to Config
Since Symfony 3.4/4.0 goes more and more bundle-less and moves to config:
services:
ResolverCollector:
calls:
- ['addSingleResolvers', [!tagged single_resolver]]
This would the best solution then:
services:
ResolverCollector:
calls:
- ['addSingleResolvers', [!typed SingleResolverInterface]]
How to achieve that? Could similar code from [DI] Reference tagged services in config PR work?