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