MediaWiki master
ApiModuleManager.php
Go to the documentation of this file.
1<?php
10namespace MediaWiki\Api;
11
12use InvalidArgumentException;
15use UnexpectedValueException;
16use Wikimedia\ObjectFactory\ObjectFactory;
17
25
29 private $mInstances = [];
33 private $mGroups = [];
37 private $mModules = [];
41 private $objectFactory;
42
49 public function __construct(
50 private readonly ApiBase $parentModule,
51 ?ObjectFactory $objectFactory = null,
52 ) {
53 $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
54 }
55
66 public function addModules( array $modules, $group ) {
67 foreach ( $modules as $name => $moduleSpec ) {
68 $this->addModule( $name, $group, $moduleSpec );
69 }
70 }
71
89 public function addModule( string $name, string $group, $spec, $factory = null ) {
90 if ( is_string( $spec ) ) {
91 $spec = [
92 'class' => $spec
93 ];
94
95 if ( is_callable( $factory ) ) {
96 wfDeprecated( __METHOD__ . ' with $class and $factory', '1.34' );
97 $spec['factory'] = $factory;
98 }
99 } elseif ( !is_array( $spec ) ) {
100 throw new InvalidArgumentException( '$spec must be a string or an array' );
101 } elseif ( !isset( $spec['class'] ) ) {
102 throw new InvalidArgumentException( '$spec must define a class name' );
103 }
104
105 $this->mGroups[$group] = null;
106 $this->mModules[$name] = [ $group, $spec ];
107 }
108
118 public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
119 if ( !isset( $this->mModules[$moduleName] ) ) {
120 return null;
121 }
122
123 [ $moduleGroup, $spec ] = $this->mModules[$moduleName];
124
125 if ( $group !== null && $moduleGroup !== $group ) {
126 return null;
127 }
128
129 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
130 // already exists
131 return $this->mInstances[$moduleName];
132 } else {
133 // new instance
134 $instance = $this->instantiateModule( $moduleName, $spec );
135
136 if ( !$ignoreCache ) {
137 // cache this instance in case it is needed later
138 $this->mInstances[$moduleName] = $instance;
139 }
140
141 return $instance;
142 }
143 }
144
154 private function instantiateModule( $name, $spec ) {
155 return $this->objectFactory->createObject(
156 $spec,
157 [
158 'extraArgs' => [
159 $this->parentModule,
160 $name
161 ],
162 'assertClass' => $spec['class']
163 ]
164 );
165 }
166
172 public function getNames( $group = null ) {
173 if ( $group === null ) {
174 return array_keys( $this->mModules );
175 }
176 $result = [];
177 foreach ( $this->mModules as $name => $groupAndSpec ) {
178 if ( $groupAndSpec[0] === $group ) {
179 $result[] = $name;
180 }
181 }
182
183 return $result;
184 }
185
191 public function getNamesWithClasses( $group = null ) {
192 $result = [];
193 foreach ( $this->mModules as $name => $groupAndSpec ) {
194 if ( $group === null || $groupAndSpec[0] === $group ) {
195 $result[$name] = $groupAndSpec[1]['class'];
196 }
197 }
198
199 return $result;
200 }
201
209 public function getClassName( $module ) {
210 if ( isset( $this->mModules[$module] ) ) {
211 return $this->mModules[$module][1]['class'];
212 }
213
214 return false;
215 }
216
223 public function isDefined( $moduleName, $group = null ) {
224 if ( isset( $this->mModules[$moduleName] ) ) {
225 return $group === null || $this->mModules[$moduleName][0] === $group;
226 }
227
228 return false;
229 }
230
236 public function getModuleGroup( $moduleName ) {
237 if ( isset( $this->mModules[$moduleName] ) ) {
238 return $this->mModules[$moduleName][0];
239 }
240
241 return null;
242 }
243
248 public function getGroups() {
249 return array_keys( $this->mGroups );
250 }
251}
252
254class_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:60
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.
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.
__construct(private readonly ApiBase $parentModule, ?ObjectFactory $objectFactory=null,)
Construct new module 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.