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
See <https://www.mediawiki.org/wiki/OOUI/Themes> for details.
* (T229035) The GetUserBlock hook was added. Use this instead of
GetBlockedStatus.
+* ObjectFactory is available as a service. When used as a service, the object
+ specs can now specify needed DI services.
=== External library changes in 1.34 ===
use TitleFormatter;
use TitleParser;
use VirtualRESTServiceClient;
+use Wikimedia\ObjectFactory;
use Wikimedia\Rdbms\LBFactory;
use Wikimedia\Services\SalvageableService;
use Wikimedia\Services\ServiceContainer;
return $this->getService( 'NameTableStoreFactory' );
}
+ /**
+ * ObjectFactory is intended for instantiating "handlers" from declarative definitions,
+ * such as Action API modules, special pages, or REST API handlers.
+ *
+ * @since 1.34
+ * @return ObjectFactory
+ */
+ public function getObjectFactory() {
+ return $this->getService( 'ObjectFactory' );
+ }
+
/**
* @since 1.32
* @return OldRevisionImporter
use MediaWiki\Storage\NameTableStoreFactory;
use MediaWiki\Storage\SqlBlobStore;
use MediaWiki\Storage\PageEditStash;
+use Wikimedia\ObjectFactory;
return [
'ActorMigration' => function ( MediaWikiServices $services ) : ActorMigration {
);
},
+ 'ObjectFactory' => function ( MediaWikiServices $services ) : ObjectFactory {
+ return new ObjectFactory( $services );
+ },
+
'OldRevisionImporter' => function ( MediaWikiServices $services ) : OldRevisionImporter {
return new ImportableOldRevisionImporter(
true,