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