26use InvalidArgumentException;
29use UnexpectedValueException;
30use Wikimedia\ObjectFactory\ObjectFactory;
47 private $mInstances = [];
51 private $mGroups = [];
55 private $mModules = [];
59 private $objectFactory;
68 $this->mParent = $parentModule;
83 foreach ( $modules as $name => $moduleSpec ) {
84 $this->
addModule( $name, $group, $moduleSpec );
105 public function addModule(
string $name,
string $group, $spec, $factory =
null ) {
106 if ( is_string( $spec ) ) {
111 if ( is_callable( $factory ) ) {
112 wfDeprecated( __METHOD__ .
' with $class and $factory',
'1.34' );
113 $spec[
'factory'] = $factory;
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' );
121 $this->mGroups[$group] =
null;
122 $this->mModules[$name] = [ $group, $spec ];
134 public function getModule( $moduleName, $group =
null, $ignoreCache =
false ) {
135 if ( !isset( $this->mModules[$moduleName] ) ) {
139 [ $moduleGroup, $spec ] = $this->mModules[$moduleName];
141 if ( $group !==
null && $moduleGroup !== $group ) {
145 if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
147 return $this->mInstances[$moduleName];
150 $instance = $this->instantiateModule( $moduleName, $spec );
152 if ( !$ignoreCache ) {
154 $this->mInstances[$moduleName] = $instance;
170 private function instantiateModule( $name, $spec ) {
171 return $this->objectFactory->createObject(
178 'assertClass' => $spec[
'class']
189 if ( $group ===
null ) {
190 return array_keys( $this->mModules );
193 foreach ( $this->mModules as $name => $groupAndSpec ) {
194 if ( $groupAndSpec[0] === $group ) {
209 foreach ( $this->mModules as $name => $groupAndSpec ) {
210 if ( $group ===
null || $groupAndSpec[0] === $group ) {
211 $result[$name] = $groupAndSpec[1][
'class'];
226 if ( isset( $this->mModules[$module] ) ) {
227 return $this->mModules[$module][1][
'class'];
239 public function isDefined( $moduleName, $group =
null ) {
240 if ( isset( $this->mModules[$moduleName] ) ) {
241 return $group ===
null || $this->mModules[$moduleName][0] === $group;
253 if ( isset( $this->mModules[$moduleName] ) ) {
254 return $this->mModules[$moduleName][0];
265 return array_keys( $this->mGroups );
270class_alias( ApiModuleManager::class,
'ApiModuleManager' );
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...