26use Wikimedia\ObjectFactory\ObjectFactory;
43 private $mInstances = [];
47 private $mGroups = [];
51 private $mModules = [];
55 private $objectFactory;
64 $this->mParent = $parentModule;
65 $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
79 foreach ( $modules as $name => $moduleSpec ) {
80 $this->
addModule( $name, $group, $moduleSpec );
103 public function addModule( $name, $group, $spec, $factory =
null ) {
104 if ( !is_string( $name ) ) {
105 throw new InvalidArgumentException(
'$name must be a string' );
108 if ( !is_string( $group ) ) {
109 throw new InvalidArgumentException(
'$group must be a string' );
112 if ( is_string( $spec ) ) {
117 if ( is_callable( $factory ) ) {
118 wfDeprecated( __METHOD__ .
' with $class and $factory',
'1.34' );
119 $spec[
'factory'] = $factory;
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' );
127 $this->mGroups[$group] =
null;
128 $this->mModules[$name] = [ $group, $spec ];
140 public function getModule( $moduleName, $group =
null, $ignoreCache =
false ) {
141 if ( !isset( $this->mModules[$moduleName] ) ) {
145 [ $moduleGroup, $spec ] = $this->mModules[$moduleName];
147 if ( $group !==
null && $moduleGroup !== $group ) {
151 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
153 return $this->mInstances[$moduleName];
156 $instance = $this->instantiateModule( $moduleName, $spec );
158 if ( !$ignoreCache ) {
160 $this->mInstances[$moduleName] = $instance;
176 private function instantiateModule( $name, $spec ) {
177 return $this->objectFactory->createObject(
184 'assertClass' => $spec[
'class']
195 if ( $group ===
null ) {
196 return array_keys( $this->mModules );
199 foreach ( $this->mModules as $name => $groupAndSpec ) {
200 if ( $groupAndSpec[0] === $group ) {
215 foreach ( $this->mModules as $name => $groupAndSpec ) {
216 if ( $group ===
null || $groupAndSpec[0] === $group ) {
217 $result[$name] = $groupAndSpec[1][
'class'];
232 if ( isset( $this->mModules[$module] ) ) {
233 return $this->mModules[$module][1][
'class'];
245 public function isDefined( $moduleName, $group =
null ) {
246 if ( isset( $this->mModules[$moduleName] ) ) {
247 return $group ===
null || $this->mModules[$moduleName][0] === $group;
259 if ( isset( $this->mModules[$moduleName] ) ) {
260 return $this->mModules[$moduleName][0];
271 return array_keys( $this->mGroups );
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.
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 ...