5 * Drush integration for the devel module.
8 use Drupal\Component\Uuid\Php;
11 * Implements hook_drush_command().
13 function devel_drush_command() {
14 $items['devel-reinstall'] = array(
15 'description' => dt('Uninstall, and Install a list of projects.'),
16 'drush dependencies' => array('pm'),
18 'projects' => dt('A space-separated list of project names.'),
20 'allow-additional-options' => array('pm-uninstall', 'pm-enable'),
21 'required-arguments' => 1,
22 'aliases' => array('dre'),
24 $items['fn-hook'] = array(
25 'description' => 'List implementations of a given hook and explore the source of the selected one.',
27 'hook' => 'The name of the hook to explore (e.g. "menu" for hook_menu()).'
30 'fn-hook cron' => 'List implementations of hook_cron().',
32 'allow-additional-options' => array('fn-view'),
33 'required-arguments' => 1,
34 'aliases' => array('fnh', 'hook'),
36 $items['fn-event'] = array(
37 'description' => 'List implementations of a given event and explore source of specified one.',
39 'event' => 'The name of the event to explore. If omitted, a list of events is shown.'
42 'fn-event' => 'Pick a Kernel event, then pick an implementation, and then view its source code.',
43 'fn-event kernel.terminate' => 'Pick a terminate subscribers and view its source code.',
45 'allow-additional-options' => array('fn-view'),
46 'aliases' => array('fne', 'event'),
48 $items['fn-view'] = array(
49 'description' => 'Show the source of specified function or method.',
51 'function' => 'The name of the function or method to view.',
54 'pipe' => 'Output just the filename of the function or method',
55 'format' => 'Specify how the filename should be printed. Available placeholders are @startline, @endline and @file',
58 'fn-view drupal_set_breadcrumb' => 'View the source code for function "drupal_set_breadcrumb"',
59 'vi `drush --pipe fn-view user_access --format=\'+@startline @file\'`' => 'Edit the file that contains the function "user_access"',
60 'fn-view NodeController::load' => 'View the source code for method load in the class NodeController'
62 'aliases' => array('fnv'),
63 'required-arguments' => 1,
65 $items['devel-token'] = array(
66 'description' => dt('List available tokens'),
67 'aliases' => array('token'),
68 //@todo support --format option for json, csv, etc.
71 $items['devel-container-services'] = array(
72 'description' => 'Get a list of available container services.',
73 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
74 'core' => array('8+'),
75 'aliases' => array('dcs'),
77 'format' => 'Format to output. Allowed values are: json, export, html.',
80 'prefix' => 'A prefix to filter the service list by.',
83 'drush container-services' => 'Gets a list of all available container services',
84 'drush container-services plugin.manager' => 'Get all services containing "plugin.manager"',
86 'outputformat' => array(
88 'pipe-format' => 'export',
92 $items['devel-generate-uuid'] = array(
93 'description' => 'Generate a UUID.',
94 'core' => array('8+'),
96 "drush devel-generate-uuid" => "Outputs a Universally Unique IDentifier.",
98 'aliases' => array('uuid'),
99 'outputformat' => array(
100 'default' => 'string',
108 * A command callback. This is faster than 3 separate bootstraps.
110 function drush_devel_reinstall() {
111 $projects = func_get_args();
113 $args = array_merge(array('pm-uninstall'), $projects);
114 call_user_func_array('drush_invoke', $args);
116 $args = array_merge(array('pm-enable'), $projects);
117 call_user_func_array('drush_invoke', $args);
121 * Command handler. Show hook implementations.
123 function drush_devel_fn_hook($hook) {
124 // Get implementations in the .install files as well.
125 include_once './core/includes/install.inc';
126 drupal_load_updates();
128 if ($hook_implementations = \Drupal::moduleHandler()->getImplementations($hook)) {
129 if ($choice = drush_choice(array_combine($hook_implementations, $hook_implementations), 'Enter the number of the hook implementation you wish to view.')) {
130 return drush_devel_fn_view($choice . "_$hook");
134 drush_log(dt('No implementations.'), 'ok');
139 * Command handler. Show hook implementations.
141 function drush_devel_fn_event($event = NULL) {
142 $dispatcher = Drupal::service('event_dispatcher');
144 $events = array('kernel.controller', 'kernel.exception', 'kernel.request', 'kernel.response', 'kernel.terminate', 'kernel.view');
145 $events = array_combine($events, $events);
146 if (!$event = drush_choice($events, 'Enter the event you wish to explore.')) {
147 return drush_user_abort();
150 if ($implementations = $dispatcher->getListeners($event)) {
151 foreach ($implementations as $implementation) {
152 $callable = get_class($implementation[0]) . '::' . $implementation[1];
153 $choices[$callable] = $callable;
155 if ($choice = drush_choice($choices, 'Enter the number of the implementation you wish to view.')) {
156 return drush_devel_fn_view($choice);
160 drush_log(dt('No implementations.'), 'ok');
164 * Command handler. Show source code of specified function or method.
166 function drush_devel_fn_view($function_name) {
167 // Get implementations in the .install files as well.
168 include_once './core/includes/install.inc';
169 drupal_load_updates();
171 if (strpos($function_name, '::') === FALSE) {
172 if (!function_exists($function_name)) {
173 return drush_set_error(dt('Function not found'));
175 $reflect = new ReflectionFunction($function_name);
178 list($class, $method) = explode('::', $function_name);
179 if (!method_exists($class, $method)) {
180 return drush_set_error(dt('Method not found'));
182 $reflect = new ReflectionMethod($class, $method);
184 $func_info = array('@file' => $reflect->getFileName(), '@startline' => $reflect->getStartLine(), '@endline' => $reflect->getEndLine());
185 $format = drush_get_option('format', '@file');
186 drush_print_pipe(dt($format, $func_info));
187 drush_print(dt("// file: @file, lines @startline-@endline", $func_info));
189 _drush_devel_print_function($reflect->getFileName(), $reflect->getStartLine(), $reflect->getEndLine());
193 * Command callback. List available tokens.
195 function drush_devel_token() {
196 $rows[] = array(dt('Group'), dt('Token'), dt('Name'));
197 $all = \Drupal::token()->getInfo();
198 foreach ($all['tokens'] as $group => $tokens) {
199 foreach ($tokens as $key => $token) {
200 $rows[] = array($group, $key, $token['name']);
203 drush_print_table($rows, TRUE);
207 * Command callback. Outputs a UUID.
210 * A freshly generated UUID.
212 function drush_devel_generate_uuid() {
214 return $uuid->generate();
218 * Print the specified function, including any
219 * doxygen-style comments that come before it.
221 function _drush_devel_print_function($file, $start_line, $end_line) {
224 $fp = fopen( $file, 'r' );
226 while (!feof($fp) && ($line_num < ($start_line - 1))) {
230 if (substr($line,0,3) == '/**') {
233 elseif (isset($doxygen)) {
235 if ($line_num + 1 == $start_line) {
236 drush_print(rtrim($doxygen));
238 if (strstr($line, '*/') !== FALSE) {
243 while (!feof($fp) && ($line_num < $end_line)) {
246 drush_print(rtrim($line));
251 * Command callback to list available container services.
253 function drush_devel_container_services($prefix = NULL) {
254 $container = Drupal::getContainer();
256 if (empty($container)) {
257 return drush_set_error(dt('No container was found.'));
260 // Get a list of all available service IDs.
261 $services = $container->getServiceIds();
263 // If there is a prefix, try to find matches.
264 if (isset($prefix)) {
265 $services = preg_grep("/$prefix/", $services);
268 if (empty($services)) {
269 return drush_log(dt('No container services found.'), 'ok');