ApiModuleManager.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * Copyright © 2012 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @since 1.21
  22. */
  23. use MediaWiki\MediaWikiServices;
  24. use Wikimedia\ObjectFactory;
  25. /**
  26. * This class holds a list of modules and handles instantiation
  27. *
  28. * @since 1.21
  29. * @ingroup API
  30. */
  31. class ApiModuleManager extends ContextSource {
  32. /**
  33. * @var ApiBase
  34. */
  35. private $mParent;
  36. /**
  37. * @var ApiBase[]
  38. */
  39. private $mInstances = [];
  40. /**
  41. * @var null[]
  42. */
  43. private $mGroups = [];
  44. /**
  45. * @var array[]
  46. */
  47. private $mModules = [];
  48. /**
  49. * @var ObjectFactory
  50. */
  51. private $objectFactory;
  52. /**
  53. * Construct new module manager
  54. *
  55. * @param ApiBase $parentModule Parent module instance will be used during instantiation
  56. * @param ObjectFactory|null $objectFactory Object factory to use when instantiating modules
  57. */
  58. public function __construct( ApiBase $parentModule, ObjectFactory $objectFactory = null ) {
  59. $this->mParent = $parentModule;
  60. $this->objectFactory = $objectFactory ?? MediaWikiServices::getInstance()->getObjectFactory();
  61. }
  62. /**
  63. * Add a list of modules to the manager. Each module is described
  64. * by an ObjectFactory spec.
  65. *
  66. * This simply calls `addModule()` for each module in `$modules`.
  67. *
  68. * @see ApiModuleManager::addModule()
  69. * @param array $modules A map of ModuleName => ModuleSpec
  70. * @param string $group Which group modules belong to (action,format,...)
  71. */
  72. public function addModules( array $modules, $group ) {
  73. foreach ( $modules as $name => $moduleSpec ) {
  74. $this->addModule( $name, $group, $moduleSpec );
  75. }
  76. }
  77. /**
  78. * Add or overwrite a module in this ApiMain instance. Intended for use by extending
  79. * classes who wish to add their own modules to their lexicon or override the
  80. * behavior of inherent ones.
  81. *
  82. * ObjectFactory is used to instantiate the module when needed. The parent module
  83. * (`$parentModule` from `__construct()`) and the `$name` are passed as extraArgs.
  84. *
  85. * @since 1.34, accepts an ObjectFactory spec as the third parameter. The old calling convention,
  86. * passing a class name as parameter #3 and an optional factory callable as parameter #4, is
  87. * deprecated.
  88. * @param string $name The identifier for this module.
  89. * @param string $group Name of the module group
  90. * @param string|array $spec The ObjectFactory spec for instantiating the module,
  91. * or a class name to instantiate.
  92. * @param callable|null $factory Callback for instantiating the module (deprecated).
  93. *
  94. * @throws InvalidArgumentException
  95. */
  96. public function addModule( $name, $group, $spec, $factory = null ) {
  97. if ( !is_string( $name ) ) {
  98. throw new InvalidArgumentException( '$name must be a string' );
  99. }
  100. if ( !is_string( $group ) ) {
  101. throw new InvalidArgumentException( '$group must be a string' );
  102. }
  103. if ( is_string( $spec ) ) {
  104. $spec = [
  105. 'class' => $spec
  106. ];
  107. if ( is_callable( $factory ) ) {
  108. wfDeprecated( __METHOD__ . ' with $class and $factory', '1.34' );
  109. $spec['factory'] = $factory;
  110. }
  111. } elseif ( !is_array( $spec ) ) {
  112. throw new InvalidArgumentException( '$spec must be a string or an array' );
  113. } elseif ( !isset( $spec['class'] ) ) {
  114. throw new InvalidArgumentException( '$spec must define a class name' );
  115. }
  116. $this->mGroups[$group] = null;
  117. $this->mModules[$name] = [ $group, $spec ];
  118. }
  119. /**
  120. * Get module instance by name, or instantiate it if it does not exist
  121. *
  122. * @param string $moduleName Module name
  123. * @param string|null $group Optionally validate that the module is in a specific group
  124. * @param bool $ignoreCache If true, force-creates a new instance and does not cache it
  125. *
  126. * @return ApiBase|null The new module instance, or null if failed
  127. */
  128. public function getModule( $moduleName, $group = null, $ignoreCache = false ) {
  129. if ( !isset( $this->mModules[$moduleName] ) ) {
  130. return null;
  131. }
  132. list( $moduleGroup, $spec ) = $this->mModules[$moduleName];
  133. if ( $group !== null && $moduleGroup !== $group ) {
  134. return null;
  135. }
  136. if ( !$ignoreCache && isset( $this->mInstances[$moduleName] ) ) {
  137. // already exists
  138. return $this->mInstances[$moduleName];
  139. } else {
  140. // new instance
  141. $instance = $this->instantiateModule( $moduleName, $spec );
  142. if ( !$ignoreCache ) {
  143. // cache this instance in case it is needed later
  144. $this->mInstances[$moduleName] = $instance;
  145. }
  146. return $instance;
  147. }
  148. }
  149. /**
  150. * Instantiate the module using the given class or factory function.
  151. *
  152. * @param string $name The identifier for this module.
  153. * @param array $spec The ObjectFactory spec for instantiating the module.
  154. *
  155. * @throws UnexpectedValueException
  156. * @return ApiBase
  157. */
  158. private function instantiateModule( $name, $spec ) {
  159. return $this->objectFactory->createObject(
  160. $spec,
  161. [
  162. 'extraArgs' => [
  163. $this->mParent,
  164. $name
  165. ],
  166. 'assertClass' => $spec['class']
  167. ]
  168. );
  169. }
  170. /**
  171. * Get an array of modules in a specific group or all if no group is set.
  172. * @param string|null $group Optional group filter
  173. * @return array List of module names
  174. */
  175. public function getNames( $group = null ) {
  176. if ( $group === null ) {
  177. return array_keys( $this->mModules );
  178. }
  179. $result = [];
  180. foreach ( $this->mModules as $name => $groupAndSpec ) {
  181. if ( $groupAndSpec[0] === $group ) {
  182. $result[] = $name;
  183. }
  184. }
  185. return $result;
  186. }
  187. /**
  188. * Create an array of (moduleName => moduleClass) for a specific group or for all.
  189. * @param string|null $group Name of the group to get or null for all
  190. * @return array Name=>class map
  191. */
  192. public function getNamesWithClasses( $group = null ) {
  193. $result = [];
  194. foreach ( $this->mModules as $name => $groupAndSpec ) {
  195. if ( $group === null || $groupAndSpec[0] === $group ) {
  196. $result[$name] = $groupAndSpec[1]['class'];
  197. }
  198. }
  199. return $result;
  200. }
  201. /**
  202. * Returns the class name of the given module
  203. *
  204. * @param string $module Module name
  205. * @return string|bool class name or false if the module does not exist
  206. * @since 1.24
  207. */
  208. public function getClassName( $module ) {
  209. if ( isset( $this->mModules[$module] ) ) {
  210. return $this->mModules[$module][1]['class'];
  211. }
  212. return false;
  213. }
  214. /**
  215. * Returns true if the specific module is defined at all or in a specific group.
  216. * @param string $moduleName Module name
  217. * @param string|null $group Group name to check against, or null to check all groups,
  218. * @return bool True if defined
  219. */
  220. public function isDefined( $moduleName, $group = null ) {
  221. if ( isset( $this->mModules[$moduleName] ) ) {
  222. return $group === null || $this->mModules[$moduleName][0] === $group;
  223. }
  224. return false;
  225. }
  226. /**
  227. * Returns the group name for the given module
  228. * @param string $moduleName
  229. * @return string|null Group name or null if missing
  230. */
  231. public function getModuleGroup( $moduleName ) {
  232. if ( isset( $this->mModules[$moduleName] ) ) {
  233. return $this->mModules[$moduleName][0];
  234. }
  235. return null;
  236. }
  237. /**
  238. * Get a list of groups this manager contains.
  239. * @return array
  240. */
  241. public function getGroups() {
  242. return array_keys( $this->mGroups );
  243. }
  244. }