Merge "EditPage::newSectionSummary should return a value in all code paths"
[lhc/web/wiklou.git] / includes / api / ApiModuleManager.php
index 8226529..fdf3f02 100644 (file)
@@ -59,13 +59,55 @@ class ApiModuleManager extends ContextSource {
        }
 
        /**
-        * Add a list of modules to the manager
-        * @param array $modules A map of ModuleName => ModuleClass
+        * Add a list of modules to the manager. Each module is described
+        * by a module spec.
+        *
+        * Each module spec is an associative array containing at least
+        * the 'class' key for the module's class, and optionally a
+        * 'factory' key for the factory function to use for the module.
+        *
+        * That factory function will be called with two parameters,
+        * the parent module (an instance of ApiBase, usually ApiMain)
+        * and the name the module was registered under. The return
+        * value must be an instance of the class given in the 'class'
+        * field.
+        *
+        * For backward compatibility, the module spec may also be a
+        * simple string containing the module's class name. In that
+        * case, the class' constructor will be called with the parent
+        * module and module name as parameters, as described above.
+        *
+        * Examples for defining module specs:
+        *
+        * @code
+        *  $modules['foo'] = 'ApiFoo';
+        *  $modules['bar'] = array(
+        *      'class' => 'ApiBar',
+        *      'factory' => function( $main, $name ) { ... }
+        *  );
+        *  $modules['xyzzy'] = array(
+        *      'class' => 'ApiXyzzy',
+        *      'factory' => array( 'XyzzyFactory', 'newApiModule' )
+        *  );
+        * @endcode
+        *
+        * @param array $modules A map of ModuleName => ModuleSpec; The ModuleSpec
+        *        is either a string containing the module's class name, or an associative
+        *        array (see above for details).
         * @param string $group Which group modules belong to (action,format,...)
         */
        public function addModules( array $modules, $group ) {
-               foreach ( $modules as $name => $class ) {
-                       $this->addModule( $name, $group, $class );
+
+               foreach ( $modules as $name => $moduleSpec ) {
+                       if ( is_array( $moduleSpec ) ) {
+                               $class = $moduleSpec['class'];
+                               $factory = ( isset( $moduleSpec['factory'] ) ? $moduleSpec['factory'] : null );
+                       } else {
+                               $class = $moduleSpec;
+                               $factory = null;
+                       }
+
+                       $this->addModule( $name, $group, $class, $factory );
                }
        }
 
@@ -74,37 +116,61 @@ class ApiModuleManager extends ContextSource {
         * classes who wish to add their own modules to their lexicon or override the
         * behavior of inherent ones.
         *
-        * @param string $group Name of the module group
         * @param string $name The identifier for this module.
+        * @param string $group Name of the module group
         * @param string $class The class where this module is implemented.
+        * @param callable|null $factory Callback for instantiating the module.
+        *
+        * @throws InvalidArgumentException
         */
-       public function addModule( $name, $group, $class ) {
+       public function addModule( $name, $group, $class, $factory = null ) {
+               if ( !is_string( $name ) ) {
+                       throw new InvalidArgumentException( '$name must be a string' );
+               }
+
+               if ( !is_string( $group ) ) {
+                       throw new InvalidArgumentException( '$group must be a string' );
+               }
+
+               if ( !is_string( $class ) ) {
+                       throw new InvalidArgumentException( '$class must be a string' );
+               }
+
+               if ( $factory !== null && !is_callable( $factory ) ) {
+                       throw new InvalidArgumentException( '$factory must be a callable (or null)' );
+               }
+
                $this->mGroups[$group] = null;
-               $this->mModules[$name] = array( $group, $class );
+               $this->mModules[$name] = array( $group, $class, $factory );
        }
 
        /**
         * Get module instance by name, or instantiate it if it does not exist
+        *
         * @param string $moduleName Module name
         * @param string $group Optionally validate that the module is in a specific group
         * @param bool $ignoreCache If true, force-creates a new instance and does not cache it
-        * @return mixed The new module instance, or null if failed
+        *
+        * @return ApiBase|null The new module instance, or null if failed
         */
        public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
                if ( !isset( $this->mModules[$moduleName] ) ) {
                        return null;
                }
-               $grpCls = $this->mModules[$moduleName];
-               if ( $group !== null && $grpCls[0] !== $group ) {
+
+               list( $moduleGroup, $moduleClass, $moduleFactory ) = $this->mModules[$moduleName];
+
+               if ( $group !== null && $moduleGroup !== $group ) {
                        return null;
                }
+
                if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
                        // already exists
                        return $this->mInstances[$moduleName];
                } else {
                        // new instance
-                       $class = $grpCls[1];
-                       $instance = new $class ( $this->mParent, $moduleName );
+                       $instance = $this->instantiateModule( $moduleName, $moduleClass, $moduleFactory );
+
                        if ( !$ignoreCache ) {
                                // cache this instance in case it is needed later
                                $this->mInstances[$moduleName] = $instance;
@@ -114,6 +180,32 @@ class ApiModuleManager extends ContextSource {
                }
        }
 
+       /**
+        * Instantiate the module using the given class or factory function.
+        *
+        * @param string $name The identifier for this module.
+        * @param string $class The class where this module is implemented.
+        * @param callable|null $factory Callback for instantiating the module.
+        *
+        * @throws MWException
+        * @return ApiBase
+        */
+       private function instantiateModule( $name, $class, $factory = null ) {
+               if ( $factory !== null ) {
+                       // create instance from factory
+                       $instance = call_user_func( $factory, $this->mParent, $name );
+
+                       if ( ! $instance instanceof $class ) {
+                               throw new MWException( "The factory function for module $name did not return an instance of $class!" );
+                       }
+               } else {
+                       // create instance from class name
+                       $instance = new $class( $this->mParent, $name );
+               }
+
+               return $instance;
+       }
+
        /**
         * Get an array of modules in a specific group or all if no group is set.
         * @param string $group Optional group filter