Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiModuleManager.php
blob42dfb719bbf84f4a3c1b0cf91d7e3847a68347e5
1 <?php
2 /**
5 * Created on Dec 27, 2012
7 * Copyright © 2012 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
25 * @since 1.21
28 /**
29 * This class holds a list of modules and handles instantiation
31 * @since 1.21
32 * @ingroup API
34 class ApiModuleManager extends ContextSource {
36 /**
37 * @var ApiBase
39 private $mParent;
40 /**
41 * @var ApiBase[]
43 private $mInstances = [];
44 /**
45 * @var null[]
47 private $mGroups = [];
48 /**
49 * @var array[]
51 private $mModules = [];
53 /**
54 * Construct new module manager
55 * @param ApiBase $parentModule Parent module instance will be used during instantiation
57 public function __construct( ApiBase $parentModule ) {
58 $this->mParent = $parentModule;
61 /**
62 * Add a list of modules to the manager. Each module is described
63 * by a module spec.
65 * Each module spec is an associative array containing at least
66 * the 'class' key for the module's class, and optionally a
67 * 'factory' key for the factory function to use for the module.
69 * That factory function will be called with two parameters,
70 * the parent module (an instance of ApiBase, usually ApiMain)
71 * and the name the module was registered under. The return
72 * value must be an instance of the class given in the 'class'
73 * field.
75 * For backward compatibility, the module spec may also be a
76 * simple string containing the module's class name. In that
77 * case, the class' constructor will be called with the parent
78 * module and module name as parameters, as described above.
80 * Examples for defining module specs:
82 * @code
83 * $modules['foo'] = 'ApiFoo';
84 * $modules['bar'] = [
85 * 'class' => 'ApiBar',
86 * 'factory' => function( $main, $name ) { ... }
87 * ];
88 * $modules['xyzzy'] = [
89 * 'class' => 'ApiXyzzy',
90 * 'factory' => [ 'XyzzyFactory', 'newApiModule' ]
91 * ];
92 * @endcode
94 * @param array $modules A map of ModuleName => ModuleSpec; The ModuleSpec
95 * is either a string containing the module's class name, or an associative
96 * array (see above for details).
97 * @param string $group Which group modules belong to (action,format,...)
99 public function addModules( array $modules, $group ) {
101 foreach ( $modules as $name => $moduleSpec ) {
102 if ( is_array( $moduleSpec ) ) {
103 $class = $moduleSpec['class'];
104 $factory = ( isset( $moduleSpec['factory'] ) ? $moduleSpec['factory'] : null );
105 } else {
106 $class = $moduleSpec;
107 $factory = null;
110 $this->addModule( $name, $group, $class, $factory );
115 * Add or overwrite a module in this ApiMain instance. Intended for use by extending
116 * classes who wish to add their own modules to their lexicon or override the
117 * behavior of inherent ones.
119 * @param string $name The identifier for this module.
120 * @param string $group Name of the module group
121 * @param string $class The class where this module is implemented.
122 * @param callable|null $factory Callback for instantiating the module.
124 * @throws InvalidArgumentException
126 public function addModule( $name, $group, $class, $factory = null ) {
127 if ( !is_string( $name ) ) {
128 throw new InvalidArgumentException( '$name must be a string' );
131 if ( !is_string( $group ) ) {
132 throw new InvalidArgumentException( '$group must be a string' );
135 if ( !is_string( $class ) ) {
136 throw new InvalidArgumentException( '$class must be a string' );
139 if ( $factory !== null && !is_callable( $factory ) ) {
140 throw new InvalidArgumentException( '$factory must be a callable (or null)' );
143 $this->mGroups[$group] = null;
144 $this->mModules[$name] = [ $group, $class, $factory ];
148 * Get module instance by name, or instantiate it if it does not exist
150 * @param string $moduleName Module name
151 * @param string $group Optionally validate that the module is in a specific group
152 * @param bool $ignoreCache If true, force-creates a new instance and does not cache it
154 * @return ApiBase|null The new module instance, or null if failed
156 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
157 if ( !isset( $this->mModules[$moduleName] ) ) {
158 return null;
161 list( $moduleGroup, $moduleClass, $moduleFactory ) = $this->mModules[$moduleName];
163 if ( $group !== null && $moduleGroup !== $group ) {
164 return null;
167 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
168 // already exists
169 return $this->mInstances[$moduleName];
170 } else {
171 // new instance
172 $instance = $this->instantiateModule( $moduleName, $moduleClass, $moduleFactory );
174 if ( !$ignoreCache ) {
175 // cache this instance in case it is needed later
176 $this->mInstances[$moduleName] = $instance;
179 return $instance;
184 * Instantiate the module using the given class or factory function.
186 * @param string $name The identifier for this module.
187 * @param string $class The class where this module is implemented.
188 * @param callable|null $factory Callback for instantiating the module.
190 * @throws MWException
191 * @return ApiBase
193 private function instantiateModule( $name, $class, $factory = null ) {
194 if ( $factory !== null ) {
195 // create instance from factory
196 $instance = call_user_func( $factory, $this->mParent, $name );
198 if ( !$instance instanceof $class ) {
199 throw new MWException(
200 "The factory function for module $name did not return an instance of $class!"
203 } else {
204 // create instance from class name
205 $instance = new $class( $this->mParent, $name );
208 return $instance;
212 * Get an array of modules in a specific group or all if no group is set.
213 * @param string $group Optional group filter
214 * @return array List of module names
216 public function getNames( $group = null ) {
217 if ( $group === null ) {
218 return array_keys( $this->mModules );
220 $result = [];
221 foreach ( $this->mModules as $name => $grpCls ) {
222 if ( $grpCls[0] === $group ) {
223 $result[] = $name;
227 return $result;
231 * Create an array of (moduleName => moduleClass) for a specific group or for all.
232 * @param string $group Name of the group to get or null for all
233 * @return array Name=>class map
235 public function getNamesWithClasses( $group = null ) {
236 $result = [];
237 foreach ( $this->mModules as $name => $grpCls ) {
238 if ( $group === null || $grpCls[0] === $group ) {
239 $result[$name] = $grpCls[1];
243 return $result;
247 * Returns the class name of the given module
249 * @param string $module Module name
250 * @return string|bool class name or false if the module does not exist
251 * @since 1.24
253 public function getClassName( $module ) {
254 if ( isset( $this->mModules[$module] ) ) {
255 return $this->mModules[$module][1];
258 return false;
262 * Returns true if the specific module is defined at all or in a specific group.
263 * @param string $moduleName Module name
264 * @param string $group Group name to check against, or null to check all groups,
265 * @return bool True if defined
267 public function isDefined( $moduleName, $group = null ) {
268 if ( isset( $this->mModules[$moduleName] ) ) {
269 return $group === null || $this->mModules[$moduleName][0] === $group;
272 return false;
276 * Returns the group name for the given module
277 * @param string $moduleName
278 * @return string|null Group name or null if missing
280 public function getModuleGroup( $moduleName ) {
281 if ( isset( $this->mModules[$moduleName] ) ) {
282 return $this->mModules[$moduleName][0];
285 return null;
289 * Get a list of groups this manager contains.
290 * @return array
292 public function getGroups() {
293 return array_keys( $this->mGroups );