Problem/Motivation
Currently when you use a service collector with tagged services, a method on the "consumer" service will be called one time for each tagged service.
This works fine for the basic case when the tagged service itself is the thing that should e.g. respond to some kind of event.
The same also applies to basic symfony event listeners.
However, sometimes what you want to add to the collector/consumer is not the tagged service itself, but some other custom object or value, which itself might not even be registered in the container.
For this purpose, it would be useful for the tagged service to act as a "configurator" on another service.
class ConfiguredService {
public function addHandler($handler) {..}
public function setColor($color) {..}
}
class ConfiguratorService {
public function configureConsumer(ConfiguredService $service): void {
$service->addHandler(new CustomObject(...));
$service->setColor('blue');
}
}
services:
configured_service:
class: ConfiguredService
configurator_service:
class: ConfiguratorService
tags:
- { name: configurator, method: configureConsumer, target: configured_service }
One limitation with the "configurator" mechanism is that there can be only one configurator per service.
But this could be circumvented by having a multicast configurator service.
Note:
I did not find anything equivalent in symfony, but perhaps I did not look hard enough.