From: Kunal Mehta Date: Fri, 15 Aug 2014 22:45:16 +0000 (-0700) Subject: Add ApiModuleManager::getClassName() X-Git-Tag: 1.31.0-rc.0~14121 X-Git-Url: http://git.cyclocoop.org/%24href?a=commitdiff_plain;h=ad9bb085a69b6b3a726677ab688d3a2c8b599120;p=lhc%2Fweb%2Fwiklou.git Add ApiModuleManager::getClassName() In cases where an extension has multiple API modules that have the same exact factory requirements, it would let them use the same factory function and check with the module manager what the class name to construct should be. This is already possible using getNamesWithClasses, but this makes it much more straightforward. Change-Id: I889e3e6f15907896a8df376913125d903debe984 --- diff --git a/includes/api/ApiModuleManager.php b/includes/api/ApiModuleManager.php index f7d0ccf554..a0300ab55d 100644 --- a/includes/api/ApiModuleManager.php +++ b/includes/api/ApiModuleManager.php @@ -241,6 +241,21 @@ class ApiModuleManager extends ContextSource { return $result; } + /** + * Returns the class name of the given module + * + * @param string $module Module name + * @return string|bool class name or false if the module does not exist + * @since 1.24 + */ + public function getClassName( $module ) { + if ( isset( $this->mModules[$module] ) ) { + return $this->mModules[$module][1]; + } + + return false; + } + /** * Returns true if the specific module is defined at all or in a specific group. * @param string $moduleName Module name diff --git a/tests/phpunit/includes/api/ApiModuleManagerTest.php b/tests/phpunit/includes/api/ApiModuleManagerTest.php index 201eed188c..994d2458de 100644 --- a/tests/phpunit/includes/api/ApiModuleManagerTest.php +++ b/tests/phpunit/includes/api/ApiModuleManagerTest.php @@ -273,4 +273,30 @@ class ApiModuleManagerTest extends MediaWikiTestCase { $this->assertArrayEquals( array( 'foo', 'bar' ), $groups ); } + /** + * @covers ApiModuleManager::getClassName + */ + public function testGetClassName() { + $fooModules = array( + 'login' => 'ApiLogin', + 'logout' => 'ApiLogout', + ); + + $barModules = array( + 'feedcontributions' => array( 'class' => 'ApiFeedContributions' ), + 'feedrecentchanges' => array( 'class' => 'ApiFeedRecentChanges' ), + ); + + $moduleManager = $this->getModuleManager(); + $moduleManager->addModules( $fooModules, 'foo' ); + $moduleManager->addModules( $barModules, 'bar' ); + + $this->assertEquals( 'ApiLogin', $moduleManager->getClassName( 'login' ) ); + $this->assertEquals( 'ApiLogout', $moduleManager->getClassName( 'logout' ) ); + $this->assertEquals( 'ApiFeedContributions', $moduleManager->getClassName( 'feedcontributions' ) ); + $this->assertEquals( 'ApiFeedRecentChanges', $moduleManager->getClassName( 'feedrecentchanges' ) ); + $this->assertFalse( $moduleManager->getClassName( 'nonexistentmodule' ) ); + } + + }