ModuleHandlerInterface.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace Drupal\Core\Extension;
  3. /**
  4. * Interface for classes that manage a set of enabled modules.
  5. *
  6. * Classes implementing this interface work with a fixed list of modules and are
  7. * responsible for loading module files and maintaining information about module
  8. * dependencies and hook implementations.
  9. */
  10. interface ModuleHandlerInterface {
  11. /**
  12. * Includes a module's .module file.
  13. *
  14. * This prevents including a module more than once.
  15. *
  16. * @param string $name
  17. * The name of the module to load.
  18. *
  19. * @return bool
  20. * TRUE if the item is loaded or has already been loaded.
  21. */
  22. public function load($name);
  23. /**
  24. * Loads all enabled modules.
  25. */
  26. public function loadAll();
  27. /**
  28. * Returns whether all modules have been loaded.
  29. *
  30. * @return bool
  31. * A Boolean indicating whether all modules have been loaded. This means all
  32. * modules; the load status of bootstrap modules cannot be checked.
  33. */
  34. public function isLoaded();
  35. /**
  36. * Reloads all enabled modules.
  37. */
  38. public function reload();
  39. /**
  40. * Returns the list of currently active modules.
  41. *
  42. * @return \Drupal\Core\Extension\Extension[]
  43. * An associative array whose keys are the names of the modules and whose
  44. * values are Extension objects.
  45. */
  46. public function getModuleList();
  47. /**
  48. * Returns a module extension object from the currently active modules list.
  49. *
  50. * @param string $name
  51. * The name of the module to return.
  52. *
  53. * @return \Drupal\Core\Extension\Extension
  54. * An extension object.
  55. *
  56. * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
  57. * Thrown when the requested module does not exist.
  58. */
  59. public function getModule($name);
  60. /**
  61. * Sets an explicit list of currently active modules.
  62. *
  63. * @param \Drupal\Core\Extension\Extension[] $module_list
  64. * An associative array whose keys are the names of the modules and whose
  65. * values are Extension objects.
  66. */
  67. public function setModuleList(array $module_list = []);
  68. /**
  69. * Adds a module to the list of currently active modules.
  70. *
  71. * @param string $name
  72. * The module name; e.g., 'node'.
  73. * @param string $path
  74. * The module path; e.g., 'core/modules/node'.
  75. */
  76. public function addModule($name, $path);
  77. /**
  78. * Adds an installation profile to the list of currently active modules.
  79. *
  80. * @param string $name
  81. * The profile name; e.g., 'standard'.
  82. * @param string $path
  83. * The profile path; e.g., 'core/profiles/standard'.
  84. */
  85. public function addProfile($name, $path);
  86. /**
  87. * Determines which modules require and are required by each module.
  88. *
  89. * @param array $modules
  90. * An array of module objects keyed by module name. Each object contains
  91. * information discovered during a Drupal\Core\Extension\ExtensionDiscovery
  92. * scan.
  93. *
  94. * @return
  95. * The same array with the new keys for each module:
  96. * - requires: An array with the keys being the modules that this module
  97. * requires.
  98. * - required_by: An array with the keys being the modules that will not work
  99. * without this module.
  100. *
  101. * @see \Drupal\Core\Extension\ExtensionDiscovery
  102. */
  103. public function buildModuleDependencies(array $modules);
  104. /**
  105. * Determines whether a given module is enabled.
  106. *
  107. * @param string $module
  108. * The name of the module (without the .module extension).
  109. *
  110. * @return bool
  111. * TRUE if the module is both installed and enabled.
  112. */
  113. public function moduleExists($module);
  114. /**
  115. * Loads an include file for each enabled module.
  116. *
  117. * @param string $type
  118. * The include file's type (file extension).
  119. * @param string $name
  120. * (optional) The base file name (without the $type extension). If omitted,
  121. * each module's name is used; i.e., "$module.$type" by default.
  122. */
  123. public function loadAllIncludes($type, $name = NULL);
  124. /**
  125. * Loads a module include file.
  126. *
  127. * Examples:
  128. * @code
  129. * // Load node.admin.inc from the node module.
  130. * $this->loadInclude('node', 'inc', 'node.admin');
  131. * // Load content_types.inc from the node module.
  132. * $this->loadInclude('node', 'inc', 'content_types');
  133. * @endcode
  134. *
  135. * @param string $module
  136. * The module to which the include file belongs.
  137. * @param string $type
  138. * The include file's type (file extension).
  139. * @param string $name
  140. * (optional) The base file name (without the $type extension). If omitted,
  141. * $module is used; i.e., resulting in "$module.$type" by default.
  142. *
  143. * @return string|false
  144. * The name of the included file, if successful; FALSE otherwise.
  145. */
  146. public function loadInclude($module, $type, $name = NULL);
  147. /**
  148. * Retrieves a list of hooks that are declared through hook_hook_info().
  149. *
  150. * @return array
  151. * An associative array whose keys are hook names and whose values are an
  152. * associative array containing a group name. The structure of the array
  153. * is the same as the return value of hook_hook_info().
  154. *
  155. * @see hook_hook_info()
  156. */
  157. public function getHookInfo();
  158. /**
  159. * Determines which modules are implementing a hook.
  160. *
  161. * @param string $hook
  162. * The name of the hook (e.g. "help" or "menu").
  163. *
  164. * @return array
  165. * An array with the names of the modules which are implementing this hook.
  166. */
  167. public function getImplementations($hook);
  168. /**
  169. * Write the hook implementation info to the cache.
  170. */
  171. public function writeCache();
  172. /**
  173. * Resets the cached list of hook implementations.
  174. */
  175. public function resetImplementations();
  176. /**
  177. * Returns whether a given module implements a given hook.
  178. *
  179. * @param string $module
  180. * The name of the module (without the .module extension).
  181. * @param string $hook
  182. * The name of the hook (e.g. "help" or "menu").
  183. *
  184. * @return bool
  185. * TRUE if the module is both installed and enabled, and the hook is
  186. * implemented in that module.
  187. */
  188. public function implementsHook($module, $hook);
  189. /**
  190. * Invokes a hook in a particular module.
  191. *
  192. * @param string $module
  193. * The name of the module (without the .module extension).
  194. * @param string $hook
  195. * The name of the hook to invoke.
  196. * @param array $args
  197. * Arguments to pass to the hook implementation.
  198. *
  199. * @return mixed
  200. * The return value of the hook implementation.
  201. */
  202. public function invoke($module, $hook, array $args = []);
  203. /**
  204. * Invokes a hook in all enabled modules that implement it.
  205. *
  206. * @param string $hook
  207. * The name of the hook to invoke.
  208. * @param array $args
  209. * Arguments to pass to the hook.
  210. *
  211. * @return array
  212. * An array of return values of the hook implementations. If modules return
  213. * arrays from their implementations, those are merged into one array
  214. * recursively. Note: integer keys in arrays will be lost, as the merge is
  215. * done using Drupal\Component\Utility\NestedArray::mergeDeepArray().
  216. */
  217. public function invokeAll($hook, array $args = []);
  218. /**
  219. * Invokes a deprecated hook in a particular module.
  220. *
  221. * Invoking a deprecated hook adds the behavior of triggering an
  222. * E_USER_DEPRECATED error if any implementations are found.
  223. *
  224. * API maintainers should use this method instead of invoke() when their hook
  225. * is deprecated. This method does not detect when a hook is deprecated.
  226. *
  227. * @param string $description
  228. * Helpful text describing what to do instead of implementing this hook.
  229. * @param string $module
  230. * The name of the module (without the .module extension).
  231. * @param string $hook
  232. * The name of the hook to invoke.
  233. * @param array $args
  234. * Arguments to pass to the hook implementation.
  235. *
  236. * @return mixed
  237. * The return value of the hook implementation.
  238. *
  239. * @see \Drupal\Core\Extension\ModuleHandlerInterface::invoke()
  240. * @see https://www.drupal.org/core/deprecation#how-hook
  241. */
  242. public function invokeDeprecated($description, $module, $hook, array $args = []);
  243. /**
  244. * Invokes a deprecated hook in all enabled modules that implement it.
  245. *
  246. * Invoking a deprecated hook adds the behavior of triggering an
  247. * E_USER_DEPRECATED error if any implementations are found.
  248. *
  249. * API maintainers should use this method instead of invokeAll() when their
  250. * hook is deprecated. This method does not detect when a hook is deprecated.
  251. *
  252. * @param string $description
  253. * Helpful text describing what to do instead of implementing this hook.
  254. * @param string $hook
  255. * The name of the hook to invoke.
  256. * @param array $args
  257. * Arguments to pass to the hook.
  258. *
  259. * @return array
  260. * An array of return values of the hook implementations. If modules return
  261. * arrays from their implementations, those are merged into one array
  262. * recursively. Note: integer keys in arrays will be lost, as the merge is
  263. * done using Drupal\Component\Utility\NestedArray::mergeDeepArray().
  264. *
  265. * @see \Drupal\Core\Extension\ModuleHandlerInterface::invokeAll()
  266. * @see https://www.drupal.org/core/deprecation#how-hook
  267. */
  268. public function invokeAllDeprecated($description, $hook, array $args = []);
  269. /**
  270. * Passes alterable variables to specific hook_TYPE_alter() implementations.
  271. *
  272. * This dispatch function hands off the passed-in variables to type-specific
  273. * hook_TYPE_alter() implementations in modules. It ensures a consistent
  274. * interface for all altering operations.
  275. *
  276. * A maximum of 2 alterable arguments is supported. In case more arguments need
  277. * to be passed and alterable, modules provide additional variables assigned by
  278. * reference in the last $context argument:
  279. * @code
  280. * $context = array(
  281. * 'alterable' => &$alterable,
  282. * 'unalterable' => $unalterable,
  283. * 'foo' => 'bar',
  284. * );
  285. * $this->alter('mymodule_data', $alterable1, $alterable2, $context);
  286. * @endcode
  287. *
  288. * Note that objects are always passed by reference. If it is absolutely
  289. * required that no implementation alters a passed object in $context, then an
  290. * object needs to be cloned:
  291. * @code
  292. * $context = array(
  293. * 'unalterable_object' => clone $object,
  294. * );
  295. * $this->alter('mymodule_data', $data, $context);
  296. * @endcode
  297. *
  298. * @param string|array $type
  299. * A string describing the type of the alterable $data. 'form', 'links',
  300. * 'node_content', and so on are several examples. Alternatively can be an
  301. * array, in which case hook_TYPE_alter() is invoked for each value in the
  302. * array, ordered first by module, and then for each module, in the order of
  303. * values in $type. For example, when Form API is using $this->alter() to
  304. * execute both hook_form_alter() and hook_form_FORM_ID_alter()
  305. * implementations, it passes array('form', 'form_' . $form_id) for $type.
  306. * @param mixed $data
  307. * The variable that will be passed to hook_TYPE_alter() implementations to be
  308. * altered. The type of this variable depends on the value of the $type
  309. * argument. For example, when altering a 'form', $data will be a structured
  310. * array. When altering a 'profile', $data will be an object.
  311. * @param mixed $context1
  312. * (optional) An additional variable that is passed by reference.
  313. * @param mixed $context2
  314. * (optional) An additional variable that is passed by reference. If more
  315. * context needs to be provided to implementations, then this should be an
  316. * associative array as described above.
  317. */
  318. public function alter($type, &$data, &$context1 = NULL, &$context2 = NULL);
  319. /**
  320. * Passes alterable variables to deprecated hook_TYPE_alter() implementations.
  321. *
  322. * This method triggers an E_USER_DEPRECATED error if any implementations of
  323. * the alter hook are found. It is otherwise identical to alter().
  324. *
  325. * See the documentation for alter() for more details.
  326. *
  327. * @param string $description
  328. * Helpful text describing what to do instead of implementing this alter
  329. * hook.
  330. * @param string|array $type
  331. * A string describing the type of the alterable $data. 'form', 'links',
  332. * 'node_content', and so on are several examples. Alternatively can be an
  333. * array, in which case hook_TYPE_alter() is invoked for each value in the
  334. * array, ordered first by module, and then for each module, in the order of
  335. * values in $type. For example, when Form API is using $this->alter() to
  336. * execute both hook_form_alter() and hook_form_FORM_ID_alter()
  337. * implementations, it passes array('form', 'form_' . $form_id) for $type.
  338. * @param mixed $data
  339. * The variable that will be passed to hook_TYPE_alter() implementations to be
  340. * altered. The type of this variable depends on the value of the $type
  341. * argument. For example, when altering a 'form', $data will be a structured
  342. * array. When altering a 'profile', $data will be an object.
  343. * @param mixed $context1
  344. * (optional) An additional variable that is passed by reference.
  345. * @param mixed $context2
  346. * (optional) An additional variable that is passed by reference. If more
  347. * context needs to be provided to implementations, then this should be an
  348. * associative array as described above.
  349. *
  350. * @see \Drupal\Core\Extension\ModuleHandlerInterface::alter()
  351. * @see https://www.drupal.org/core/deprecation#how-hook
  352. */
  353. public function alterDeprecated($description, $type, &$data, &$context1 = NULL, &$context2 = NULL);
  354. /**
  355. * Returns an array of directories for all enabled modules. Useful for
  356. * tasks such as finding a file that exists in all module directories.
  357. *
  358. * @return array
  359. */
  360. public function getModuleDirectories();
  361. /**
  362. * Gets the human readable name of a given module.
  363. *
  364. * @param string $module
  365. * The machine name of the module which title should be shown.
  366. *
  367. * @return string
  368. * Returns the human readable name of the module or the machine name passed
  369. * in if no matching module is found.
  370. */
  371. public function getName($module);
  372. }