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