Version 1
[yaffs-website] / web / modules / contrib / devel / drush / devel.drush.inc
1 <?php
2
3 /**
4  * @file
5  * Drush integration for the devel module.
6  */
7
8 use Drupal\Component\Uuid\Php;
9
10 /**
11  * Implements hook_drush_command().
12  */
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'),
17     'arguments' => array(
18       'projects' => dt('A space-separated list of project names.'),
19     ),
20     'allow-additional-options' => array('pm-uninstall', 'pm-enable'),
21     'required-arguments' => 1,
22     'aliases' => array('dre'),
23   );
24   $items['fn-hook'] = array(
25     'description' => 'List implementations of a given hook and explore the source of the selected one.',
26     'arguments' => array(
27       'hook' => 'The name of the hook to explore (e.g. "menu" for hook_menu()).'
28     ),
29     'examples' => array(
30       'fn-hook cron' => 'List implementations of hook_cron().',
31     ),
32     'allow-additional-options' => array('fn-view'),
33     'required-arguments' => 1,
34     'aliases' => array('fnh', 'hook'),
35   );
36   $items['fn-event'] = array(
37     'description' => 'List implementations of a given event and explore source of specified one.',
38     'arguments' => array(
39       'event' => 'The name of the event to explore. If omitted, a list of events is shown.'
40     ),
41     'examples' => array(
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.',
44     ),
45     'allow-additional-options' => array('fn-view'),
46     'aliases' => array('fne', 'event'),
47   );
48   $items['fn-view'] = array(
49     'description' => 'Show the source of specified function or method.',
50     'arguments' => array(
51       'function' => 'The name of the function or method to view.',
52     ),
53     'options' => array(
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',
56     ),
57     'examples' => array(
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'
61     ),
62     'aliases' => array('fnv'),
63     'required-arguments' => 1,
64   );
65   $items['devel-token'] = array(
66     'description' => dt('List available tokens'),
67     'aliases' => array('token'),
68     //@todo support --format option for json, csv, etc.
69   );
70
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'),
76     'options' => array(
77       'format' => 'Format to output. Allowed values are: json, export, html.',
78     ),
79     'arguments' => array(
80       'prefix' => 'A prefix to filter the service list by.',
81     ),
82     'examples' => array(
83       'drush container-services' => 'Gets a list of all available container services',
84       'drush container-services plugin.manager' => 'Get all services containing "plugin.manager"',
85     ),
86     'outputformat' => array(
87       'default' => 'list',
88       'pipe-format' => 'export',
89     ),
90   );
91
92   $items['devel-generate-uuid'] = array(
93     'description' => 'Generate a UUID.',
94     'core' => array('8+'),
95     'examples' => array(
96       "drush devel-generate-uuid" => "Outputs a Universally Unique IDentifier.",
97     ),
98     'aliases' => array('uuid'),
99     'outputformat' => array(
100       'default' => 'string',
101     ),
102   );
103
104   return $items;
105 }
106
107 /**
108  * A command callback. This is faster than 3 separate bootstraps.
109  */
110 function drush_devel_reinstall() {
111   $projects = func_get_args();
112
113   $args = array_merge(array('pm-uninstall'), $projects);
114   call_user_func_array('drush_invoke', $args);
115
116   $args = array_merge(array('pm-enable'), $projects);
117   call_user_func_array('drush_invoke', $args);
118 }
119
120 /**
121  * Command handler. Show hook implementations.
122  */
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();
127
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");
131     }
132   }
133   else {
134     drush_log(dt('No implementations.'), 'ok');
135   }
136 }
137
138 /**
139  * Command handler. Show hook implementations.
140  */
141 function drush_devel_fn_event($event = NULL) {
142   $dispatcher = Drupal::service('event_dispatcher');
143   if (empty($event)) {
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();
148     }
149   }
150   if ($implementations = $dispatcher->getListeners($event)) {
151     foreach ($implementations as $implementation) {
152       $callable = get_class($implementation[0]) . '::' . $implementation[1];
153       $choices[$callable] = $callable;
154     }
155     if ($choice = drush_choice($choices, 'Enter the number of the implementation you wish to view.')) {
156       return drush_devel_fn_view($choice);
157     }
158   }
159   else {
160     drush_log(dt('No implementations.'), 'ok');
161   }
162 }
163 /**
164  * Command handler.  Show source code of specified function or method.
165  */
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();
170
171   if (strpos($function_name, '::') === FALSE) {
172     if (!function_exists($function_name)) {
173       return drush_set_error(dt('Function not found'));
174     }
175     $reflect = new ReflectionFunction($function_name);
176   }
177   else {
178     list($class, $method) = explode('::', $function_name);
179     if (!method_exists($class, $method)) {
180       return drush_set_error(dt('Method not found'));
181     }
182     $reflect = new ReflectionMethod($class, $method);
183   }
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));
188
189   _drush_devel_print_function($reflect->getFileName(), $reflect->getStartLine(), $reflect->getEndLine());
190 }
191
192 /**
193  * Command callback. List available tokens.
194  */
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']);
201     }
202   }
203   drush_print_table($rows, TRUE);
204 }
205
206 /**
207  * Command callback. Outputs a UUID.
208  *
209  * @return string
210  *   A freshly generated UUID.
211  */
212 function drush_devel_generate_uuid() {
213   $uuid = new Php();
214   return $uuid->generate();
215 }
216
217 /**
218  * Print the specified function, including any
219  * doxygen-style comments that come before it.
220  */
221 function _drush_devel_print_function($file, $start_line, $end_line) {
222   $line_num = 0;
223   $doxygen = NULL;
224   $fp = fopen( $file, 'r' );
225
226   while (!feof($fp) && ($line_num < ($start_line - 1))) {
227     $line = fgets($fp);
228     ++$line_num;
229
230     if (substr($line,0,3) == '/**') {
231       $doxygen = $line;
232     }
233     elseif (isset($doxygen)) {
234       $doxygen .= $line;
235       if ($line_num + 1 == $start_line) {
236         drush_print(rtrim($doxygen));
237       }
238       if (strstr($line, '*/') !== FALSE) {
239         $doxygen = NULL;
240       }
241     }
242   }
243   while (!feof($fp) && ($line_num < $end_line)) {
244     $line = fgets($fp);
245     ++$line_num;
246     drush_print(rtrim($line));
247   }
248 }
249
250 /**
251  * Command callback to list available container services.
252  */
253 function drush_devel_container_services($prefix = NULL) {
254   $container = Drupal::getContainer();
255
256   if (empty($container)) {
257     return drush_set_error(dt('No container was found.'));
258   }
259
260   // Get a list of all available service IDs.
261   $services = $container->getServiceIds();
262
263   // If there is a prefix, try to find matches.
264   if (isset($prefix)) {
265     $services = preg_grep("/$prefix/", $services);
266   }
267
268   if (empty($services)) {
269     return drush_log(dt('No container services found.'), 'ok');
270   }
271
272   sort($services);
273   return $services;
274 }