This is intended for extensions that want control
over the instantiation of their jobs,
to allow for proper dependency injection.
+* $wgResourceModules may now specify callback functions as an alternative
+ to plain class names, using the 'factory' key in the module description
+ array. This allows dependency injection to be used for ResourceLoader modules.
=== New features in 1.30 ===
* (T37247) Output from Parser::parse() will now be wrapped in a div with
}
},
{
- "description": "An arbitrary ResourceLoaderModule definition",
+ "description": "An arbitrary ResourceLoaderModule definition by class",
"properties": {
"class": {
"type": "string",
}
},
"required": ["class"]
+ },
+ {
+ "description": "An arbitrary ResourceLoaderModule definition with instantiator",
+ "properties": {
+ "factory": {
+ "type": "string",
+ "description": "A static instantiator function for creating the ResourceLoaderModule object."
+ }
+ },
+ "required": ["factory"]
}
]
}
if ( isset( $info['object'] ) ) {
// Object given in info array
$object = $info['object'];
+ } elseif ( isset( $info['factory'] ) ) {
+ $object = call_user_func( $info['factory'], $info );
+ $object->setConfig( $this->getConfig() );
+ $object->setLogger( $this->logger );
} else {
if ( !isset( $info['class'] ) ) {
$class = 'ResourceLoaderFileModule';
);
}
+ /**
+ * @covers ResourceLoader::getModule
+ */
+ public function testGetModuleFactory() {
+ $factory = function( array $info ) {
+ $this->assertArrayHasKey( 'kitten', $info );
+ return new ResourceLoaderTestModule( $info );
+ };
+
+ $rl = new EmptyResourceLoader();
+ $rl->register( 'test', [ 'factory' => $factory, 'kitten' => 'little ball of fur' ] );
+ $this->assertInstanceOf(
+ ResourceLoaderTestModule::class,
+ $rl->getModule( 'test' )
+ );
+ }
+
/**
* @covers ResourceLoader::getModule
*/