environment.inc

  1. 8.0.x includes/environment.inc
  2. 8.0.x commands/core/drupal/environment.inc
  3. 6.x includes/environment.inc
  4. 6.x commands/core/drupal/environment.inc
  5. 7.x includes/environment.inc
  6. 7.x commands/core/drupal/environment.inc
  7. 3.x includes/environment.inc
  8. 4.x commands/core/drupal/environment.inc
  9. 4.x includes/environment.inc
  10. 5.x commands/core/drupal/environment.inc
  11. 5.x includes/environment.inc
  12. master commands/core/drupal/environment.inc
  13. master includes/environment.inc

Specific functions for a drupal 8+ environment. drush_include_engine() magically includes either this file or environment_X.inc depending on which version of drupal Drush is called from.

Functions

Namesort descending Description
drush_check_module_dependencies Return dependencies and its status for modules.
drush_get_modules Get complete information for all available modules.
drush_get_named_extensions_list Return a list of modules from a list of named modules. Both enabled and disabled/uninstalled modules are returned.
drush_get_themes Get complete information for all available themes.
drush_get_token
drush_module_dependents Return dependents of modules.
drush_module_disable Disable a list of modules. It is assumed the list contains all dependents not already disabled.
drush_module_enable Enable a list of modules. It is assumed the list contains all the dependencies not already enabled.
drush_module_exists Checks that a given module exists and is enabled.
drush_module_implements Determines which modules are implementing a hook.
drush_module_install Installs a given list of modules.
drush_module_invoke Invokes a hook in a particular module.
drush_module_invoke_all Invokes a hook in all enabled modules that implement it.
drush_module_list Returns a list of enabled modules.
drush_module_uninstall Uninstall a list of modules.
drush_theme_disable Disable a list of themes.
drush_theme_enable Enable a list of themes.
drush_theme_uninstall Uninstall a list of themes.
drush_url
drush_watchdog_message_types Helper function to obtain the message types based on drupal version.
drush_watchdog_severity_levels Helper function to obtain the severity levels based on Drupal version.
_drush_drupal_required_modules Returns drupal required modules, including modules declared as required dynamically.
_drush_extension_get_name Gets the extension name.
_drush_extension_get_path Gets the extension path.
_drush_extension_get_type Gets the extension type.
_drush_file_private_path
_drush_file_public_path
_drush_theme_admin
_drush_theme_default

File

