Page MenuHomePhabricator

Service Container should implement a mechanism to collect services
Closed, InvalidPublic

Description

Problem
The MediaWiki-libs-Services does not provide any sort of mechanism (that I can find) to collect services of a certain kind. For instance, if I would like to collect all of the services that implement a specific interface, I would have to loop through each service (and let them be instantiated) in order to do that.

Solution
Provide a mechanim to collect services, perhaps similar to Symfony's Service Tags.

Event Timeline

@dbarratt when would such a feature be needed / how would it be used? C.f. XY problem

@dbarratt when would such a feature be needed / how would it be used? C.f. XY problem

I can't remember what I was trying to solve. The hooks kind of do this now anyways.

Generally, I don't think this kind of "collection" should be done by the top level service container. Services that implement a shared interface should use a factory (or "registry"). Such factories/registries do not shared an interface. They could share code internally though. They would typically based on ObjectFactory, rather than service wiring (the distinction between ObjectFactory and wiring is somewhat arbitrary and should go away eventually). We already have this kind of thing, e.g.

MediaWikiServices::getInstance()->getContentHandlerFactory()->getContentHandler( CONTENT_MODEL_CSS );

For collecting services that are related logically, but that do not share an interface, one can simply create another ServiceContainer subclass with getters for these services, and treat it as a service in the top level container. Usage would be something like:

MediaWikiServices::getInstance()->getUserManagementServiceContainer()->getUserPreferenceStore();

So yes, grouping/collecting services is useful. But it can be done naturally by treating service containers as services themselves. No special mechanism is needed.

For "tagging" services that share some quality that related to service management, services can simply implement (empty marker) interfaces.