-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathPreflight.php
358 lines (312 loc) · 12.1 KB
/
Preflight.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
<?php
namespace Drush\Preflight;
use Drush\Config\Environment;
use Drush\Config\ConfigLocator;
use Drush\Config\EnvironmentConfigLoader;
use Drush\SiteAlias\SiteAliasManager;
use DrupalFinder\DrupalFinder;
/**
* The Drush preflight determines what needs to be done for this request.
* The preflight happens after Drush has loaded its autoload file, but
* prior to loading Drupal's autoload file and setting up the DI container.
*
* - Pre-parse commandline arguments
* - Read configuration .yml files
* - Determine the site to use
*/
class Preflight
{
/**
* @var Environment $environment
*/
protected $environment;
/**
* @var PreflightVerify
*/
protected $verify;
/**
* @var ConfigLocator
*/
protected $configLocator;
/**
* @var DrupalFinder
*/
protected $drupalFinder;
/**
* @var PreflightArgs
*/
protected $preflightArgs;
/**
* @var SiteAliasManager
*/
protected $aliasManager;
/**
* @var PreflightLog $logger An early logger, just for Preflight.
*/
protected $logger;
/**
* Preflight constructor
*/
public function __construct(Environment $environment, $verify = null, $configLocator = null)
{
$this->environment = $environment;
$this->verify = $verify ?: new PreflightVerify();
$this->configLocator = $configLocator ?: new ConfigLocator('DRUSH_');
$this->drupalFinder = new DrupalFinder();
$this->logger = new PreflightLog();
}
/**
* @return PreflightLog
*/
public function logger()
{
return $this->logger;
}
/**
* @param PreflightLog $logger
*/
public function setLogger(PreflightLog $logger)
{
$this->logger = $logger;
}
/**
* Perform preliminary initialization. This mostly involves setting up
* legacy systems.
*/
public function init()
{
// Define legacy constants, and include legacy files that Drush still needs
LegacyPreflight::includeCode($this->environment->drushBasePath());
LegacyPreflight::defineConstants($this->environment, $this->preflightArgs->applicationPath());
LegacyPreflight::setContexts($this->environment);
}
/**
* Remapping table for arguments. Anything found in a key
* here will be converted to the corresponding value entry.
*
* For example:
* --ssh-options='-i mysite_dsa'
* will become:
* -Dssh.options='-i mysite_dsa'
*
* TODO: We could consider loading this from a file or some other
* source. However, this table is needed very early -- even earlier
* than config is loaded (since this is needed for preflighting the
* arguments, which can select config files to load). Hardcoding
* is probably best; we might want to move to another class, perhaps.
* We also need this prior to Dependency Injection, though.
*
* Eventually, we might want to expose this table to some form of
* 'help' output, so folks can see the available conversions.
*/
protected function remapOptions()
{
return [
'--ssh-options' => '-Dssh.options',
'--php' => '-Druntime.php.path',
'--php-options' => '-Druntime.php.options',
'--php-notices' => '-Druntime.php.notices',
'--halt-on-error' => '-Druntime.php.halt-on-error',
'--output_charset' => '-Dio.output.charset',
'--output-charset' => '-Dio.output.charset',
'--db-su' => '-Dsql.db-su',
'--notify' => '-Dnotify.duration',
'--xh-link' => '-Dxh.link',
];
}
/**
* Symfony Console dislikes certain command aliases, because
* they are too similar to other Drush commands that contain
* the same characters. To avoid the "I don't know which
* command you mean"-type errors, we will replace problematic
* aliases with their longhand equivalents.
*
* This should be fixed in Symfony Console.
*/
protected function remapCommandAliases()
{
return [
'si' => 'site:install',
'en' => 'pm:enable',
// php was an alias for core-cli which got renamed to php-cli. See https://github.com/drush-ops/drush/issues/3091.
'php' => 'php:cli',
];
}
/**
* Preprocess the args, removing any @sitealias that may be present.
* Arguments and options not used during preflight will be processed
* with an ArgvInput.
*/
public function preflightArgs($argv)
{
$argProcessor = new ArgsPreprocessor();
$remapper = new ArgsRemapper($this->remapOptions(), $this->remapCommandAliases());
$preflightArgs = new PreflightArgs();
$preflightArgs->setHomeDir($this->environment()->homeDir());
$argProcessor->setArgsRemapper($remapper);
$argProcessor->parse($argv, $preflightArgs);
return $preflightArgs;
}
/**
* Create the initial config locator object, and inject any needed
* settings, paths and so on into it.
*/
public function prepareConfig(Environment $environment)
{
// Make our environment settings available as configuration items
$this->configLocator->addEnvironment($environment);
$this->configLocator->setLocal($this->preflightArgs->isLocal());
$this->configLocator->addUserConfig($this->preflightArgs->configPaths(), $environment->systemConfigPath(), $environment->userConfigPath());
$this->configLocator->addDrushConfig($environment->drushBasePath());
}
/**
* Start code coverage collection
*/
public function startCoverage()
{
if ($coverage_file = $this->preflightArgs->coverageFile()) {
// TODO: modernize code coverage handling
drush_set_context('DRUSH_CODE_COVERAGE', $coverage_file);
xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
register_shutdown_function('drush_coverage_shutdown');
}
}
public function createInput()
{
return $this->preflightArgs->createInput();
}
public function getCommandFilePaths()
{
// Find all of the available commandfiles, save for those that are
// provided by modules in the selected site; those will be added
// during bootstrap.
return $this->configLocator->getCommandFilePaths($this->preflightArgs->commandPaths(), $this->drupalFinder()->getDrupalRoot());
}
public function loadSiteAutoloader()
{
return $this->environment()->loadSiteAutoloader($this->drupalFinder()->getDrupalRoot());
}
public function config()
{
return $this->configLocator->config();
}
/**
* @param $argv
* @return bool
* True if the request was successfully redispatched remotely. False if the request should proceed.
*/
public function preflight($argv)
{
// Fail fast if there is anything in our environment that does not check out
$this->verify->verify($this->environment);
// Get the preflight args and begin collecting configuration files.
$this->preflightArgs = $this->preflightArgs($argv);
$this->prepareConfig($this->environment);
// Now that we know the value, set debug flag.
$this->logger()->setDebug($this->preflightArgs->get(PreflightArgs::DEBUG));
// Do legacy initialization (load static includes, define old constants, etc.)
$this->init();
// Start code coverage
$this->startCoverage();
// Get the config files provided by prepareConfig()
$config = $this->config();
// Copy items from the preflight args into configuration.
// This will also load certain config values into the preflight args.
$this->preflightArgs->applyToConfig($config);
// Determine the local site targeted, if any.
// Extend configuration and alias files to include files in
// target site.
$root = $this->findSelectedSite();
$this->configLocator->addSitewideConfig($root);
$this->configLocator->setComposerRoot($this->drupalFinder()->getComposerRoot());
// Look up the locations where alias files may be found.
$paths = $this->configLocator->getSiteAliasPaths($this->preflightArgs->aliasPaths(), $this->environment);
// Configure alias manager.
$this->aliasManager = (new SiteAliasManager())->addSearchLocations($paths);
$this->aliasManager->setReferenceData($config->export());
$selfAliasRecord = $this->aliasManager->findSelf($this->preflightArgs, $this->environment, $root);
$this->configLocator->addAliasConfig($selfAliasRecord->exportConfig());
// Process the selected alias. This might change the selected site,
// so we will add new site-wide config location for the new root.
$root = $this->setSelectedSite($selfAliasRecord->localRoot());
// Now that we have our final Drupal root, check to see if there is
// a site-local Drush. If there is, we will redispatch to it.
// NOTE: termination handlers have not been set yet, so it is okay
// to exit early without taking special action.
$status = RedispatchToSiteLocal::redispatchIfSiteLocalDrush($argv, $root, $this->environment->vendorPath(), $this->logger()) ;
if ($status !== false) {
return $status;
}
// If we did not redispatch, then add the site-wide config for the
// new root (if the root did in fact change) and continue.
$this->configLocator->addSitewideConfig($root);
// Remember the paths to all the files we loaded, so that we can
// report on it from Drush status or wherever else it may be needed.
$configFilePaths = $this->configLocator->configFilePaths();
$config->set('runtime.config.paths', $configFilePaths);
$this->logger()->log(dt('Config paths: ' . implode(',', $configFilePaths)));
$this->logger()->log(dt('Alias paths: ' . implode(',', $paths)));
// We need to check the php minimum version again, in case anyone
// has set it to something higher in one of the config files we loaded.
$this->verify->confirmPhpVersion($config->get('drush.php.minimum-version'));
return false;
}
/**
* Find the site the user selected based on --root or cwd. If neither of
* those result in a site, then we will fall back to the vendor path.
*/
protected function findSelectedSite()
{
// TODO: If we want to support ONLY site-local Drush (which is
// DIFFERENT than --local), then skip the call to `$preflightArgs->selectedSite`
// and just assign `false` to $selectedRoot.
// Try two approaches.
$selectedRoot = $this->preflightArgs->selectedSite($this->environment->cwd());
$fallBackPath = $this->preflightArgs->selectedSite(DRUSH_COMMAND);
return $this->setSelectedSite($selectedRoot, $fallBackPath);
}
/**
* Use the DrupalFinder to locate the Drupal Root + Composer Root at
* the selected root, or, if nothing is found there, at a fallback path.
*
* @param string $selectedRoot The location to being searching for a site
* @param string|bool $fallbackPath The secondary location to search (usualy the vendor director)
*/
protected function setSelectedSite($selectedRoot, $fallbackPath = false)
{
if ($selectedRoot || $fallbackPath) {
$foundRoot = $this->drupalFinder->locateRoot($selectedRoot);
if (!$foundRoot && $fallbackPath) {
$this->drupalFinder->locateRoot($fallbackPath);
}
return $this->drupalFinder()->getDrupalRoot();
}
}
/**
* Return the Drupal Finder
*
* @return DrupalFinder
*/
public function drupalFinder()
{
return $this->drupalFinder;
}
/**
* Return the alias manager
*
* @return SiteAliasManager
*/
public function aliasManager()
{
return $this->aliasManager;
}
/**
* Return the environment
*
* @return Environment
*/
public function environment()
{
return $this->environment;
}
}