commands/core/drupal/environment.inc
View source
  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. use Drupal\Core\Site\Settings;
  10. use Drupal\Core\StreamWrapper\PrivateStream;
  11. use Drush\Log\LogLevel;
  12. /**
  13. * Get complete information for all available modules.
  14. *
  15. * @param $include_hidden
  16. * Boolean to indicate whether hidden modules should be excluded or not.
  17. * @return
  18. * An array containing module info for all available modules.
  19. */
  20. function drush_get_modules($include_hidden = TRUE) {
  21. $modules = system_rebuild_module_data();
  22. foreach ($modules as $key => $module) {
  23. if ((!$include_hidden) && (isset($module->info['hidden']))) {
  24. unset($modules[$key]);
  25. }
  26. else {
  27. $module->schema_version = drupal_get_installed_schema_version($key);
  28. }
  29. }
  30. return $modules;
  31. }
  32. /**
  33. * Returns drupal required modules, including modules declared as required dynamically.
  34. */
  35. function _drush_drupal_required_modules($module_info) {
  36. $required = drupal_required_modules();
  37. foreach ($module_info as $name => $module) {
  38. if (isset($module->info['required']) && $module->info['required']) {
  39. $required[] = $name;
  40. }
  41. }
  42. return array_unique($required);
  43. }
  44. /**
  45. * Return dependencies and its status for modules.
  46. *
  47. * @param $modules
  48. * Array of module names
  49. * @param $module_info
  50. * Drupal 'files' array for modules as returned by drush_get_modules().
  51. * @return
  52. * Array with dependencies and status for $modules
  53. */
  54. function drush_check_module_dependencies($modules, $module_info) {
  55. $status = array();
  56. foreach ($modules as $key => $module) {
  57. $dependencies = array_reverse($module_info[$module]->requires);
  58. $unmet_dependencies = array_diff(array_keys($dependencies), array_keys($module_info));
  59. if (!empty($unmet_dependencies)) {
  60. $status[$key]['error'] = array(
  61. 'code' => 'DRUSH_PM_ENABLE_DEPENDENCY_NOT_FOUND',
  62. '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)))
  63. );
  64. }
  65. else {
  66. // check for version incompatibility
  67. foreach ($dependencies as $dependency_name => $v) {
  68. $current_version = $module_info[$dependency_name]->info['version'];
  69. $current_version = str_replace(drush_get_drupal_core_compatibility() . '-', '', $current_version);
  70. $incompatibility = drupal_check_incompatibility($v, $current_version);
  71. if (isset($incompatibility)) {
  72. $status[$key]['error'] = array(
  73. 'code' => 'DRUSH_PM_ENABLE_DEPENDENCY_VERSION_MISMATCH',
  74. '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))
  75. );
  76. }
  77. }
  78. }
  79. $status[$key]['unmet-dependencies'] = $unmet_dependencies;
  80. $status[$key]['dependencies'] = array_keys($dependencies);
  81. }
  82. return $status;
  83. }
  84. /**
  85. * Return dependents of modules.
  86. *
  87. * @param $modules
  88. * Array of module names
  89. * @param $module_info
  90. * Drupal 'files' array for modules as returned by drush_get_modules().
  91. * @return
  92. * Array with dependents for each one of $modules
  93. */
  94. function drush_module_dependents($modules, $module_info) {
  95. $dependents = array();
  96. foreach ($modules as $module) {
  97. $keys = array_keys($module_info[$module]->required_by);
  98. $dependents = array_merge($dependents, array_combine($keys, $keys));
  99. }
  100. return array_unique($dependents);
  101. }
  102. /**
  103. * Returns a list of enabled modules.
  104. *
  105. * This is a wrapper for module_list().
  106. */
  107. function drush_module_list() {
  108. $modules = array_keys(\Drupal::moduleHandler()->getModuleList());
  109. return array_combine($modules, $modules);
  110. }
  111. /**
  112. * Installs a given list of modules.
  113. *
  114. * @see \Drupal\Core\Extension\ModuleInstallerInterface::install()
  115. *
  116. */
  117. function drush_module_install($module_list, $enable_dependencies = TRUE) {
  118. return \Drupal::service('module_installer')->install($module_list, $enable_dependencies);
  119. }
  120. /**
  121. * Checks that a given module exists and is enabled.
  122. *
  123. * @see \Drupal\Core\Extension\ModuleHandlerInterface::moduleExists()
  124. *
  125. */
  126. function drush_module_exists($module) {
  127. return \Drupal::moduleHandler()->moduleExists($module);
  128. }
  129. /**
  130. * Determines which modules are implementing a hook.
  131. *
  132. * @param string $hook
  133. * The hook name.
  134. * @param bool $sort
  135. * Not used in Drupal 8 environment.
  136. * @param bool $reset
  137. * TRUE to reset the hook implementation cache.
  138. *
  139. * @see \Drupal\Core\Extension\ModuleHandlerInterface::getImplementations().
  140. * @see \Drupal\Core\Extension\ModuleHandlerInterface::resetImplementations().
  141. *
  142. */
  143. function drush_module_implements($hook, $sort = FALSE, $reset = FALSE) {
  144. // $sort is there for consistency, but looks like Drupal 8 has no equilavient for it.
  145. // We can sort the list manually later if really needed.
  146. if ($reset == TRUE){
  147. \Drupal::moduleHandler()->resetImplementations();
  148. }
  149. return \Drupal::moduleHandler()->getImplementations($hook);
  150. }
  151. /**
  152. * Return a list of modules from a list of named modules.
  153. * Both enabled and disabled/uninstalled modules are returned.
  154. */
  155. function drush_get_named_extensions_list($extensions) {
  156. $result = array();
  157. $modules = drush_get_modules();
  158. foreach($modules as $name => $module) {
  159. if (in_array($name, $extensions)) {
  160. $result[$name] = $module;
  161. }
  162. }
  163. $themes = drush_get_themes();
  164. foreach($themes as $name => $theme) {
  165. if (in_array($name, $extensions)) {
  166. $result[$name] = $theme;
  167. }
  168. }
  169. return $result;
  170. }
  171. /**
  172. * Enable a list of modules. It is assumed the list contains all the dependencies not already enabled.
  173. *
  174. * @param $modules
  175. * Array of module names
  176. */
  177. function drush_module_enable($modules) {
  178. // The list of modules already have all the dependencies, but they might not
  179. // be in the correct order. Still pass $enable_dependencies = TRUE so that
  180. // Drupal will enable the modules in the correct order.
  181. drush_module_install($modules);
  182. // Our logger got blown away during the container rebuild above.
  183. \Drupal::getContainer()->get('logger.factory')->addLogger(new \Drush\Log\DrushLog);
  184. // Flush all caches. No longer needed in D8 per https://github.com/drush-ops/drush/issues/1207
  185. // drupal_flush_all_caches();
  186. }
  187. /**
  188. * Disable a list of modules. It is assumed the list contains all dependents not already disabled.
  189. *
  190. * @param $modules
  191. * Array of module names
  192. */
  193. function drush_module_disable($modules) {
  194. drush_set_error('DRUSH_MODULE_DISABLE', dt('Drupal 8 does not support disabling modules. Use pm-uninstall instead.'));
  195. }
  196. /**
  197. * Uninstall a list of modules.
  198. *
  199. * @param $modules
  200. * Array of module names
  201. *
  202. * @see \Drupal\Core\Extension\ModuleInstallerInterface::uninstall()
  203. */
  204. function drush_module_uninstall($modules) {
  205. \Drupal::service('module_installer')->uninstall($modules);
  206. // Our logger got blown away during the container rebuild above.
  207. \Drupal::getContainer()->get('logger.factory')->addLogger(new \Drush\Log\DrushLog);
  208. }
  209. /**
  210. * Invokes a hook in a particular module.
  211. *
  212. */
  213. function drush_module_invoke($module, $hook) {
  214. $args = func_get_args();
  215. // Remove $module and $hook from the arguments.
  216. unset($args[0], $args[1]);
  217. return \Drupal::moduleHandler()->invoke($module, $hook, $args);
  218. }
  219. /**
  220. * Invokes a hook in all enabled modules that implement it.
  221. *
  222. */
  223. function drush_module_invoke_all($hook) {
  224. $args = func_get_args();
  225. // Remove $hook from the arguments.
  226. array_shift($args);
  227. return Drupal::moduleHandler()->invokeAll($hook, $args);
  228. }
  229. /**
  230. * Get complete information for all available themes.
  231. *
  232. * @param $include_hidden
  233. * Boolean to indicate whether hidden themes should be excluded or not.
  234. * @return
  235. * An array containing theme info for all available themes.
  236. */
  237. function drush_get_themes($include_hidden = TRUE) {
  238. $themes = \Drupal::service('theme_handler')->rebuildThemeData();
  239. foreach ($themes as $key => $theme) {
  240. if (!$include_hidden) {
  241. if (isset($theme->info['hidden'])) {
  242. unset($themes[$key]);
  243. }
  244. }
  245. }
  246. return $themes;
  247. }
  248. /**
  249. * Enable a list of themes.
  250. *
  251. * @param $themes
  252. * Array of theme names.
  253. */
  254. function drush_theme_enable($themes) {
  255. \Drupal::service('theme_handler')->install($themes);
  256. }
  257. /**
  258. * Disable a list of themes.
  259. *
  260. * @param $themes
  261. * Array of theme names.
  262. */
  263. function drush_theme_disable($themes) {
  264. drush_set_error('DRUSH_THEME_DISABLE', dt('Drupal 8 does not support disabling themes. Use pm-uninstall instead.'));
  265. }
  266. /**
  267. * Uninstall a list of themes.
  268. *
  269. * @param $themes
  270. * Array of theme names
  271. *
  272. * @see \Drupal\Core\Extension\ThemeHandlerInterface::uninstall()
  273. */
  274. function drush_theme_uninstall($themes) {
  275. \Drupal::service('theme_handler')->uninstall($themes);
  276. // Our logger got blown away during the container rebuild above.
  277. \Drupal::getContainer()->get('logger.factory')->addLogger(new \Drush\Log\DrushLog);
  278. }
  279. /**
  280. * Helper function to obtain the severity levels based on Drupal version.
  281. *
  282. * This is a copy of watchdog_severity_levels() without t().
  283. *
  284. * Severity levels, as defined in RFC 3164: http://www.ietf.org/rfc/rfc3164.txt.
  285. *
  286. * @return
  287. * Array of watchdog severity levels.
  288. */
  289. function drush_watchdog_severity_levels() {
  290. return array(
  291. LogLevel::EMERGENCY => 'emergency',
  292. LogLevel::ALERT => 'alert',
  293. LogLevel::CRITICAL => 'critical',
  294. LogLevel::ERROR => 'error',
  295. LogLevel::WARNING => 'warning',
  296. LogLevel::NOTICE => 'notice',
  297. LogLevel::INFO => 'info',
  298. LogLevel::DEBUG => 'debug',
  299. );
  300. }
  301. /**
  302. * Helper function to obtain the message types based on drupal version.
  303. *
  304. * @return
  305. * Array of watchdog message types.
  306. */
  307. function drush_watchdog_message_types() {
  308. return _dblog_get_message_types();
  309. }
  310. function _drush_theme_default() {
  311. return \Drupal::config('system.theme')->get('default');
  312. }
  313. function _drush_theme_admin() {
  314. $theme = \Drupal::config('system.theme')->get('admin');
  315. return empty($theme) ? 'seven' : $theme;
  316. }
  317. function _drush_file_public_path() {
  318. return Settings::get('file_public_path', conf_path() . '/files');
  319. }
  320. function _drush_file_private_path() {
  321. return PrivateStream::basePath();
  322. }
  323. /**
  324. * Gets the extension name.
  325. *
  326. * @param $info
  327. * The extension info.
  328. * @return string
  329. * The extension name.
  330. */
  331. function _drush_extension_get_name($info) {
  332. return $info->getName();
  333. }
  334. /**
  335. * Gets the extension type.
  336. *
  337. * @param $info
  338. * The extension info.
  339. * @return string
  340. * The extension type.
  341. */
  342. function _drush_extension_get_type($info) {
  343. return $info->getType();
  344. }
  345. /**
  346. * Gets the extension path.
  347. *
  348. * @param $info
  349. * The extension info.
  350. * @return string
  351. * The extension path.
  352. */
  353. function _drush_extension_get_path($info) {
  354. return $info->getPath();
  355. }
  356. /*
  357. * Wrapper for CSRF token generation.
  358. */
  359. function drush_get_token($value = NULL) {
  360. return \Drupal::csrfToken()->get($value);
  361. }
  362. /*
  363. * Wrapper for _url().
  364. */
  365. function drush_url($path = NULL, array $options = array()) {
  366. return \Drupal::urlGenerator()->generateFromPath($path, $options);
  367. }