5 * This file is only used by Drush8. Drush9 discovers its commands via tagged
6 * service(s) in devel.services.yml. Also see classes in src/Commands.
9 use Drupal\Component\Uuid\Php;
12 * Implements hook_drush_command().
14 function devel_drush_command() {
15 $items['devel-reinstall'] = array(
16 'description' => dt('Uninstall, and Install a list of projects.'),
17 'drush dependencies' => array('pm'),
19 'projects' => dt('A space-separated list of project names.'),
21 'allow-additional-options' => array('pm-uninstall', 'pm-enable'),
22 'required-arguments' => 1,
23 'aliases' => array('dre'),
25 $items['fn-hook'] = array(
26 'description' => 'List implementations of a given hook and explore the source of the selected one.',
28 'hook' => 'The name of the hook to explore (e.g. "menu" for hook_menu()).'
31 'fn-hook cron' => 'List implementations of hook_cron().',
33 'allow-additional-options' => array('fn-view'),
34 'required-arguments' => 1,
35 'aliases' => array('fnh', 'hook'),
37 $items['fn-event'] = array(
38 'description' => 'List implementations of a given event and explore source of specified one.',
40 'event' => 'The name of the event to explore. If omitted, a list of events is shown.'
43 'fn-event' => 'Pick a Kernel event, then pick an implementation, and then view its source code.',
44 'fn-event kernel.terminate' => 'Pick a terminate subscribers and view its source code.',
46 'allow-additional-options' => array('fn-view'),
47 'aliases' => array('fne', 'event'),
49 $items['fn-view'] = array(
50 'description' => 'Show the source of specified function or method.',
52 'function' => 'The name of the function or method to view.',
55 'pipe' => 'Output just the filename of the function or method',
56 'format' => 'Specify how the filename should be printed. Available placeholders are @startline, @endline and @file',
59 'fn-view drupal_set_breadcrumb' => 'View the source code for function "drupal_set_breadcrumb"',
60 'vi `drush --pipe fn-view user_access --format=\'+@startline @file\'`' => 'Edit the file that contains the function "user_access"',
61 'fn-view NodeController::load' => 'View the source code for method load in the class NodeController'
63 'aliases' => array('fnv'),
64 'required-arguments' => 1,
66 $items['devel-token'] = array(
67 'description' => dt('List available tokens'),
68 'aliases' => array('token'),
69 //@todo support --format option for json, csv, etc.
72 $items['devel-container-services'] = array(
73 'description' => 'Get a list of available container services.',
74 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
75 'core' => array('8+'),
76 'aliases' => array('dcs'),
78 'format' => 'Format to output. Allowed values are: json, export, html.',
81 'prefix' => 'A prefix to filter the service list by.',
84 'drush container-services' => 'Gets a list of all available container services',
85 'drush container-services plugin.manager' => 'Get all services containing "plugin.manager"',
87 'outputformat' => array(
89 'pipe-format' => 'export',
93 $items['devel-generate-uuid'] = array(
94 'description' => 'Generate a UUID.',
95 'core' => array('8+'),
97 "drush devel-generate-uuid" => "Outputs a Universally Unique IDentifier.",
99 'aliases' => array('uuid'),
100 'outputformat' => array(
101 'default' => 'string',
109 * A command callback. This is faster than 3 separate bootstraps.
111 function drush_devel_reinstall() {
112 $projects = func_get_args();
114 $args = array_merge(array('pm-uninstall'), $projects);
115 call_user_func_array('drush_invoke', $args);
117 $args = array_merge(array('pm-enable'), $projects);
118 call_user_func_array('drush_invoke', $args);
122 * Command handler. Show hook implementations.
124 function drush_devel_fn_hook($hook) {
125 // Get implementations in the .install files as well.
126 include_once './core/includes/install.inc';
127 drupal_load_updates();
129 if ($hook_implementations = \Drupal::moduleHandler()->getImplementations($hook)) {
130 if ($choice = drush_choice(array_combine($hook_implementations, $hook_implementations), 'Enter the number of the hook implementation you wish to view.')) {
131 return drush_devel_fn_view($choice . "_$hook");
135 drush_log(dt('No implementations.'), 'ok');
140 * Command handler. Show hook implementations.
142 function drush_devel_fn_event($event = NULL) {
143 $dispatcher = Drupal::service('event_dispatcher');
145 $events = array('kernel.controller', 'kernel.exception', 'kernel.request', 'kernel.response', 'kernel.terminate', 'kernel.view');
146 $events = array_combine($events, $events);
147 if (!$event = drush_choice($events, 'Enter the event you wish to explore.')) {
148 return drush_user_abort();
151 if ($implementations = $dispatcher->getListeners($event)) {
152 foreach ($implementations as $implementation) {
153 $callable = get_class($implementation[0]) . '::' . $implementation[1];
154 $choices[$callable] = $callable;
156 if ($choice = drush_choice($choices, 'Enter the number of the implementation you wish to view.')) {
157 return drush_devel_fn_view($choice);
161 drush_log(dt('No implementations.'), 'ok');
166 * Command handler. Show source code of specified function or method.
168 function drush_devel_fn_view($function_name) {
169 // Get implementations in the .install files as well.
170 include_once './core/includes/install.inc';
171 drupal_load_updates();
173 if (strpos($function_name, '::') === FALSE) {
174 if (!function_exists($function_name)) {
175 return drush_set_error(dt('Function not found'));
177 $reflect = new ReflectionFunction($function_name);
180 list($class, $method) = explode('::', $function_name);
181 if (!method_exists($class, $method)) {
182 return drush_set_error(dt('Method not found'));
184 $reflect = new ReflectionMethod($class, $method);
186 $func_info = array('@file' => $reflect->getFileName(), '@startline' => $reflect->getStartLine(), '@endline' => $reflect->getEndLine());
187 $format = drush_get_option('format', '@file');
188 drush_print_pipe(dt($format, $func_info));
189 drush_print(dt("// file: @file, lines @startline-@endline", $func_info));
191 _drush_devel_print_function($reflect->getFileName(), $reflect->getStartLine(), $reflect->getEndLine());
195 * Command callback. List available tokens.
197 function drush_devel_token() {
198 $rows[] = array(dt('Group'), dt('Token'), dt('Name'));
199 $all = \Drupal::token()->getInfo();
200 foreach ($all['tokens'] as $group => $tokens) {
201 foreach ($tokens as $key => $token) {
202 $rows[] = array($group, $key, $token['name']);
205 drush_print_table($rows, TRUE);
209 * Command callback. Outputs a UUID.
212 * A freshly generated UUID.
214 function drush_devel_generate_uuid() {
216 return $uuid->generate();
220 * Print the specified function, including any
221 * doxygen-style comments that come before it.
223 function _drush_devel_print_function($file, $start_line, $end_line) {
226 $fp = fopen( $file, 'r' );
228 while (!feof($fp) && ($line_num < ($start_line - 1))) {
232 if (substr($line,0,3) == '/**') {
235 elseif (isset($doxygen)) {
237 if ($line_num + 1 == $start_line) {
238 drush_print(rtrim($doxygen));
240 if (strstr($line, '*/') !== FALSE) {
245 while (!feof($fp) && ($line_num < $end_line)) {
248 drush_print(rtrim($line));
253 * Command callback to list available container services.
255 function drush_devel_container_services($prefix = NULL) {
256 $container = Drupal::getContainer();
258 if (empty($container)) {
259 return drush_set_error(dt('No container was found.'));
262 // Get a list of all available service IDs.
263 $services = $container->getServiceIds();
265 // If there is a prefix, try to find matches.
266 if (isset($prefix)) {
267 $services = preg_grep("/$prefix/", $services);
270 if (empty($services)) {
271 return drush_log(dt('No container services found.'), 'ok');