|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\SiteProcess\Util; |
|
3
|
|
|
|
|
4
|
|
|
use Consolidation\SiteAlias\AliasRecord; |
|
5
|
|
|
use Symfony\Component\Process\Process; |
|
6
|
|
|
use Consolidation\SiteProcess\Transport\TransportInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* ArgumentProcessor takes a set of arguments and options from the caller |
|
10
|
|
|
* and processes them with the provided site alias to produce a final |
|
11
|
|
|
* executable command that will run either locally or on a remote system, |
|
12
|
|
|
* as applicable. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ArgumentProcessor |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* selectArgs selects the appropriate set of arguments for the command |
|
18
|
|
|
* to be executed and orders them as needed. |
|
19
|
|
|
* |
|
20
|
|
|
* @param AliasRecord $siteAlias Description of |
|
21
|
|
|
* @param array $args Command and arguments to execute (source) |
|
22
|
|
|
* @param array $options key / value pair of option and value in include |
|
23
|
|
|
* in final arguments |
|
24
|
|
|
* @param array $optionsPassedAsArgs key / value pair of option and value |
|
25
|
|
|
* to include in final arguments after the '--' argument. |
|
26
|
|
|
* @return array Command and arguments to execute |
|
27
|
|
|
*/ |
|
28
|
|
|
public function selectArgs(AliasRecord $siteAlias, $args, $options = [], $optionsPassedAsArgs = []) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
// Split args into three arrays separated by the `--` |
|
31
|
|
|
list($leadingArgs, $dashDash, $remaingingArgs) = $this->findArgSeparator($args); |
|
32
|
|
|
$convertedOptions = $this->convertOptions($options); |
|
33
|
|
|
$convertedOptionsPassedAsArgs = $this->convertOptions($optionsPassedAsArgs); |
|
34
|
|
|
|
|
35
|
|
|
// If the caller provided options that should be passed as args, then we |
|
36
|
|
|
// always need a `--`, whether or not one existed to begin with in $args |
|
37
|
|
|
if (!empty($convertedOptionsPassedAsArgs)) { |
|
38
|
|
|
$dashDash = ['--']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
// Combine our separated args in the correct order. $dashDash will |
|
42
|
|
|
// always be `['--']` if $optionsPassedAsArgs or $remaingingArgs are |
|
43
|
|
|
// not empty, and otherwise will usually be empty. |
|
44
|
|
|
return array_merge( |
|
45
|
|
|
$leadingArgs, |
|
46
|
|
|
$convertedOptions, |
|
47
|
|
|
$dashDash, |
|
48
|
|
|
$convertedOptionsPassedAsArgs, |
|
49
|
|
|
$remaingingArgs |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* findArgSeparator finds the "--" argument in the provided arguments list, |
|
55
|
|
|
* if present, and returns the arguments in three sets. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array of three arrays, leading, "--" and trailing |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function findArgSeparator($args) |
|
60
|
|
|
{ |
|
61
|
|
|
$pos = array_search('--', $args); |
|
62
|
|
|
if ($pos === false) { |
|
63
|
|
|
return [$args, [], []]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return [ |
|
67
|
|
|
array_slice($args, 0, $pos), |
|
68
|
|
|
['--'], |
|
69
|
|
|
array_slice($args, $pos + 1), |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* convertOptions takes an associative array of options (key / value) and |
|
75
|
|
|
* converts it to an array of strings in the form --key=value. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $options in key => value form |
|
78
|
|
|
* @return array options in --option=value form |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function convertOptions($options) |
|
81
|
|
|
{ |
|
82
|
|
|
$result = []; |
|
83
|
|
|
foreach ($options as $option => $value) { |
|
84
|
|
|
if ($value === true || $value === null) { |
|
85
|
|
|
$result[] = "--$option"; |
|
86
|
|
|
} elseif ($value === false) { |
|
87
|
|
|
// Ignore this option. |
|
88
|
|
|
} else { |
|
89
|
|
|
$result[] = "--{$option}={$value}"; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $result; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.