MediaWiki master
ApiModuleManager.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Api;
25
26use InvalidArgumentException;
29use UnexpectedValueException;
30use Wikimedia\ObjectFactory\ObjectFactory;
31
39
43 private $mParent;
47 private $mInstances = [];
51 private $mGroups = [];
55 private $mModules = [];
59 private $objectFactory;
60
67 public function __construct( ApiBase $parentModule, ?ObjectFactory $objectFactory = null ) {
68 $this->mParent = $parentModule;
69 $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
70 }
71
82 public function addModules( array $modules, $group ) {
83 foreach ( $modules as $name => $moduleSpec ) {
84 $this->addModule( $name, $group, $moduleSpec );
85 }
86 }
87
105 public function addModule( string $name, string $group, $spec, $factory = null ) {
106 if ( is_string( $spec ) ) {
107 $spec = [
108 'class' => $spec
109 ];
110
111 if ( is_callable( $factory ) ) {
112 wfDeprecated( __METHOD__ . ' with $class and $factory', '1.34' );
113 $spec['factory'] = $factory;
114 }
115 } elseif ( !is_array( $spec ) ) {
116 throw new InvalidArgumentException( '$spec must be a string or an array' );
117 } elseif ( !isset( $spec['class'] ) ) {
118 throw new InvalidArgumentException( '$spec must define a class name' );
119 }
120
121 $this->mGroups[$group] = null;
122 $this->mModules[$name] = [ $group, $spec ];
123 }
124
134 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
135 if ( !isset( $this->mModules[$moduleName] ) ) {
136 return null;
137 }
138
139 [ $moduleGroup, $spec ] = $this->mModules[$moduleName];
140
141 if ( $group !== null && $moduleGroup !== $group ) {
142 return null;
143 }
144
145 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
146 // already exists
147 return $this->mInstances[$moduleName];
148 } else {
149 // new instance
150 $instance = $this->instantiateModule( $moduleName, $spec );
151
152 if ( !$ignoreCache ) {
153 // cache this instance in case it is needed later
154 $this->mInstances[$moduleName] = $instance;
155 }
156
157 return $instance;
158 }
159 }
160
170 private function instantiateModule( $name, $spec ) {
171 return $this->objectFactory->createObject(
172 $spec,
173 [
174 'extraArgs' => [
175 $this->mParent,
176 $name
177 ],
178 'assertClass' => $spec['class']
179 ]
180 );
181 }
182
188 public function getNames( $group = null ) {
189 if ( $group === null ) {
190 return array_keys( $this->mModules );
191 }
192 $result = [];
193 foreach ( $this->mModules as $name => $groupAndSpec ) {
194 if ( $groupAndSpec[0] === $group ) {
195 $result[] = $name;
196 }
197 }
198
199 return $result;
200 }
201
207 public function getNamesWithClasses( $group = null ) {
208 $result = [];
209 foreach ( $this->mModules as $name => $groupAndSpec ) {
210 if ( $group === null || $groupAndSpec[0] === $group ) {
211 $result[$name] = $groupAndSpec[1]['class'];
212 }
213 }
214
215 return $result;
216 }
217
225 public function getClassName( $module ) {
226 if ( isset( $this->mModules[$module] ) ) {
227 return $this->mModules[$module][1]['class'];
228 }
229
230 return false;
231 }
232
239 public function isDefined( $moduleName, $group = null ) {
240 if ( isset( $this->mModules[$moduleName] ) ) {
241 return $group === null || $this->mModules[$moduleName][0] === $group;
242 }
243
244 return false;
245 }
246
252 public function getModuleGroup( $moduleName ) {
253 if ( isset( $this->mModules[$moduleName] ) ) {
254 return $this->mModules[$moduleName][0];
255 }
256
257 return null;
258 }
259
264 public function getGroups() {
265 return array_keys( $this->mGroups );
266 }
267}
268
270class_alias( ApiModuleManager::class, 'ApiModuleManager' );
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:76
This class holds a list of modules and handles instantiation.
isDefined( $moduleName, $group=null)
Returns true if the specific module is defined at all or in a specific group.
__construct(ApiBase $parentModule, ?ObjectFactory $objectFactory=null)
Construct new module manager.
getModule( $moduleName, $group=null, $ignoreCache=false)
Get module instance by name, or instantiate it if it does not exist.
getModuleGroup( $moduleName)
Returns the group name for the given module.
addModules(array $modules, $group)
Add a list of modules to the manager.
getNames( $group=null)
Get an array of modules in a specific group or all if no group is set.
getGroups()
Get a list of groups this manager contains.
addModule(string $name, string $group, $spec, $factory=null)
Add or overwrite a module in this ApiMain instance.
getClassName( $module)
Returns the class name of the given module.
getNamesWithClasses( $group=null)
Create an array of (moduleName => moduleClass) for a specific group or for all.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.