MediaWiki REL1_39
ApiModuleManager.php
Go to the documentation of this file.
1<?php
25use Wikimedia\ObjectFactory\ObjectFactory;
26
34
38 private $mParent;
42 private $mInstances = [];
46 private $mGroups = [];
50 private $mModules = [];
54 private $objectFactory;
55
62 public function __construct( ApiBase $parentModule, ObjectFactory $objectFactory = null ) {
63 $this->mParent = $parentModule;
64 $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
65 }
66
77 public function addModules( array $modules, $group ) {
78 foreach ( $modules as $name => $moduleSpec ) {
79 $this->addModule( $name, $group, $moduleSpec );
80 }
81 }
82
102 public function addModule( $name, $group, $spec, $factory = null ) {
103 if ( !is_string( $name ) ) {
104 throw new InvalidArgumentException( '$name must be a string' );
105 }
106
107 if ( !is_string( $group ) ) {
108 throw new InvalidArgumentException( '$group must be a string' );
109 }
110
111 if ( is_string( $spec ) ) {
112 $spec = [
113 'class' => $spec
114 ];
115
116 if ( is_callable( $factory ) ) {
117 wfDeprecated( __METHOD__ . ' with $class and $factory', '1.34' );
118 $spec['factory'] = $factory;
119 }
120 } elseif ( !is_array( $spec ) ) {
121 throw new InvalidArgumentException( '$spec must be a string or an array' );
122 } elseif ( !isset( $spec['class'] ) ) {
123 throw new InvalidArgumentException( '$spec must define a class name' );
124 }
125
126 $this->mGroups[$group] = null;
127 $this->mModules[$name] = [ $group, $spec ];
128 }
129
139 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
140 if ( !isset( $this->mModules[$moduleName] ) ) {
141 return null;
142 }
143
144 list( $moduleGroup, $spec ) = $this->mModules[$moduleName];
145
146 if ( $group !== null && $moduleGroup !== $group ) {
147 return null;
148 }
149
150 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
151 // already exists
152 return $this->mInstances[$moduleName];
153 } else {
154 // new instance
155 $instance = $this->instantiateModule( $moduleName, $spec );
156
157 if ( !$ignoreCache ) {
158 // cache this instance in case it is needed later
159 $this->mInstances[$moduleName] = $instance;
160 }
161
162 return $instance;
163 }
164 }
165
175 private function instantiateModule( $name, $spec ) {
176 return $this->objectFactory->createObject(
177 $spec,
178 [
179 'extraArgs' => [
180 $this->mParent,
181 $name
182 ],
183 'assertClass' => $spec['class']
184 ]
185 );
186 }
187
193 public function getNames( $group = null ) {
194 if ( $group === null ) {
195 return array_keys( $this->mModules );
196 }
197 $result = [];
198 foreach ( $this->mModules as $name => $groupAndSpec ) {
199 if ( $groupAndSpec[0] === $group ) {
200 $result[] = $name;
201 }
202 }
203
204 return $result;
205 }
206
212 public function getNamesWithClasses( $group = null ) {
213 $result = [];
214 foreach ( $this->mModules as $name => $groupAndSpec ) {
215 if ( $group === null || $groupAndSpec[0] === $group ) {
216 $result[$name] = $groupAndSpec[1]['class'];
217 }
218 }
219
220 return $result;
221 }
222
230 public function getClassName( $module ) {
231 if ( isset( $this->mModules[$module] ) ) {
232 return $this->mModules[$module][1]['class'];
233 }
234
235 return false;
236 }
237
244 public function isDefined( $moduleName, $group = null ) {
245 if ( isset( $this->mModules[$moduleName] ) ) {
246 return $group === null || $this->mModules[$moduleName][0] === $group;
247 }
248
249 return false;
250 }
251
257 public function getModuleGroup( $moduleName ) {
258 if ( isset( $this->mModules[$moduleName] ) ) {
259 return $this->mModules[$moduleName][0];
260 }
261
262 return null;
263 }
264
269 public function getGroups() {
270 return array_keys( $this->mGroups );
271 }
272}
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:56
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.