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