module.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @file
  4. * API for loading and interacting with Drupal modules.
  5. */
  6. use Drupal\Core\Extension\ExtensionDiscovery;
  7. /**
  8. * Builds a list of installed themes.
  9. *
  10. * @param $type
  11. * The type of list to return:
  12. * - theme: All installed themes.
  13. *
  14. * @return
  15. * An associative array of themes, keyed by name.
  16. * For $type 'theme', the array values are objects representing the
  17. * respective database row, with the 'info' property already unserialized.
  18. *
  19. * @see \Drupal\Core\Extension\ThemeHandler::listInfo()
  20. */
  21. function system_list($type) {
  22. $lists = &drupal_static(__FUNCTION__);
  23. if ($cached = \Drupal::cache('bootstrap')->get('system_list')) {
  24. $lists = $cached->data;
  25. }
  26. else {
  27. $lists = [
  28. 'theme' => [],
  29. 'filepaths' => [],
  30. ];
  31. // ThemeHandler maintains the 'system.theme.data' state record.
  32. $theme_data = \Drupal::state()->get('system.theme.data', []);
  33. foreach ($theme_data as $name => $theme) {
  34. $lists['theme'][$name] = $theme;
  35. $lists['filepaths'][] = [
  36. 'type' => 'theme',
  37. 'name' => $name,
  38. 'filepath' => $theme->getPathname(),
  39. ];
  40. }
  41. \Drupal::cache('bootstrap')->set('system_list', $lists);
  42. }
  43. // To avoid a separate database lookup for the filepath, prime the
  44. // drupal_get_filename() static cache with all enabled themes.
  45. foreach ($lists['filepaths'] as $item) {
  46. system_register($item['type'], $item['name'], $item['filepath']);
  47. }
  48. return $lists[$type];
  49. }
  50. /**
  51. * Resets all system_list() caches.
  52. */
  53. function system_list_reset() {
  54. drupal_static_reset('system_list');
  55. drupal_static_reset('system_rebuild_module_data');
  56. \Drupal::cache('bootstrap')->delete('system_list');
  57. }
  58. /**
  59. * Registers an extension in runtime registries for execution.
  60. *
  61. * @param string $type
  62. * The extension type; e.g., 'module' or 'theme'.
  63. * @param string $name
  64. * The internal name of the extension; e.g., 'node'.
  65. * @param string $uri
  66. * The relative URI of the primary extension file; e.g.,
  67. * 'core/modules/node/node.module'.
  68. */
  69. function system_register($type, $name, $uri) {
  70. drupal_get_filename($type, $name, $uri);
  71. drupal_classloader_register($name, dirname($uri));
  72. }
  73. /**
  74. * Loads a module's installation hooks.
  75. *
  76. * @param $module
  77. * The name of the module (without the .module extension).
  78. *
  79. * @return
  80. * The name of the module's install file, if successful; FALSE otherwise.
  81. */
  82. function module_load_install($module) {
  83. // Make sure the installation API is available
  84. include_once __DIR__ . '/install.inc';
  85. return module_load_include('install', $module);
  86. }
  87. /**
  88. * Loads a module include file.
  89. *
  90. * Examples:
  91. * @code
  92. * // Load node.admin.inc from the node module.
  93. * module_load_include('inc', 'node', 'node.admin');
  94. * // Load content_types.inc from the node module.
  95. * module_load_include('inc', 'node', 'content_types');
  96. * @endcode
  97. *
  98. * Do not use this function to load an install file, use module_load_install()
  99. * instead. Do not use this function in a global context since it requires
  100. * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file'
  101. * instead.
  102. *
  103. * @param $type
  104. * The include file's type (file extension).
  105. * @param $module
  106. * The module to which the include file belongs.
  107. * @param $name
  108. * (optional) The base file name (without the $type extension). If omitted,
  109. * $module is used; i.e., resulting in "$module.$type" by default.
  110. *
  111. * @return
  112. * The name of the included file, if successful; FALSE otherwise.
  113. *
  114. * @todo The module_handler service has a loadInclude() method which performs
  115. * this same task but only for enabled modules. Figure out a way to move this
  116. * functionality entirely into the module_handler while keeping the ability to
  117. * load the files of disabled modules.
  118. */
  119. function module_load_include($type, $module, $name = NULL) {
  120. if (!isset($name)) {
  121. $name = $module;
  122. }
  123. if (function_exists('drupal_get_path')) {
  124. $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
  125. if (is_file($file)) {
  126. require_once $file;
  127. return $file;
  128. }
  129. }
  130. return FALSE;
  131. }
  132. /**
  133. * Returns an array of modules required by core.
  134. */
  135. function drupal_required_modules() {
  136. $listing = new ExtensionDiscovery(\Drupal::root());
  137. $files = $listing->scan('module');
  138. $required = [];
  139. // Unless called by the installer, an installation profile is required and
  140. // must always be loaded. drupal_get_profile() also returns the installation
  141. // profile in the installer, but only after it has been selected.
  142. if ($profile = drupal_get_profile()) {
  143. $required[] = $profile;
  144. }
  145. foreach ($files as $name => $file) {
  146. $info = \Drupal::service('info_parser')->parse($file->getPathname());
  147. if (!empty($info) && !empty($info['required']) && $info['required']) {
  148. $required[] = $name;
  149. }
  150. }
  151. return $required;
  152. }
  153. /**
  154. * Sets weight of a particular module.
  155. *
  156. * The weight of uninstalled modules cannot be changed.
  157. *
  158. * @param string $module
  159. * The name of the module (without the .module extension).
  160. * @param int $weight
  161. * An integer representing the weight of the module.
  162. */
  163. function module_set_weight($module, $weight) {
  164. $extension_config = \Drupal::configFactory()->getEditable('core.extension');
  165. if ($extension_config->get("module.$module") !== NULL) {
  166. // Pre-cast the $weight to an integer so that we can save this without using
  167. // schema. This is a performance improvement for module installation.
  168. $extension_config
  169. ->set("module.$module", (int) $weight)
  170. ->set('module', module_config_sort($extension_config->get('module')))
  171. ->save(TRUE);
  172. // Prepare the new module list, sorted by weight, including filenames.
  173. // @see \Drupal\Core\Extension\ModuleInstaller::install()
  174. $module_handler = \Drupal::moduleHandler();
  175. $current_module_filenames = $module_handler->getModuleList();
  176. $current_modules = array_fill_keys(array_keys($current_module_filenames), 0);
  177. $current_modules = module_config_sort(array_merge($current_modules, $extension_config->get('module')));
  178. $module_filenames = [];
  179. foreach ($current_modules as $name => $weight) {
  180. $module_filenames[$name] = $current_module_filenames[$name];
  181. }
  182. // Update the module list in the extension handler.
  183. $module_handler->setModuleList($module_filenames);
  184. return;
  185. }
  186. }
  187. /**
  188. * Sorts the configured list of enabled modules.
  189. *
  190. * The list of enabled modules is expected to be ordered by weight and name.
  191. * The list is always sorted on write to avoid the overhead on read.
  192. *
  193. * @param array $data
  194. * An array of module configuration data.
  195. *
  196. * @return array
  197. * An array of module configuration data sorted by weight and name.
  198. */
  199. function module_config_sort($data) {
  200. // PHP array sorting functions such as uasort() do not work with both keys and
  201. // values at the same time, so we achieve weight and name sorting by computing
  202. // strings with both information concatenated (weight first, name second) and
  203. // use that as a regular string sort reference list via array_multisort(),
  204. // compound of "[sign-as-integer][padded-integer-weight][name]"; e.g., given
  205. // two modules and weights (spaces added for clarity):
  206. // - Block with weight -5: 0 0000000000000000005 block
  207. // - Node with weight 0: 1 0000000000000000000 node
  208. $sort = [];
  209. foreach ($data as $name => $weight) {
  210. // Prefix negative weights with 0, positive weights with 1.
  211. // +/- signs cannot be used, since + (ASCII 43) is before - (ASCII 45).
  212. $prefix = (int) ($weight >= 0);
  213. // The maximum weight is PHP_INT_MAX, so pad all weights to 19 digits.
  214. $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name;
  215. }
  216. array_multisort($sort, SORT_STRING, $data);
  217. return $data;
  218. }