forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.php
364 lines (320 loc) · 13.4 KB
/
Application.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
namespace Drush;
use Consolidation\AnnotatedCommand\AnnotatedCommand;
use Consolidation\AnnotatedCommand\CommandFileDiscovery;
use Drush\Boot\BootstrapManager;
use Drush\Runtime\TildeExpansionHook;
use Consolidation\SiteAlias\SiteAliasManager;
use Drush\Log\LogLevel;
use Drush\Command\RemoteCommandProxy;
use Drush\Runtime\RedispatchHook;
use Robo\Common\ConfigAwareTrait;
use Robo\Contract\ConfigAwareInterface;
use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
/**
* Our application object
*
* Note: Implementing *AwareInterface here does NOT automatically cause
* that corresponding service to be injected into the Application. This
* is because the application object is created prior to the DI container.
* See DependencyInjection::injectApplicationServices() to add more services.
*/
class Application extends SymfonyApplication implements LoggerAwareInterface, ConfigAwareInterface
{
use LoggerAwareTrait;
use ConfigAwareTrait;
/** @var BootstrapManager */
protected $bootstrapManager;
/** @var SiteAliasManager */
protected $aliasManager;
/** @var RedispatchHook */
protected $redispatchHook;
/** @var TildeExpansionHook */
protected $tildeExpansionHook;
/**
* Add global options to the Application and their default values to Config.
*/
public function configureGlobalOptions()
{
$this->getDefinition()
->addOption(
new InputOption('--debug', 'd', InputOption::VALUE_NONE, 'Equivalent to -vv')
);
$this->getDefinition()
->addOption(
new InputOption('--yes', 'y', InputOption::VALUE_NONE, 'Equivalent to --no-interaction.')
);
// Note that -n belongs to Symfony Console's --no-interaction.
$this->getDefinition()
->addOption(
new InputOption('--no', null, InputOption::VALUE_NONE, 'Cancels at any confirmation prompt.')
);
$this->getDefinition()
->addOption(
new InputOption('--remote-host', null, InputOption::VALUE_REQUIRED, 'Run on a remote server.')
);
$this->getDefinition()
->addOption(
new InputOption('--remote-user', null, InputOption::VALUE_REQUIRED, 'The user to use in remote execution.')
);
$this->getDefinition()
->addOption(
new InputOption('--root', '-r', InputOption::VALUE_REQUIRED, 'The Drupal root for this site.')
);
$this->getDefinition()
->addOption(
new InputOption('--uri', '-l', InputOption::VALUE_REQUIRED, 'Which multisite from the selected root to use.')
);
$this->getDefinition()
->addOption(
new InputOption('--simulate', null, InputOption::VALUE_NONE, 'Run in simulated mode (show what would have happened).')
);
// TODO: Implement handling for 'pipe'
$this->getDefinition()
->addOption(
new InputOption('--pipe', null, InputOption::VALUE_NONE, 'Select the canonical script-friendly output format.')
);
$this->getDefinition()
->addOption(
new InputOption('--define', '-D', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Define a configuration item value.', [])
);
}
public function bootstrapManager()
{
return $this->bootstrapManager;
}
public function setBootstrapManager(BootstrapManager $bootstrapManager)
{
$this->bootstrapManager = $bootstrapManager;
}
public function aliasManager()
{
return $this->aliasManager;
}
public function setAliasManager($aliasManager)
{
$this->aliasManager = $aliasManager;
}
public function setRedispatchHook(RedispatchHook $redispatchHook)
{
$this->redispatchHook = $redispatchHook;
}
public function setTildeExpansionHook(TildeExpansionHook $tildeExpansionHook)
{
$this->tildeExpansionHook = $tildeExpansionHook;
}
/**
* Return the framework uri selected by the user.
*/
public function getUri()
{
if (!$this->bootstrapManager) {
return 'default';
}
return $this->bootstrapManager->getUri();
}
/**
* If the user did not explicitly select a site URI,
* then pick an appropriate site from the cwd.
*/
public function refineUriSelection($cwd)
{
if (!$this->bootstrapManager || !$this->aliasManager) {
return;
}
$selfAliasRecord = $this->aliasManager->getSelf();
if (!$selfAliasRecord->hasRoot() && !$this->bootstrapManager()->drupalFinder()->getDrupalRoot()) {
return;
}
$uri = $selfAliasRecord->uri();
if (empty($uri)) {
$uri = $this->selectUri($cwd);
$selfAliasRecord->setUri($uri);
$this->aliasManager->setSelf($selfAliasRecord);
}
// Update the uri in the bootstrap manager
$this->bootstrapManager->setUri($uri);
}
/**
* Select a URI to use for the site, based on directory or config.
*/
public function selectUri($cwd)
{
$uri = $this->config->get('options.uri');
if ($uri) {
return $uri;
}
$uri = $this->bootstrapManager()->selectUri($cwd);
return $uri;
}
/**
* @inheritdoc
*/
public function find($name)
{
if (empty($name)) {
return;
}
$command = $this->bootstrapAndFind($name);
// Avoid exception when help is being built by https://github.com/bamarni/symfony-console-autocomplete.
// @todo Find a cleaner solution.
if (Drush::config()->get('runtime.argv')[1] !== 'help') {
$this->checkObsolete($command);
}
return $command;
}
/**
* Look up a command. Bootstrap further if necessary.
*/
protected function bootstrapAndFind($name)
{
try {
return parent::find($name);
} catch (CommandNotFoundException $e) {
// Is the unknown command destined for a remote site?
if ($this->aliasManager) {
$selfAlias = $this->aliasManager->getSelf();
if ($selfAlias->isRemote()) {
$command = new RemoteCommandProxy($name, $this->redispatchHook);
$command->setApplication($this);
return $command;
}
}
// If we have no bootstrap manager, then just re-throw
// the exception.
if (!$this->bootstrapManager) {
throw $e;
}
$this->logger->log(LogLevel::DEBUG, 'Bootstrap further to find {command}', ['command' => $name]);
$this->bootstrapManager->bootstrapMax();
$this->logger->log(LogLevel::DEBUG, 'Done with bootstrap max in Application::find(): trying to find {command} again.', ['command' => $name]);
if (!$this->bootstrapManager()->hasBootstrapped(DRUSH_BOOTSTRAP_DRUPAL_ROOT)) {
// Unable to progress in the bootstrap. Give friendly error message.
throw new CommandNotFoundException(dt('Command !command was not found. Pass --root or a @siteAlias in order to run Drupal-specific commands.', ['!command' => $name]));
}
// Try to find it again, now that we bootstrapped as far as possible.
try {
return parent::find($name);
} catch (CommandNotFoundException $e) {
if (!$this->bootstrapManager()->hasBootstrapped(DRUSH_BOOTSTRAP_DRUPAL_DATABASE)) {
// Unable to bootstrap to DB. Give targetted error message.
throw new CommandNotFoundException(dt('Command !command was not found. Drush was unable to query the database. As a result, many commands are unavailable. Re-run your command with --debug to see relevant log messages.', ['!command' => $name]));
}
if (!$this->bootstrapManager()->hasBootstrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
// Unable to fully bootstrap. Give targetted error message.
throw new CommandNotFoundException(dt('Command !command was not found. Drush successfully connected to the database but was unable to fully bootstrap your site. As a result, many commands are unavailable. Re-run your command with --debug to see relevant log messages.', ['!command' => $name]));
} else {
// We fully bootstrapped but still could not find command. Rethrow.
throw $e;
}
}
}
}
/**
* If a command is annotated @obsolete, then we will throw an exception
* immediately; the command will not run, and no hooks will be called.
*/
protected function checkObsolete($command)
{
if (!$command instanceof AnnotatedCommand) {
return;
}
$annotationData = $command->getAnnotationData();
if (!$annotationData->has('obsolete')) {
return;
}
$obsoleteMessage = $command->getDescription();
throw new \Exception($obsoleteMessage);
}
/**
* @inheritdoc
*
* Note: This method is called twice, as we wish to configure the IO
* objects earlier than Symfony does. We could define a boolean class
* field to record when this method is called, and do nothing on the
* second call. At the moment, the work done here is trivial, so we let
* it happen twice.
*/
protected function configureIO(InputInterface $input, OutputInterface $output)
{
// Do default Symfony confguration.
parent::configureIO($input, $output);
// Process legacy Drush global options.
// Note that `getParameterOption` returns the VALUE of the option if
// it is found, or NULL if it finds an option with no value.
if ($input->getParameterOption(['--yes', '-y', '--no', '-n'], false, true) !== false) {
$input->setInteractive(false);
}
// Symfony will set these later, but we want it set upfront
if ($input->getParameterOption(['--verbose', '-v'], false, true) !== false) {
$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
}
// We are not using "very verbose", but set this for completeness
if ($input->getParameterOption(['-vv'], false, true) !== false) {
$output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
}
// Use -vvv of --debug for even more verbose logging.
if ($input->getParameterOption(['--debug', '-d', '-vvv'], false, true) !== false) {
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
}
}
/**
* Configure the application object and register all of the commandfiles
* available in the search paths provided via Preflight
*/
public function configureAndRegisterCommands(InputInterface $input, OutputInterface $output, $commandfileSearchpath)
{
// Symfony will call this method for us in run() (it will be
// called again), but we want to call it up-front, here, so that
// our $input and $output objects have been appropriately
// configured in case we wish to use them (e.g. for logging) in
// any of the configuration steps we do here.
$this->configureIO($input, $output);
$discovery = $this->commandDiscovery();
$commandClasses = $discovery->discover($commandfileSearchpath, '\Drush');
$commandClasses[] = \Consolidation\Filter\Hooks\FilterHooks::class;
$this->loadCommandClasses($commandClasses);
// Uncomment the lines below to use Console's built in help and list commands.
// unset($commandClasses[__DIR__ . '/Commands/help/HelpCommands.php']);
// unset($commandClasses[__DIR__ . '/Commands/help/ListCommands.php']);
// Use the robo runner to register commands with Symfony application.
// This method could / should be refactored in Robo so that we can use
// it without creating a Runner object that we would not otherwise need.
$runner = new \Robo\Runner();
$runner->registerCommandClasses($this, $commandClasses);
}
/**
* Ensure that any discovered class that is not part of the autoloader
* is, in fact, included.
*/
protected function loadCommandClasses($commandClasses)
{
foreach ($commandClasses as $file => $commandClass) {
if (!class_exists($commandClass)) {
include $file;
}
}
}
/**
* Create a command file discovery object
*/
protected function commandDiscovery()
{
$discovery = new CommandFileDiscovery();
$discovery
->setIncludeFilesAtBase(true)
->setSearchDepth(3)
->ignoreNamespacePart('contrib', 'Commands')
->ignoreNamespacePart('custom', 'Commands')
->ignoreNamespacePart('src')
->setSearchLocations(['Commands', 'Hooks', 'Generators'])
->setSearchPattern('#.*(Command|Hook|Generator)s?.php$#');
return $discovery;
}
}