MediaWiki master
ApiModuleManager.php
Go to the documentation of this file.
1<?php
26use Wikimedia\ObjectFactory\ObjectFactory;
27
35
39 private $mParent;
43 private $mInstances = [];
47 private $mGroups = [];
51 private $mModules = [];
55 private $objectFactory;
56
63 public function __construct( ApiBase $parentModule, ObjectFactory $objectFactory = null ) {
64 $this->mParent = $parentModule;
65 $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
66 }
67
78 public function addModules( array $modules, $group ) {
79 foreach ( $modules as $name => $moduleSpec ) {
80 $this->addModule( $name, $group, $moduleSpec );
81 }
82 }
83
103 public function addModule( $name, $group, $spec, $factory = null ) {
104 if ( !is_string( $name ) ) {
105 throw new InvalidArgumentException( '$name must be a string' );
106 }
107
108 if ( !is_string( $group ) ) {
109 throw new InvalidArgumentException( '$group must be a string' );
110 }
111
112 if ( is_string( $spec ) ) {
113 $spec = [
114 'class' => $spec
115 ];
116
117 if ( is_callable( $factory ) ) {
118 wfDeprecated( __METHOD__ . ' with $class and $factory', '1.34' );
119 $spec['factory'] = $factory;
120 }
121 } elseif ( !is_array( $spec ) ) {
122 throw new InvalidArgumentException( '$spec must be a string or an array' );
123 } elseif ( !isset( $spec['class'] ) ) {
124 throw new InvalidArgumentException( '$spec must define a class name' );
125 }
126
127 $this->mGroups[$group] = null;
128 $this->mModules[$name] = [ $group, $spec ];
129 }
130
140 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
141 if ( !isset( $this->mModules[$moduleName] ) ) {
142 return null;
143 }
144
145 [ $moduleGroup, $spec ] = $this->mModules[$moduleName];
146
147 if ( $group !== null && $moduleGroup !== $group ) {
148 return null;
149 }
150
151 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
152 // already exists
153 return $this->mInstances[$moduleName];
154 } else {
155 // new instance
156 $instance = $this->instantiateModule( $moduleName, $spec );
157
158 if ( !$ignoreCache ) {
159 // cache this instance in case it is needed later
160 $this->mInstances[$moduleName] = $instance;
161 }
162
163 return $instance;
164 }
165 }
166
176 private function instantiateModule( $name, $spec ) {
177 return $this->objectFactory->createObject(
178 $spec,
179 [
180 'extraArgs' => [
181 $this->mParent,
182 $name
183 ],
184 'assertClass' => $spec['class']
185 ]
186 );
187 }
188
194 public function getNames( $group = null ) {
195 if ( $group === null ) {
196 return array_keys( $this->mModules );
197 }
198 $result = [];
199 foreach ( $this->mModules as $name => $groupAndSpec ) {
200 if ( $groupAndSpec[0] === $group ) {
201 $result[] = $name;
202 }
203 }
204
205 return $result;
206 }
207
213 public function getNamesWithClasses( $group = null ) {
214 $result = [];
215 foreach ( $this->mModules as $name => $groupAndSpec ) {
216 if ( $group === null || $groupAndSpec[0] === $group ) {
217 $result[$name] = $groupAndSpec[1]['class'];
218 }
219 }
220
221 return $result;
222 }
223
231 public function getClassName( $module ) {
232 if ( isset( $this->mModules[$module] ) ) {
233 return $this->mModules[$module][1]['class'];
234 }
235
236 return false;
237 }
238
245 public function isDefined( $moduleName, $group = null ) {
246 if ( isset( $this->mModules[$moduleName] ) ) {
247 return $group === null || $this->mModules[$moduleName][0] === $group;
248 }
249
250 return false;
251 }
252
258 public function getModuleGroup( $moduleName ) {
259 if ( isset( $this->mModules[$moduleName] ) ) {
260 return $this->mModules[$moduleName][0];
261 }
262
263 return null;
264 }
265
270 public function getGroups() {
271 return array_keys( $this->mGroups );
272 }
273}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:64
This class holds a list of modules and handles instantiation.
getModule( $moduleName, $group=null, $ignoreCache=false)
Get module instance by name, or instantiate it if it does not exist.
getGroups()
Get a list of groups this manager contains.
getNamesWithClasses( $group=null)
Create an array of (moduleName => moduleClass) for a specific group or for all.
__construct(ApiBase $parentModule, ObjectFactory $objectFactory=null)
Construct new module manager.
getClassName( $module)
Returns the class name of the given module.
getModuleGroup( $moduleName)
Returns the group name for the given module.
addModules(array $modules, $group)
Add a list of modules to the manager.
addModule( $name, $group, $spec, $factory=null)
Add or overwrite a module in this ApiMain instance.
getNames( $group=null)
Get an array of modules in a specific group or all if no group is set.
isDefined( $moduleName, $group=null)
Returns true if the specific module is defined at all or in a specific group.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Service locator for MediaWiki core services.