/
PreflightArgs.php
485 lines (423 loc) · 12.3 KB
/
PreflightArgs.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
declare(strict_types=1);
namespace Drush\Preflight;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Consolidation\Config\Config;
use Consolidation\Config\ConfigInterface;
use Drush\Utils\StringUtils;
use Drush\Symfony\LessStrictArgvInput;
/**
* Storage for arguments preprocessed during preflight.
*
* Holds @sitealias, if present, and a limited number of global options.
*
* TODO: The methods here with >~3 lines of logic could be refactored into a couple
* of different classes e.g. a helper to convert preflight args to configuration,
* and another to prepare the input object.
*/
class PreflightArgs extends Config implements PreflightArgsInterface
{
/**
* @var array $args Remaining arguments not handled by the preprocessor
*/
protected $args;
/**
* @var string $homeDir Path to directory to use when replacing ~ in paths
*/
protected $homeDir;
protected $commandName;
public function homeDir(): string
{
return $this->homeDir;
}
public function setHomeDir(string $homeDir): void
{
$this->homeDir = $homeDir;
}
const DRUSH_CONFIG_PATH_NAMESPACE = 'drush.paths';
const DRUSH_RUNTIME_CONTEXT_NAMESPACE = 'runtime.contxt';
const ALIAS = 'alias';
const ALIAS_PATH = 'alias-path';
const COMMAND_PATH = 'include';
const CONFIG_PATH = 'config';
const COVERAGE_FILE = 'coverage-file';
const LOCAL = 'local';
const ROOT = 'root';
const URI = 'uri';
const SIMULATE = 'simulate';
const STRICT = 'strict';
const DEBUG = 'preflight-debug';
/**
* PreflightArgs constructor
*
* @param array $data Initial data (not usually used)
*/
public function __construct($data = [])
{
parent::__construct($data + [self::STRICT => true]);
}
/**
* @inheritdoc
*/
public function optionsWithValues(): array
{
return [
'-r=' => 'setSelectedSite',
'--root=' => 'setSelectedSite',
'--debug' => 'setDebug',
'-d' => 'setDebug',
'-vvv' => 'setDebug',
'-l=' => 'setUri',
'--uri=' => 'setUri',
'-c=' => 'addConfigPath',
'--config=' => 'addConfigPath',
'--alias-path=' => 'addAliasPath',
'--include=' => 'addCommandPath',
'--local' => 'setLocal',
'--simulate' => 'setSimulate',
'-s' => 'setSimulate',
'--drush-coverage=' => 'setCoverageFile',
'--strict=' => 'setStrict',
'--help' => 'adjustHelpOption',
'-h' => 'adjustHelpOption',
];
}
/**
* If the user enters '--help' or '-h', thrown that
* option away and add a 'help' command to the beginning
* of the argument list.
*/
public function adjustHelpOption(): void
{
$drushPath = array_shift($this->args);
array_unshift($this->args, $drushPath, 'help');
}
/**
* Map of option key to the corresponding config key to store the
* preflight option in. The values of the config items in this map
* must be BOOLEANS or STRINGS.
*/
protected function optionConfigMap(): array
{
return [
self::SIMULATE => \Robo\Config\Config::SIMULATE,
self::LOCAL => self::DRUSH_RUNTIME_CONTEXT_NAMESPACE . '.' . self::LOCAL,
];
}
/**
* Map of path option keys to the corresponding config key to store the
* preflight option in. The values of the items in this map must be
* STRINGS or ARRAYS OF STRINGS.
*/
protected function optionConfigPathMap(): array
{
return [
self::ALIAS_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::ALIAS_PATH,
self::CONFIG_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::CONFIG_PATH,
self::COMMAND_PATH => self::DRUSH_CONFIG_PATH_NAMESPACE . '.' . self::COMMAND_PATH,
];
}
/**
* @inheritdoc
*
* @see Environment::exportConfigData(), which also exports information to config.
*/
public function applyToConfig(ConfigInterface $config): void
{
// Copy the relevant preflight options to the applicable configuration namespace
foreach ($this->optionConfigMap() as $option_key => $config_key) {
$config->set($config_key, $this->get($option_key));
}
// Merging as they are lists.
foreach ($this->optionConfigPathMap() as $option_key => $config_key) {
$cli_paths = $this->get($option_key, []);
$config_paths = (array)$config->get($config_key, []);
$merged_paths = array_unique(array_merge($cli_paths, $config_paths));
$config->set($config_key, $merged_paths);
$this->set($option_key, $merged_paths);
}
// Store the runtime arguments and options (sans the runtime context items)
// in runtime.argv et. al.
$config->set('runtime.drush-script', $this->applicationPath());
$config->set('runtime.command', $this->commandName() ?: 'help');
$config->set('runtime.argv', $this->args());
$config->set('runtime.options', $this->getOptionNameList($this->args()));
}
/**
* @inheritdoc
*/
public function args(): array
{
return $this->args;
}
/**
* @inheritdoc
*/
public function applicationPath()
{
return realpath(reset($this->args));
}
/**
* @inheritdoc
*/
public function commandName()
{
return $this->commandName;
}
/**
* @inheritdoc
*/
public function setCommandName($commandName): void
{
$this->commandName = $commandName;
}
/**
* @inheritdoc
*/
public function addArg($arg): self
{
$this->args[] = $arg;
return $this;
}
/**
* @inheritdoc
*/
public function passArgs($args): self
{
$this->args = array_merge($this->args, $args);
return $this;
}
/**
* @inheritdoc
*/
public function alias()
{
return $this->get(self::ALIAS);
}
/**
* @inheritdoc
*/
public function hasAlias(): bool
{
return $this->has(self::ALIAS);
}
/**
* @inheritdoc
*/
public function setAlias($alias): self
{
// Treat `drush @self ...` as if an alias had not been used at all.
if ($alias == '@self') {
$alias = '';
}
return $this->set(self::ALIAS, $alias);
}
/**
* Get the selected site. Here, the default will typically be the cwd.
*/
public function selectedSite($default = false)
{
return $this->get(self::ROOT, $default);
}
public function setDebug($value): void
{
$this->set(self::DEBUG, $value);
$this->addArg('-vvv');
}
/**
* Set the selected site.
*/
public function setSelectedSite($root): self
{
return $this->set(self::ROOT, StringUtils::replaceTilde($root, $this->homeDir()));
}
/**
* Get the selected uri
*/
public function uri($default = false)
{
return $this->get(self::URI, $default);
}
public function hasUri(): bool
{
return $this->has(self::URI);
}
/**
* Set the uri option
*/
public function setUri($uri): self
{
return $this->set(self::URI, $uri);
}
/**
* Get the config path where drush.yml files may be found
*/
public function configPaths()
{
return $this->get(self::CONFIG_PATH, []);
}
/**
* Add another location where drush.yml files may be found
*/
public function addConfigPath(string $path): self
{
$paths = $this->configPaths();
$paths[] = StringUtils::replaceTilde($path, $this->homeDir());
return $this->set(self::CONFIG_PATH, $paths);
}
/**
* Add multiple additional locations where drush.yml files may be found.
*
* @param string[] $configPaths
*/
public function mergeConfigPaths(array $configPaths): self
{
$paths = $this->configPaths();
$merged_paths = array_merge($paths, $configPaths);
return $this->set(self::CONFIG_PATH, $merged_paths);
}
/**
* Get the alias paths where drush site.site.yml files may be found
*/
public function aliasPaths()
{
return $this->get(self::ALIAS_PATH, []);
}
/**
* Set one more path where aliases may be found.
*/
public function addAliasPath(string $path): self
{
$paths = $this->aliasPaths();
$paths[] = StringUtils::replaceTilde($path, $this->homeDir());
return $this->set(self::ALIAS_PATH, $paths);
}
/**
* Get the path where Drush commandfiles e.g. FooCommands.php may be found.
*/
public function commandPaths()
{
return $this->get(self::COMMAND_PATH, []);
}
/**
* Add one more path where commandfiles might be found.
*/
public function addCommandPath(string $path): self
{
$paths = $this->commandPaths();
$paths[] = StringUtils::replaceTilde($path, $this->homeDir());
return $this->set(self::COMMAND_PATH, $paths);
}
/**
* Add multiple paths where commandfiles might be found.
*
* @param $commanPaths
*/
public function mergeCommandPaths($commandPaths): self
{
$paths = $this->commandPaths();
$merged_paths = array_merge($paths, $commandPaths);
return $this->set(self::COMMAND_PATH, $merged_paths);
}
/**
* Determine whether Drush is in "local" mode
*/
public function isLocal()
{
return $this->get(self::LOCAL, false);
}
/**
* Set local mode
*/
public function setLocal(bool $isLocal): self
{
return $this->set(self::LOCAL, $isLocal);
}
/**
* Determine whether Drush is in "simulated" mode.
*/
public function isSimulated()
{
return $this->get(self::SIMULATE);
}
/**
* Set simulated mode
*
* @param bool $simulated
*/
public function setSimulate($simulate): self
{
return $this->set(self::SIMULATE, $simulate);
}
/**
* Get the path to the coverage file.
*/
public function coverageFile()
{
return $this->get(self::COVERAGE_FILE);
}
/**
* Set the coverage file path.
*
* @param string
*/
public function setCoverageFile($coverageFile): self
{
return $this->set(self::COVERAGE_FILE, StringUtils::replaceTilde($coverageFile, $this->homeDir()));
}
/**
* Determine whether Drush is in "strict" mode or not.
*/
public function isStrict()
{
return $this->get(self::STRICT);
}
/**
* Set strict mode.
*/
public function setStrict(bool $strict): self
{
return $this->set(self::STRICT, $strict);
}
/**
* Search through the provided argv list, and return
* just the option name of any item that is an option.
*
* @param array $argv e.g. ['foo', '--bar=baz', 'boz']
*
* @return string[] e.g. ['bar']
*/
protected function getOptionNameList(array $argv): array
{
return array_filter(
array_map(
function ($item) {
// Ignore configuration definitions
if (str_starts_with($item, '-D')) {
return null;
}
// Regular expression matches:
// ^-+ # anything that begins with one or more '-'
// ([^= ]*) # any number of characters up to the first = or space
if (preg_match('#^-+([^= ]*)#', $item, $matches)) {
return $matches[1];
}
},
$argv
)
);
}
/**
* Create a Symfony Input object.
*/
public function createInput(): InputInterface
{
// In strict mode (the default), create an ArgvInput. When
// strict mode is disabled, create a more forgiving input object.
if ($this->isStrict()) {
return new ArgvInput($this->args());
}
return new LessStrictArgvInput($this->args());
}
}