Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / api / ApiModuleManager.php
1 <?php
2 /**
3 *
4 *
5 * Created on Dec 27, 2012
6 *
7 * Copyright © 2012 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @since 1.21
26 */
27
28 /**
29 * This class holds a list of modules and handles instantiation
30 *
31 * @since 1.21
32 * @ingroup API
33 */
34 class ApiModuleManager extends ContextSource {
35
36 /**
37 * @var ApiBase
38 */
39 private $mParent;
40 /**
41 * @var ApiBase[]
42 */
43 private $mInstances = [];
44 /**
45 * @var null[]
46 */
47 private $mGroups = [];
48 /**
49 * @var array[]
50 */
51 private $mModules = [];
52
53 /**
54 * Construct new module manager
55 * @param ApiBase $parentModule Parent module instance will be used during instantiation
56 */
57 public function __construct( ApiBase $parentModule ) {
58 $this->mParent = $parentModule;
59 }
60
61 /**
62 * Add a list of modules to the manager. Each module is described
63 * by a module spec.
64 *
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.
68 *
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.
74 *
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.
79 *
80 * Examples for defining module specs:
81 *
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
93 *
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,...)
98 */
99 public function addModules( array $modules, $group ) {
100 foreach ( $modules as $name => $moduleSpec ) {
101 if ( is_array( $moduleSpec ) ) {
102 $class = $moduleSpec['class'];
103 $factory = ( isset( $moduleSpec['factory'] ) ? $moduleSpec['factory'] : null );
104 } else {
105 $class = $moduleSpec;
106 $factory = null;
107 }
108
109 $this->addModule( $name, $group, $class, $factory );
110 }
111 }
112
113 /**
114 * Add or overwrite a module in this ApiMain instance. Intended for use by extending
115 * classes who wish to add their own modules to their lexicon or override the
116 * behavior of inherent ones.
117 *
118 * @param string $name The identifier for this module.
119 * @param string $group Name of the module group
120 * @param string $class The class where this module is implemented.
121 * @param callable|null $factory Callback for instantiating the module.
122 *
123 * @throws InvalidArgumentException
124 */
125 public function addModule( $name, $group, $class, $factory = null ) {
126 if ( !is_string( $name ) ) {
127 throw new InvalidArgumentException( '$name must be a string' );
128 }
129
130 if ( !is_string( $group ) ) {
131 throw new InvalidArgumentException( '$group must be a string' );
132 }
133
134 if ( !is_string( $class ) ) {
135 throw new InvalidArgumentException( '$class must be a string' );
136 }
137
138 if ( $factory !== null && !is_callable( $factory ) ) {
139 throw new InvalidArgumentException( '$factory must be a callable (or null)' );
140 }
141
142 $this->mGroups[$group] = null;
143 $this->mModules[$name] = [ $group, $class, $factory ];
144 }
145
146 /**
147 * Get module instance by name, or instantiate it if it does not exist
148 *
149 * @param string $moduleName Module name
150 * @param string $group Optionally validate that the module is in a specific group
151 * @param bool $ignoreCache If true, force-creates a new instance and does not cache it
152 *
153 * @return ApiBase|null The new module instance, or null if failed
154 */
155 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
156 if ( !isset( $this->mModules[$moduleName] ) ) {
157 return null;
158 }
159
160 list( $moduleGroup, $moduleClass, $moduleFactory ) = $this->mModules[$moduleName];
161
162 if ( $group !== null && $moduleGroup !== $group ) {
163 return null;
164 }
165
166 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
167 // already exists
168 return $this->mInstances[$moduleName];
169 } else {
170 // new instance
171 $instance = $this->instantiateModule( $moduleName, $moduleClass, $moduleFactory );
172
173 if ( !$ignoreCache ) {
174 // cache this instance in case it is needed later
175 $this->mInstances[$moduleName] = $instance;
176 }
177
178 return $instance;
179 }
180 }
181
182 /**
183 * Instantiate the module using the given class or factory function.
184 *
185 * @param string $name The identifier for this module.
186 * @param string $class The class where this module is implemented.
187 * @param callable|null $factory Callback for instantiating the module.
188 *
189 * @throws MWException
190 * @return ApiBase
191 */
192 private function instantiateModule( $name, $class, $factory = null ) {
193 if ( $factory !== null ) {
194 // create instance from factory
195 $instance = call_user_func( $factory, $this->mParent, $name );
196
197 if ( !$instance instanceof $class ) {
198 throw new MWException(
199 "The factory function for module $name did not return an instance of $class!"
200 );
201 }
202 } else {
203 // create instance from class name
204 $instance = new $class( $this->mParent, $name );
205 }
206
207 return $instance;
208 }
209
210 /**
211 * Get an array of modules in a specific group or all if no group is set.
212 * @param string $group Optional group filter
213 * @return array List of module names
214 */
215 public function getNames( $group = null ) {
216 if ( $group === null ) {
217 return array_keys( $this->mModules );
218 }
219 $result = [];
220 foreach ( $this->mModules as $name => $grpCls ) {
221 if ( $grpCls[0] === $group ) {
222 $result[] = $name;
223 }
224 }
225
226 return $result;
227 }
228
229 /**
230 * Create an array of (moduleName => moduleClass) for a specific group or for all.
231 * @param string $group Name of the group to get or null for all
232 * @return array Name=>class map
233 */
234 public function getNamesWithClasses( $group = null ) {
235 $result = [];
236 foreach ( $this->mModules as $name => $grpCls ) {
237 if ( $group === null || $grpCls[0] === $group ) {
238 $result[$name] = $grpCls[1];
239 }
240 }
241
242 return $result;
243 }
244
245 /**
246 * Returns the class name of the given module
247 *
248 * @param string $module Module name
249 * @return string|bool class name or false if the module does not exist
250 * @since 1.24
251 */
252 public function getClassName( $module ) {
253 if ( isset( $this->mModules[$module] ) ) {
254 return $this->mModules[$module][1];
255 }
256
257 return false;
258 }
259
260 /**
261 * Returns true if the specific module is defined at all or in a specific group.
262 * @param string $moduleName Module name
263 * @param string $group Group name to check against, or null to check all groups,
264 * @return bool True if defined
265 */
266 public function isDefined( $moduleName, $group = null ) {
267 if ( isset( $this->mModules[$moduleName] ) ) {
268 return $group === null || $this->mModules[$moduleName][0] === $group;
269 }
270
271 return false;
272 }
273
274 /**
275 * Returns the group name for the given module
276 * @param string $moduleName
277 * @return string|null Group name or null if missing
278 */
279 public function getModuleGroup( $moduleName ) {
280 if ( isset( $this->mModules[$moduleName] ) ) {
281 return $this->mModules[$moduleName][0];
282 }
283
284 return null;
285 }
286
287 /**
288 * Get a list of groups this manager contains.
289 * @return array
290 */
291 public function getGroups() {
292 return array_keys( $this->mGroups );
293 }
294 }