cli.drush.inc

  1. 8.0.x commands/core/cli.drush.inc
  2. 7.x commands/core/cli.drush.inc
  3. master commands/core/cli.drush.inc

Functions

Namesort descending Description
cli_drush_command Implements hook_drush_command().
drush_cli_core_cli Command callback.
_drush_core_cli_get_casters Returns a mapped array of casters for use in the shell.
_drush_core_cli_get_commands Returns a filtered list of Drush commands used for CLI commands.
_drush_core_cli_get_php_keywords Returns a list of PHP keywords.

File

commands/core/cli.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. use Drupal\Component\Assertion\Handle;
  4. use Drush\Psysh\DrushHelpCommand;
  5. use Drush\Psysh\DrushCommand;
  6. use Drush\Psysh\Shell;
  7. /**
  8. * Implements hook_drush_command().
  9. */
  10. function cli_drush_command() {
  11. $items['core-cli'] = array(
  12. 'description' => 'Open an interactive shell on a Drupal site.',
  13. 'remote-tty' => TRUE,
  14. 'aliases' => array('php'),
  15. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  16. 'topics' => array('docs-repl'),
  17. );
  18. $items['docs-repl'] = array(
  19. 'description' => 'repl.md',
  20. 'hidden' => TRUE,
  21. 'topic' => TRUE,
  22. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  23. 'callback' => 'drush_print_file',
  24. 'callback arguments' => array(drush_get_context('DOC_PREFIX', DRUSH_BASE_PATH) . '/docs/repl.md'),
  25. );
  26. return $items;
  27. }
  28. /**
  29. * Command callback.
  30. */
  31. function drush_cli_core_cli() {
  32. $configuration = new \Psy\Configuration();
  33. $shell = new Shell($configuration);
  34. if (drush_drupal_major_version() >= 8) {
  35. // Register the assertion handler so exceptions are thrown instead of errors
  36. // being triggered. This plays nicer with PsySH.
  37. Handle::register();
  38. $shell->setScopeVariables(['container' => \Drupal::getContainer()]);
  39. // Add Drupal 8 specific casters to the shell configuration.
  40. $configuration->addCasters(_drush_core_cli_get_casters());
  41. }
  42. // Add Drush commands to the shell.
  43. $commands = [new DrushHelpCommand()];
  44. foreach (drush_commands_categorize(_drush_core_cli_get_commands()) as $category_data) {
  45. $category_title = (string) $category_data['title'];
  46. foreach ($category_data['commands'] as $command_config) {
  47. $command = new DrushCommand($command_config);
  48. // Set the category label on each.
  49. $command->setCategory($category_title);
  50. $commands[] = $command;
  51. }
  52. }
  53. $shell->addCommands($commands);
  54. // PsySH will never return control to us, but our shutdown handler will still
  55. // run after the user presses ^D. Mark this command as completed to avoid a
  56. // spurious error message.
  57. drush_set_context('DRUSH_EXECUTION_COMPLETED', TRUE);
  58. // Run the terminate event before the shell is run. Otherwise, if the shell
  59. // is forking processes (the default), any child processes will close the
  60. // database connection when they are killed. So when we return back to the
  61. // parent process after, there is no connection. This will be called after the
  62. // command in preflight still, but the subscriber instances are already
  63. // created from before. Call terminate() regardless, this is a no-op for all
  64. // DrupalBoot classes except DrupalBoot8.
  65. if ($bootstrap = drush_get_bootstrap_object()) {
  66. $bootstrap->terminate();
  67. }
  68. // To fix the above problem in Drupal 7, the connection can be closed manually.
  69. // This will make sure a new connection is created again in child loops. So
  70. // any shutdown functions will still run ok after the shell has exited.
  71. if (drush_drupal_major_version() == 7) {
  72. Database::closeConnection();
  73. }
  74. $shell->run();
  75. }
  76. /**
  77. * Returns a filtered list of Drush commands used for CLI commands.
  78. *
  79. * @return array
  80. */
  81. function _drush_core_cli_get_commands() {
  82. $commands = drush_get_commands();
  83. $ignored_commands = ['help', 'drush-psysh', 'php-eval', 'core-cli', 'php'];
  84. $php_keywords = _drush_core_cli_get_php_keywords();
  85. foreach ($commands as $name => $config) {
  86. // Ignore some commands that don't make sense inside PsySH, are PHP keywords
  87. // are hidden, or are aliases.
  88. if (in_array($name, $ignored_commands) || in_array($name, $php_keywords) || !empty($config['hidden']) || ($name !== $config['command'])) {
  89. unset($commands[$name]);
  90. }
  91. else {
  92. // Make sure the command aliases don't contain any PHP keywords.
  93. if (!empty($config['aliases'])) {
  94. $commands[$name]['aliases'] = array_diff($commands[$name]['aliases'], $php_keywords);
  95. }
  96. }
  97. }
  98. return $commands;
  99. }
  100. /**
  101. * Returns a mapped array of casters for use in the shell.
  102. *
  103. * These are Symfony VarDumper casters.
  104. * See http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
  105. * for more information.
  106. *
  107. * @return array.
  108. * An array of caster callbacks keyed by class or interface.
  109. */
  110. function _drush_core_cli_get_casters() {
  111. return [
  112. 'Drupal\Core\Entity\ContentEntityInterface' => 'Drush\Psysh\Caster::castContentEntity',
  113. 'Drupal\Core\Field\FieldItemListInterface' => 'Drush\Psysh\Caster::castFieldItemList',
  114. 'Drupal\Core\Field\FieldItemInterface' => 'Drush\Psysh\Caster::castFieldItem',
  115. 'Drupal\Core\Config\Entity\ConfigEntityInterface' => 'Drush\Psysh\Caster::castConfigEntity',
  116. 'Drupal\Core\Config\ConfigBase' => 'Drush\Psysh\Caster::castConfig',
  117. ];
  118. }
  119. /**
  120. * Returns a list of PHP keywords.
  121. *
  122. * This will act as a blacklist for command and alias names.
  123. *
  124. * @return array
  125. */
  126. function _drush_core_cli_get_php_keywords() {
  127. return [
  128. '__halt_compiler',
  129. 'abstract',
  130. 'and',
  131. 'array',
  132. 'as',
  133. 'break',
  134. 'callable',
  135. 'case',
  136. 'catch',
  137. 'class',
  138. 'clone',
  139. 'const',
  140. 'continue',
  141. 'declare',
  142. 'default',
  143. 'die',
  144. 'do',
  145. 'echo',
  146. 'else',
  147. 'elseif',
  148. 'empty',
  149. 'enddeclare',
  150. 'endfor',
  151. 'endforeach',
  152. 'endif',
  153. 'endswitch',
  154. 'endwhile',
  155. 'eval',
  156. 'exit',
  157. 'extends',
  158. 'final',
  159. 'for',
  160. 'foreach',
  161. 'function',
  162. 'global',
  163. 'goto',
  164. 'if',
  165. 'implements',
  166. 'include',
  167. 'include_once',
  168. 'instanceof',
  169. 'insteadof',
  170. 'interface',
  171. 'isset',
  172. 'list',
  173. 'namespace',
  174. 'new',
  175. 'or',
  176. 'print',
  177. 'private',
  178. 'protected',
  179. 'public',
  180. 'require',
  181. 'require_once',
  182. 'return',
  183. 'static',
  184. 'switch',
  185. 'throw',
  186. 'trait',
  187. 'try',
  188. 'unset',
  189. 'use',
  190. 'var',
  191. 'while',
  192. 'xor',
  193. ];
  194. }