devel.drush.inc 6.2 KB

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