Jeroen De Dauw has uploaded a new change for review.
https://gerrit.wikimedia.org/r/49781
Change subject: Allow registration of API modules using a callback that returns
an instance of ApiBase
......................................................................
Allow registration of API modules using a callback that returns an instance of
ApiBase
Basically implementing what Brion suggested on wikitech
This allows for injecting dependencies while still only loading the actual
class when needed.
Simple example:
$wgAPIModules['wbgetclaims'] = function( ApiMain $mainModule, $moduleName ) use
( $options ) {
$module = new \Wikibase\ApiGetClaims( $mainModule, $moduleName );
$module->setClaimOptions( $options );
return $module;
};
Change-Id: I98e49f410bad417b67c2cbb7e0abe0ea6eae5dcc
---
M RELEASE-NOTES-1.21
M includes/api/ApiModuleManager.php
2 files changed, 19 insertions(+), 3 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/81/49781/1
diff --git a/RELEASE-NOTES-1.21 b/RELEASE-NOTES-1.21
index 7e06218..f91f896 100644
--- a/RELEASE-NOTES-1.21
+++ b/RELEASE-NOTES-1.21
@@ -96,6 +96,8 @@
* (bug 43915) New maintenance script deleteEqualMessages.php.
* New collation uppercase-sv, which is like uppercase, but adapted
to Swedish sort order.
+* API modules can now be registered using a callback that returns an instance
+ of an API module instead of providing a class name.
* WikiText now permits the use of WAI-ARIA's role="presentation" inside of
html elements and tables. This allows presentational markup, especially
tables. To be marked up as such.
diff --git a/includes/api/ApiModuleManager.php
b/includes/api/ApiModuleManager.php
index db1d36d..4c9e310 100644
--- a/includes/api/ApiModuleManager.php
+++ b/includes/api/ApiModuleManager.php
@@ -64,7 +64,7 @@
*
* @param $group string Name of the module group
* @param $name string The identifier for this module.
- * @param $class string The class where this module is implemented.
+ * @param $class string The class where this module is implemented or a
callback returning an instance of ApiBase.
*/
public function addModule( $name, $group, $class ) {
$this->mGroups[$group] = null;
@@ -91,12 +91,26 @@
return $this->mInstances[$moduleName];
} else {
// new instance
- $class = $grpCls[1];
- $instance = new $class ( $this->mParent, $moduleName );
+ $classOrCallback = $grpCls[1];
+
+ $instance = null;
+
+ if ( is_string( $classOrCallback ) ) {
+ $instance = new $classOrCallback(
$this->mParent, $moduleName );
+ }
+ elseif ( is_callable( $classOrCallback ) ) {
+ $instance = call_user_func_array(
$classOrCallback, array( $this->mParent, $moduleName ) );
+ }
+
+ if ( !( $instance instanceof ApiBase ) ) {
+ throw new MWException( "Invalid registration
for the '$moduleName' API module" );
+ }
+
if ( !$ignoreCache ) {
// cache this instance in case it is needed
later
$this->mInstances[$moduleName] = $instance;
}
+
return $instance;
}
}
--
To view, visit https://gerrit.wikimedia.org/r/49781
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I98e49f410bad417b67c2cbb7e0abe0ea6eae5dcc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits