9a9192090ea55157870f4b7d04e0b8277e231668
[yaffs-website] / vendor / drush / drush / commands / core / drupal / environment.inc
1 <?php
2 /**
3  * @file
4  *   Specific functions for a drupal 8+ environment.
5  *   drush_include_engine() magically includes either this file
6  *   or environment_X.inc depending on which version of drupal Drush
7  *   is called from.
8  */
9
10 use Drupal\Core\Site\Settings;
11 use Drupal\Core\StreamWrapper\PrivateStream;
12 use Drupal\Core\StreamWrapper\PublicStream;
13 use Drush\Log\LogLevel;
14 use Drupal\Core\Logger\RfcLogLevel;
15
16 /**
17  * Get complete information for all available modules.
18  *
19  * @param $include_hidden
20  *   Boolean to indicate whether hidden modules should be excluded or not.
21  * @return
22  *   An array containing module info for all available modules.
23  */
24 function drush_get_modules($include_hidden = TRUE) {
25   $modules = system_rebuild_module_data();
26
27   foreach ($modules as $key => $module) {
28     if ((!$include_hidden) && (!empty($module->info['hidden']))) {
29       unset($modules[$key]);
30     }
31     else {
32       $module->schema_version = drupal_get_installed_schema_version($key);
33     }
34   }
35
36   return $modules;
37 }
38
39 /**
40  * Returns drupal required modules, including modules declared as required dynamically.
41  */
42 function _drush_drupal_required_modules($module_info) {
43   $required = drupal_required_modules();
44   foreach ($module_info as $name => $module) {
45     if (isset($module->info['required']) && $module->info['required']) {
46       $required[] = $name;
47     }
48   }
49   return array_unique($required);
50 }
51
52 /**
53  * Return dependencies and its status for modules.
54  *
55  * @param $modules
56  *   Array of module names
57  * @param $module_info
58  *   Drupal 'files' array for modules as returned by drush_get_modules().
59  * @return
60  *   Array with dependencies and status for $modules
61  */
62 function drush_check_module_dependencies($modules, $module_info) {
63   $status = array();
64   foreach ($modules as $key => $module) {
65     $dependencies = array_reverse($module_info[$module]->requires);
66     $unmet_dependencies = array_diff(array_keys($dependencies), array_keys($module_info));
67     if (!empty($unmet_dependencies)) {
68       $status[$key]['error'] = array(
69           'code' => 'DRUSH_PM_ENABLE_DEPENDENCY_NOT_FOUND',
70           'message' => dt('Module !module cannot be enabled because it depends on the following modules which could not be found: !unmet_dependencies', array('!module' => $module, '!unmet_dependencies' => implode(',', $unmet_dependencies)))
71       );
72     }
73     else {
74       // check for version incompatibility
75       foreach ($dependencies as $dependency_name => $v) {
76         $current_version = $module_info[$dependency_name]->info['version'];
77         $current_version = str_replace(drush_get_drupal_core_compatibility() . '-', '', $current_version);
78         $incompatibility = drupal_check_incompatibility($v, $current_version);
79         if (isset($incompatibility)) {
80           $status[$key]['error'] = array(
81             'code' => 'DRUSH_PM_ENABLE_DEPENDENCY_VERSION_MISMATCH',
82             'message' => dt('Module !module cannot be enabled because it depends on !dependency !required_version but !current_version is available', array('!module' => $module, '!dependency' => $dependency_name, '!required_version' => $incompatibility, '!current_version' => $current_version))
83           );
84         }
85       }
86     }
87     $status[$key]['unmet-dependencies'] = $unmet_dependencies;
88     $status[$key]['dependencies'] = $dependencies;
89   }
90
91   return $status;
92 }
93
94 /**
95  * Return dependents of modules.
96  *
97  * @param $modules
98  *   Array of module names
99  * @param $module_info
100  *   Drupal 'files' array for modules as returned by drush_get_modules().
101  * @return
102  *   Array with dependents for each one of $modules
103  */
104 function drush_module_dependents($modules, $module_info) {
105   $dependents = array();
106   foreach ($modules as $module) {
107     $keys = array_keys($module_info[$module]->required_by);
108     $dependents = array_merge($dependents, array_combine($keys, $keys));
109   }
110
111   return array_unique($dependents);
112 }
113
114 /**
115  * Returns a list of enabled modules.
116  *
117  * This is a wrapper for module_list().
118  */
119 function drush_module_list() {
120   $modules = array_keys(\Drupal::moduleHandler()->getModuleList());
121   return array_combine($modules, $modules);
122 }
123
124 /**
125  * Installs a given list of modules.
126  *
127  * @see \Drupal\Core\Extension\ModuleInstallerInterface::install()
128  *
129  */
130 function drush_module_install($module_list, $enable_dependencies = TRUE) {
131   return \Drupal::service('module_installer')->install($module_list, $enable_dependencies);
132 }
133
134 /**
135  * Checks that a given module exists and is enabled.
136  *
137  * @see \Drupal\Core\Extension\ModuleHandlerInterface::moduleExists()
138  *
139  */
140 function drush_module_exists($module) {
141   return \Drupal::moduleHandler()->moduleExists($module);
142 }
143
144 /**
145  * Determines which modules are implementing a hook.
146  *
147  * @param string $hook
148  *   The hook name.
149  * @param bool $sort
150  *  Not used in Drupal 8 environment.
151  * @param bool $reset
152  *  TRUE to reset the hook implementation cache.
153  *
154  * @see \Drupal\Core\Extension\ModuleHandlerInterface::getImplementations().
155  * @see \Drupal\Core\Extension\ModuleHandlerInterface::resetImplementations().
156  *
157  */
158 function drush_module_implements($hook, $sort = FALSE, $reset = FALSE) {
159   // $sort is there for consistency, but looks like Drupal 8 has no equilavient for it.
160   // We can sort the list manually later if really needed.
161   if ($reset == TRUE){
162     \Drupal::moduleHandler()->resetImplementations();
163   }
164   return \Drupal::moduleHandler()->getImplementations($hook);
165 }
166
167 /**
168  * Return a list of modules from a list of named modules.
169  * Both enabled and disabled/uninstalled modules are returned.
170  */
171 function drush_get_named_extensions_list($extensions) {
172   $result = array();
173   $modules = drush_get_modules();
174   foreach($modules as $name => $module) {
175     if (in_array($name, $extensions)) {
176       $result[$name] = $module;
177     }
178   }
179   $themes = drush_get_themes();
180   foreach($themes as $name => $theme) {
181     if (in_array($name, $extensions)) {
182       $result[$name] = $theme;
183     }
184   }
185   return $result;
186 }
187
188 /**
189  * Enable a list of modules. It is assumed the list contains all the dependencies not already enabled.
190  *
191  * @param $modules
192  *   Array of module names
193  */
194 function drush_module_enable($modules) {
195   // The list of modules already have all the dependencies, but they might not
196   // be in the correct order. Still pass $enable_dependencies = TRUE so that
197   // Drupal will enable the modules in the correct order.
198   drush_module_install($modules);
199
200   // Our logger got blown away during the container rebuild above.
201   $boot = drush_select_bootstrap_class();
202   $boot->add_logger();
203
204   // Flush all caches. No longer needed in D8 per https://github.com/drush-ops/drush/issues/1207
205   // drupal_flush_all_caches();
206 }
207
208 /**
209  * Disable a list of modules. It is assumed the list contains all dependents not already disabled.
210  *
211  * @param $modules
212  *   Array of module names
213  */
214 function drush_module_disable($modules) {
215   drush_set_error('DRUSH_MODULE_DISABLE', dt('Drupal 8 does not support disabling modules. Use pm-uninstall instead.'));
216 }
217
218 /**
219  * Uninstall a list of modules.
220  *
221  * @param $modules
222  *   Array of module names
223  *
224  * @see \Drupal\Core\Extension\ModuleInstallerInterface::uninstall()
225  */
226 function drush_module_uninstall($modules) {
227   \Drupal::service('module_installer')->uninstall($modules);
228   // Our logger got blown away during the container rebuild above.
229   $boot = drush_select_bootstrap_class();
230   $boot->add_logger();
231 }
232
233 /**
234   * Invokes a hook in a particular module.
235   *
236   */
237 function drush_module_invoke($module, $hook) {
238   $args = func_get_args();
239   // Remove $module and $hook from the arguments.
240   unset($args[0], $args[1]);
241   return \Drupal::moduleHandler()->invoke($module, $hook, $args);
242 }
243
244 /**
245   * Invokes a hook in all enabled modules that implement it.
246   *
247   */
248 function drush_module_invoke_all($hook) {
249   $args = func_get_args();
250   // Remove $hook from the arguments.
251   array_shift($args);
252   return \Drupal::moduleHandler()->invokeAll($hook, $args);
253 }
254
255 /**
256  * Returns a list of enabled themes. Use drush_get_themes() if you need to rebuild
257  * and include hidden as well.
258  *
259  * @return \Drupal\Core\Extension\Extension[]
260  *  A list of themes keyed by name.
261  */
262 function drush_theme_list() {
263   $theme_handler = \Drupal::service('theme_handler');
264   return $theme_handler->listInfo();
265 }
266
267 /**
268  * Get complete information for all available themes.
269  *
270  * @param $include_hidden
271  *   Boolean to indicate whether hidden themes should be excluded or not.
272  * @return
273  *   An array containing theme info for all available themes.
274  */
275 function drush_get_themes($include_hidden = TRUE) {
276   $themes = \Drupal::service('theme_handler')->rebuildThemeData();
277   foreach ($themes as $key => $theme) {
278     if (!$include_hidden) {
279       if (isset($theme->info['hidden'])) {
280         // Don't exclude default or admin theme.
281         if ($key != _drush_theme_default() && $key != _drush_theme_admin()) {
282           unset($themes[$key]);
283         }
284       }
285     }
286   }
287
288   return $themes;
289 }
290
291 /**
292  * Enable a list of themes.
293  *
294  * @param $themes
295  *  Array of theme names.
296  */
297 function drush_theme_enable($themes) {
298   \Drupal::service('theme_handler')->install($themes);
299 }
300
301 /**
302  * Disable a list of themes.
303  *
304  * @param $themes
305  *  Array of theme names.
306  */
307 function drush_theme_disable($themes) {
308   drush_set_error('DRUSH_THEME_DISABLE', dt('Drupal 8 does not support disabling themes. Use pm-uninstall instead.'));
309 }
310
311 /**
312  * Uninstall a list of themes.
313  *
314  * @param $themes
315  *  Array of theme names
316  *
317  * @see \Drupal\Core\Extension\ThemeHandlerInterface::uninstall()
318  */
319 function drush_theme_uninstall($themes) {
320   \Drupal::service('theme_handler')->uninstall($themes);
321   // Our logger got blown away during the container rebuild above.
322   $boot = drush_select_bootstrap_class();
323   $boot->add_logger();
324 }
325
326 /**
327  * Helper function to obtain the severity levels based on Drupal version.
328  *
329  * @return array
330  *   Watchdog severity levels keyed by RFC 3164 severities.
331  */
332 function drush_watchdog_severity_levels() {
333   return array(
334     RfcLogLevel::EMERGENCY => LogLevel::EMERGENCY,
335     RfcLogLevel::ALERT => LogLevel::ALERT,
336     RfcLogLevel::CRITICAL => LogLevel::CRITICAL,
337     RfcLogLevel::ERROR => LogLevel::ERROR,
338     RfcLogLevel::WARNING => LogLevel::WARNING,
339     RfcLogLevel::NOTICE => LogLevel::NOTICE,
340     RfcLogLevel::INFO => LogLevel::INFO,
341     RfcLogLevel::DEBUG => LogLevel::DEBUG,
342   );
343 }
344
345 /**
346  * Helper function to obtain the message types based on drupal version.
347  *
348  * @return
349  *   Array of watchdog message types.
350  */
351 function drush_watchdog_message_types() {
352   return _dblog_get_message_types();
353 }
354
355 function _drush_theme_default() {
356   return \Drupal::config('system.theme')->get('default');
357 }
358
359 function _drush_theme_admin() {
360   $theme = \Drupal::config('system.theme')->get('admin');
361   return empty($theme) ? 'seven' : $theme;
362 }
363
364 function _drush_file_public_path() {
365   return PublicStream::basePath();
366 }
367
368 function _drush_file_private_path() {
369   return PrivateStream::basePath();
370 }
371
372 /**
373  * Gets the extension name.
374  *
375  * @param $info
376  *   The extension info.
377  * @return string
378  *   The extension name.
379  */
380 function _drush_extension_get_name($info) {
381   return $info->getName();
382 }
383
384 /**
385  * Gets the extension type.
386  *
387  * @param $info
388  *   The extension info.
389  * @return string
390  *   The extension type.
391  */
392 function _drush_extension_get_type($info) {
393   return $info->getType();
394 }
395
396 /**
397  * Gets the extension path.
398  *
399  * @param $info
400  *   The extension info.
401  * @return string
402  *   The extension path.
403  */
404 function _drush_extension_get_path($info) {
405   return $info->getPath();
406 }
407
408 /*
409  * Wrapper for CSRF token generation.
410  */
411 function drush_get_token($value = NULL) {
412   return \Drupal::csrfToken()->get($value);
413 }
414
415 /*
416  * Wrapper for _url().
417  */
418 function drush_url($path = NULL, array $options = array()) {
419   return \Drupal\Core\Url::fromUserInput('/' . $path, $options)->toString();
420 }
421
422 /**
423  * Output a Drupal render array, object or string as plain text.
424  *
425  * @param string $data
426  *   Data to render.
427  *
428  * @return string
429  *   The plain-text representation of the input.
430  */
431 function drush_render($data) {
432   if (is_array($data)) {
433     $data = \Drupal::service('renderer')->renderRoot($data);
434   }
435
436   $data = \Drupal\Core\Mail\MailFormatHelper::htmlToText($data);
437   return $data;
438 }