-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
cli.drush.inc
210 lines (193 loc) · 5.65 KB
/
cli.drush.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
use Drupal\Component\Assertion\Handle;
use Drush\Psysh\DrushHelpCommand;
use Drush\Psysh\DrushCommand;
use Drush\Psysh\Shell;
/**
* Implements hook_drush_command().
*/
function cli_drush_command() {
$items['core-cli'] = array(
'description' => 'Open an interactive shell on a Drupal site.',
'remote-tty' => TRUE,
'aliases' => array('php'),
'bootstrap' => DRUSH_BOOTSTRAP_MAX,
'topics' => array('docs-repl'),
);
$items['docs-repl'] = array(
'description' => 'repl.md',
'hidden' => TRUE,
'topic' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_NONE,
'callback' => 'drush_print_file',
'callback arguments' => array(drush_get_context('DOC_PREFIX', DRUSH_BASE_PATH) . '/docs/repl.md'),
);
return $items;
}
/**
* Command callback.
*/
function drush_cli_core_cli() {
$configuration = new \Psy\Configuration();
$shell = new Shell($configuration);
if (drush_drupal_major_version() >= 8) {
// Register the assertion handler so exceptions are thrown instead of errors
// being triggered. This plays nicer with PsySH.
Handle::register();
$shell->setScopeVariables(['container' => \Drupal::getContainer()]);
// Add Drupal 8 specific casters to the shell configuration.
$configuration->addCasters(_drush_core_cli_get_casters());
}
// Add Drush commands to the shell.
$commands = [new DrushHelpCommand()];
foreach (drush_commands_categorize(_drush_core_cli_get_commands()) as $category_data) {
$category_title = (string) $category_data['title'];
foreach ($category_data['commands'] as $command_config) {
$command = new DrushCommand($command_config);
// Set the category label on each.
$command->setCategory($category_title);
$commands[] = $command;
}
}
$shell->addCommands($commands);
// PsySH will never return control to us, but our shutdown handler will still
// run after the user presses ^D. Mark this command as completed to avoid a
// spurious error message.
drush_set_context('DRUSH_EXECUTION_COMPLETED', TRUE);
// Run the terminate event before the shell is run. Otherwise, if the shell
// is forking processes (the default), any child processes will close the
// database connection when they are killed. So when we return back to the
// parent process after, there is no connection. This will be called after the
// command in preflight still, but the subscriber instances are already
// created from before. Call terminate() regardless, this is a no-op for all
// DrupalBoot classes except DrupalBoot8.
if ($bootstrap = drush_get_bootstrap_object()) {
$bootstrap->terminate();
}
// To fix the above problem in Drupal 7, the connection can be closed manually.
// This will make sure a new connection is created again in child loops. So
// any shutdown functions will still run ok after the shell has exited.
if (drush_drupal_major_version() == 7) {
Database::closeConnection();
}
$shell->run();
}
/**
* Returns a filtered list of Drush commands used for CLI commands.
*
* @return array
*/
function _drush_core_cli_get_commands() {
$commands = drush_get_commands();
$ignored_commands = ['help', 'drush-psysh', 'php-eval', 'core-cli', 'php'];
$php_keywords = _drush_core_cli_get_php_keywords();
foreach ($commands as $name => $config) {
// Ignore some commands that don't make sense inside PsySH, are PHP keywords
// are hidden, or are aliases.
if (in_array($name, $ignored_commands) || in_array($name, $php_keywords) || !empty($config['hidden']) || ($name !== $config['command'])) {
unset($commands[$name]);
}
else {
// Make sure the command aliases don't contain any PHP keywords.
if (!empty($config['aliases'])) {
$commands[$name]['aliases'] = array_diff($commands[$name]['aliases'], $php_keywords);
}
}
}
return $commands;
}
/**
* Returns a mapped array of casters for use in the shell.
*
* These are Symfony VarDumper casters.
* See http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
* for more information.
*
* @return array.
* An array of caster callbacks keyed by class or interface.
*/
function _drush_core_cli_get_casters() {
return [
'Drupal\Core\Entity\ContentEntityInterface' => 'Drush\Psysh\Caster::castContentEntity',
'Drupal\Core\Field\FieldItemListInterface' => 'Drush\Psysh\Caster::castFieldItemList',
'Drupal\Core\Field\FieldItemInterface' => 'Drush\Psysh\Caster::castFieldItem',
'Drupal\Core\Config\Entity\ConfigEntityInterface' => 'Drush\Psysh\Caster::castConfigEntity',
'Drupal\Core\Config\ConfigBase' => 'Drush\Psysh\Caster::castConfig',
];
}
/**
* Returns a list of PHP keywords.
*
* This will act as a blacklist for command and alias names.
*
* @return array
*/
function _drush_core_cli_get_php_keywords() {
return [
'__halt_compiler',
'abstract',
'and',
'array',
'as',
'break',
'callable',
'case',
'catch',
'class',
'clone',
'const',
'continue',
'declare',
'default',
'die',
'do',
'echo',
'else',
'elseif',
'empty',
'enddeclare',
'endfor',
'endforeach',
'endif',
'endswitch',
'endwhile',
'eval',
'exit',
'extends',
'final',
'for',
'foreach',
'function',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'isset',
'list',
'namespace',
'new',
'or',
'print',
'private',
'protected',
'public',
'require',
'require_once',
'return',
'static',
'switch',
'throw',
'trait',
'try',
'unset',
'use',
'var',
'while',
'xor',
];
}