ObjectFactory
creates objects from specifications (via $objectFactory->createObject()
or ObjectFactory::getObjectFromSpec()
).
This format is used by certain configuration settings (such as $wgMWLoggerDefaultSpi or $wgSessionProviders ).
A typical specification looks like this:
$spec = [
'class' => 'Message',
'args' => [ 'unexpected', [ 'foo', 123 ] ],
];
which is the specification for new Message( 'unexpected', [ 'foo', 123 ] )
.
Other options include:
$spec = [
'factory' => 'Message::newFallbackSequence',
'args' => [ 'unexpected', 'unexpected2' ],
];
// Message::newFallbackSequence( 'unexpected', 'unexpected2' )
$spec = [
'class' => 'Message',
'args' => [ 'foo', 'bar' ],
'calls' => [ 'inLanguage' => [ 'en' ], 'useDatabase' => [ false ] ],
];
// <translate nowrap><!--T:15--> will call <tvar name=1>$message->inLanguage( 'en' ), $message->useDatabase( false )</tvar> after creating <tvar name=2>$message</tvar></translate>
With Dependency Injection
MediaWiki version: | ≥ 1.34 |
Starting with MediaWiki 1.34createObject
method.
For non-MediaWiki uses, an ObjectFactory instance can be created with a PSR-11 Container interface that will be used to retrieve the services.
$spec = [
'class' => 'MyClass',
'args' => [ 'foo', 'bar' ],
'services' => [ 'Service1', 'Service2' ],
];
// $services = \MediaWiki\MediaWikiServices::getInstance();
// new MyClass( $services->get( 'Service1' ), $services->get( 'Service2' ), 'foo', 'bar' )
External links
This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.