25 use Wikimedia\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];
157 if ( !$ignoreCache ) {
159 $this->mInstances[$moduleName] = $instance;
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 );