Add ObjectFactory as a service
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 21 Aug 2019 16:10:30 +0000 (12:10 -0400)
committerDaniel Kinzler <dkinzler@wikimedia.org>
Fri, 23 Aug 2019 08:46:48 +0000 (08:46 +0000)
commita11ad5f0b644760172ebbd7dbc110303929eff0e
treed915f65b92e1a29e18170ff30430deae57839938
parent5019accfe102ed81e4740203c5337df27cf9d9f0
Add ObjectFactory as a service

ObjectFactory exists to handle lazy instantiation of objects based on
declarative specifications.

One drawback to the current use of static ObjectFactory::getObjectFromSpec()
is that it doesn't have a good way to handle dependency injection. The
best you could do is refer to a static 'factory' function in your
specification, and that function would access the global service
container. But DI would prefer not to have that "global service
container" being accessed.

The solution is to have a non-static ObjectFactory that can supply
the needed services based on the object definition. For example,

 [
     'class' => MyObject::class,
     'services' => [ 'FooFactory', 'BarFactory' ],
 ]

would get the FooFactory and BarFactory from a service container to pass
into MyObject's constructor.

But in order for that to be possible, you need to have an instance of
ObjectFactory that has reference to the service container from which to
fetch the services. And the most DI-correct way to get that is to have
it dependency-injected into your constructor, at which point the
ObjectFactory instance should itself be a service in the DI service
container.

Bug: T222409
Change-Id: I2a56059a9209abce0b22fb93c9f9c6a09a825c56
RELEASE-NOTES-1.34
includes/MediaWikiServices.php
includes/ServiceWiring.php