devel.drush.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Drush integration for the devel module.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function devel_drush_command() {
  10. $items['devel-download'] = array(
  11. 'description' => dt('Downloads the FirePHP library from http://firephp.org/.'),
  12. 'arguments' => array(
  13. 'path' => dt('Path to the download folder. This path is relative to the Drupal root. If omitted Drush will use the default location (sites/all/libraries/FirePHPCore).'),
  14. ),
  15. );
  16. $items['devel-reinstall'] = array(
  17. 'description' => dt('Disable, Uninstall, and Install a list of projects.'),
  18. 'drush dependencies' => array('pm'),
  19. 'arguments' => array(
  20. 'projects' => dt('A space-separated list of project names.'),
  21. ),
  22. 'allow-additional-options' => array('pm-disable', 'pm-uninstall', 'pm-enable'),
  23. 'required-arguments' => 1,
  24. 'aliases' => array('dre'),
  25. );
  26. $items['fn-hook'] = array(
  27. 'description' => 'List implementations of a given hook and explore the source of the selected one.',
  28. 'arguments' => array(
  29. 'hook' => 'The name of the hook to explore (e.g. "menu" for hook_menu()).'
  30. ),
  31. 'examples' => array(
  32. 'fn-hook cron' => 'List implementations of hook_cron().',
  33. ),
  34. 'allow-additional-options' => array('fn-view'),
  35. 'required-arguments' => 1,
  36. 'aliases' => array('fnh', 'hook'),
  37. );
  38. $items['fn-view'] = array(
  39. 'description' => 'Show the source of specified function or method.',
  40. 'arguments' => array(
  41. 'function' => 'The name of the function or method to view.',
  42. ),
  43. 'options' => array(
  44. 'pipe' => 'Output just the filename of the function',
  45. 'format' => 'Specify how the filename should be printed. Available placeholders are !startline, !endline and !file',
  46. ),
  47. 'examples' => array(
  48. 'fn-view drupal_set_breadcrumb' => 'View the source code for function "drupal_set_breadcrumb"',
  49. 'vi `drush --pipe fn-view user_access --format=\'+!startline !file\'`' => 'Edit the file that contains the function "user_access"',
  50. 'fn-view NodeController::load' => 'View the source code for method load in the class NodeController'
  51. ),
  52. 'aliases' => array('fnv'),
  53. 'required-arguments' => 1,
  54. );
  55. $items['devel-token'] = array(
  56. 'description' => dt('List available tokens'),
  57. 'aliases' => array('token'),
  58. 'core' => array(7), // Remove once 3.0 is released.
  59. //@todo support --format option for json, csv, etc.
  60. );
  61. return $items;
  62. }
  63. /**
  64. * A command callback. This is faster than 3 separate bootstraps.
  65. */
  66. function drush_devel_reinstall() {
  67. $projects = func_get_args();
  68. $args = array_merge(array('pm-disable'), $projects);
  69. call_user_func_array('drush_invoke', $args);
  70. $args = array_merge(array('pm-uninstall'), $projects);
  71. call_user_func_array('drush_invoke', $args);
  72. $args = array_merge(array('pm-enable'), $projects);
  73. call_user_func_array('drush_invoke', $args);
  74. }
  75. /**
  76. * A command callback.
  77. */
  78. function drush_devel_download($path = NULL) {
  79. // If no path is provided by the user, set our default path.
  80. if (is_null($path)) {
  81. // We use devel folder for legacy reason.
  82. $path = drupal_get_path('module', 'devel') . '/FirePHPCore';
  83. }
  84. // If FirePHP is not installed and libraries module is enabled,
  85. // try to find FirePHP by its own means.
  86. if (!is_dir($path)) {
  87. if (module_exists('libraries')) {
  88. // Libraries 1.x will return a path even if it doesn't exist
  89. // while 2.x will return FALSE.
  90. $path = libraries_get_path('FirePHPCore');
  91. if (!$path) {
  92. $path = 'sites/all/libraries/FirePHPCore';
  93. }
  94. }
  95. }
  96. if (is_dir($path)) {
  97. drush_log(dt('FirePHP already present at @path. No download required.', array('@path' => $path)), 'ok');
  98. }
  99. elseif (drush_shell_exec('svn export http://firephp.googlecode.com/svn/branches/Library-FirePHPCore-0.3 %s', $path)) {
  100. drush_log(dt('FirePHP has been exported via svn to @path.', array('@path' => $path)), 'success');
  101. }
  102. else {
  103. drush_log(dt('Drush was unable to export FirePHP to @path.', array('@path' => $path)), 'warning');
  104. }
  105. }
  106. /**
  107. * Command handler. Show hook implementations.
  108. */
  109. function drush_devel_fn_hook($hook) {
  110. // Get implementations in the .install files as well.
  111. include_once './includes/install.inc';
  112. drupal_load_updates();
  113. if ($hook_implementations = module_implements($hook)) {
  114. if ($choice = drush_choice(array_combine($hook_implementations, $hook_implementations), 'Enter the number of the hook implementation you wish to view.')) {
  115. return drush_devel_fn_view($choice . "_$hook");
  116. }
  117. }
  118. else {
  119. drush_log(dt('No implementations.'), 'ok');
  120. }
  121. }
  122. /**
  123. * Command handler. Show source code of specified function or method.
  124. */
  125. function drush_devel_fn_view($function_name) {
  126. // Get implementations in the .install files as well.
  127. include_once './includes/install.inc';
  128. drupal_load_updates();
  129. if (strpos($function_name, '::') === FALSE) {
  130. if (!function_exists($function_name)) {
  131. return drush_set_error(dt('Function not found'));
  132. }
  133. $reflect = new ReflectionFunction($function_name);
  134. }
  135. else {
  136. list($class, $method) = explode('::', $function_name);
  137. if (!method_exists($class, $method)) {
  138. return drush_set_error(dt('Method not found'));
  139. }
  140. $reflect = new ReflectionMethod($class, $method);
  141. }
  142. $func_info = array('!file' => $reflect->getFileName(), '!startline' => $reflect->getStartLine(), '!endline' => $reflect->getEndLine());
  143. $format = drush_get_option('format', '!file');
  144. drush_print_pipe(dt($format, $func_info));
  145. drush_print(dt("// file: !file, lines !startline-!endline", $func_info));
  146. _drush_devel_print_function($reflect->getFileName(), $reflect->getStartLine(), $reflect->getEndLine());
  147. }
  148. /**
  149. * Command callback. List available tokens.
  150. */
  151. function drush_devel_token() {
  152. $rows[] = array(dt('Group'), dt('Token'), dt('Name'));
  153. $all = token_info();
  154. foreach ($all['tokens'] as $group => $tokens) {
  155. foreach ($tokens as $key => $token) {
  156. $rows[] = array($group, $key, $token['name']);
  157. }
  158. }
  159. drush_print_table($rows, TRUE);
  160. }
  161. /**
  162. * Print the specified function, including any
  163. * doxygen-style comments that come before it.
  164. */
  165. function _drush_devel_print_function($file, $start_line, $end_line) {
  166. $line_num = 0;
  167. $doxygen = NULL;
  168. $fp = fopen( $file, 'r' );
  169. while (!feof($fp) && ($line_num < ($start_line - 1))) {
  170. $line = fgets($fp);
  171. ++$line_num;
  172. if (substr($line,0,3) == '/**') {
  173. $doxygen = $line;
  174. }
  175. elseif (isset($doxygen)) {
  176. $doxygen .= $line;
  177. if ($line_num + 1 == $start_line) {
  178. drush_print(rtrim($doxygen));
  179. }
  180. if (strstr($line, '*/') !== FALSE) {
  181. $doxygen = NULL;
  182. }
  183. }
  184. }
  185. while (!feof($fp) && ($line_num < $end_line)) {
  186. $line = fgets($fp);
  187. ++$line_num;
  188. drush_print(rtrim($line));
  189. }
  190. }