MediaWiki  1.34.0
ApiModuleManager.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\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 }
ApiModuleManager\$mGroups
null[] $mGroups
Definition: ApiModuleManager.php:46
ApiModuleManager\getModuleGroup
getModuleGroup( $moduleName)
Returns the group name for the given module.
Definition: ApiModuleManager.php:257
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
ApiModuleManager\$mModules
array[] $mModules
Definition: ApiModuleManager.php:50
ApiModuleManager\$mInstances
ApiBase[] $mInstances
Definition: ApiModuleManager.php:42
ApiModuleManager\getClassName
getClassName( $module)
Returns the class name of the given module.
Definition: ApiModuleManager.php:230
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
ContextSource
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
Definition: ContextSource.php:29
$modules
$modules
Definition: HTMLFormElement.php:13
ApiModuleManager\$objectFactory
ObjectFactory $objectFactory
Definition: ApiModuleManager.php:54
ApiModuleManager\__construct
__construct(ApiBase $parentModule, ObjectFactory $objectFactory=null)
Construct new module manager.
Definition: ApiModuleManager.php:62
ApiModuleManager\getNames
getNames( $group=null)
Get an array of modules in a specific group or all if no group is set.
Definition: ApiModuleManager.php:193
ApiModuleManager
This class holds a list of modules and handles instantiation.
Definition: ApiModuleManager.php:33
ApiModuleManager\addModule
addModule( $name, $group, $spec, $factory=null)
Add or overwrite a module in this ApiMain instance.
Definition: ApiModuleManager.php:102
ApiModuleManager\getNamesWithClasses
getNamesWithClasses( $group=null)
Create an array of (moduleName => moduleClass) for a specific group or for all.
Definition: ApiModuleManager.php:212
ApiModuleManager\isDefined
isDefined( $moduleName, $group=null)
Returns true if the specific module is defined at all or in a specific group.
Definition: ApiModuleManager.php:244
ApiModuleManager\addModules
addModules(array $modules, $group)
Add a list of modules to the manager.
Definition: ApiModuleManager.php:77
ApiModuleManager\instantiateModule
instantiateModule( $name, $spec)
Instantiate the module using the given class or factory function.
Definition: ApiModuleManager.php:175
ApiModuleManager\getModule
getModule( $moduleName, $group=null, $ignoreCache=false)
Get module instance by name, or instantiate it if it does not exist.
Definition: ApiModuleManager.php:139
ApiModuleManager\getGroups
getGroups()
Get a list of groups this manager contains.
Definition: ApiModuleManager.php:269
ApiModuleManager\$mParent
ApiBase $mParent
Definition: ApiModuleManager.php:38