25use Wikimedia\ObjectFactory\ObjectFactory;
42 private $mInstances = [];
46 private $mGroups = [];
50 private $mModules = [];
54 private $objectFactory;
63 $this->mParent = $parentModule;
64 $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
78 foreach (
$modules as $name => $moduleSpec ) {
79 $this->
addModule( $name, $group, $moduleSpec );
102 public function addModule( $name, $group, $spec, $factory =
null ) {
103 if ( !is_string( $name ) ) {
104 throw new InvalidArgumentException(
'$name must be a string' );
107 if ( !is_string( $group ) ) {
108 throw new InvalidArgumentException(
'$group must be a string' );
111 if ( is_string( $spec ) ) {
116 if ( is_callable( $factory ) ) {
117 wfDeprecated( __METHOD__ .
' with $class and $factory',
'1.34' );
118 $spec[
'factory'] = $factory;
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' );
126 $this->mGroups[$group] =
null;
127 $this->mModules[$name] = [ $group, $spec ];
139 public function getModule( $moduleName, $group =
null, $ignoreCache =
false ) {
140 if ( !isset( $this->mModules[$moduleName] ) ) {
144 list( $moduleGroup, $spec ) = $this->mModules[$moduleName];
146 if ( $group !==
null && $moduleGroup !== $group ) {
150 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
152 return $this->mInstances[$moduleName];
155 $instance = $this->instantiateModule( $moduleName, $spec );
157 if ( !$ignoreCache ) {
159 $this->mInstances[$moduleName] = $instance;
175 private function instantiateModule( $name, $spec ) {
176 return $this->objectFactory->createObject(
183 'assertClass' => $spec[
'class']
194 if ( $group ===
null ) {
195 return array_keys( $this->mModules );
198 foreach ( $this->mModules as $name => $groupAndSpec ) {
199 if ( $groupAndSpec[0] === $group ) {
214 foreach ( $this->mModules as $name => $groupAndSpec ) {
215 if ( $group ===
null || $groupAndSpec[0] === $group ) {
216 $result[$name] = $groupAndSpec[1][
'class'];
231 if ( isset( $this->mModules[$module] ) ) {
232 return $this->mModules[$module][1][
'class'];
244 public function isDefined( $moduleName, $group =
null ) {
245 if ( isset( $this->mModules[$moduleName] ) ) {
246 return $group ===
null || $this->mModules[$moduleName][0] === $group;
258 if ( isset( $this->mModules[$moduleName] ) ) {
259 return $this->mModules[$moduleName][0];
270 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 ...