Add ApiModuleManager::getClassName()
authorKunal Mehta <legoktm@gmail.com>
Fri, 15 Aug 2014 22:45:16 +0000 (15:45 -0700)
committerKunal Mehta <legoktm@gmail.com>
Mon, 8 Sep 2014 09:06:33 +0000 (02:06 -0700)
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

includes/api/ApiModuleManager.php
tests/phpunit/includes/api/ApiModuleManagerTest.php

index f7d0ccf..a0300ab 100644 (file)
@@ -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
index 201eed1..994d245 100644 (file)
@@ -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' ) );
+       }
+
+
 }