1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Robo\Common; |
4
|
|
|
|
5
|
|
|
use Robo\Common\ProcessUtils; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Use this to add arguments and options to the $arguments property. |
9
|
|
|
*/ |
10
|
|
|
trait CommandArguments |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $arguments = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Pass argument to executable. Its value will be automatically escaped. |
19
|
|
|
* |
20
|
|
|
* @param string $arg |
21
|
|
|
* |
22
|
|
|
* @return $this |
23
|
|
|
*/ |
24
|
|
|
public function arg($arg) |
25
|
|
|
{ |
26
|
|
|
return $this->args($arg); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Pass methods parameters as arguments to executable. Argument values |
31
|
|
|
* are automatically escaped. |
32
|
|
|
* |
33
|
|
|
* @param string|string[] $args |
34
|
|
|
* |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function args($args) |
38
|
|
|
{ |
39
|
|
|
if (!is_array($args)) { |
40
|
|
|
$args = func_get_args(); |
41
|
|
|
} |
42
|
|
|
$this->arguments .= ' ' . implode(' ', array_map('static::escape', $args)); |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Pass the provided string in its raw (as provided) form as an argument to executable. |
48
|
|
|
* |
49
|
|
|
* @param string $arg |
50
|
|
|
* |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function rawArg($arg) |
54
|
|
|
{ |
55
|
|
|
$this->arguments .= " $arg"; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Escape the provided value, unless it contains only alphanumeric |
62
|
|
|
* plus a few other basic characters. |
63
|
|
|
* |
64
|
|
|
* @param string $value |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public static function escape($value) |
69
|
|
|
{ |
70
|
|
|
if (preg_match('/^[a-zA-Z0-9\/\.@~_-]+$/', $value)) { |
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
return ProcessUtils::escapeArgument($value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Pass option to executable. Options are prefixed with `--` , value can be provided in second parameter. |
78
|
|
|
* Option values are automatically escaped. |
79
|
|
|
* |
80
|
|
|
* @param string $option |
81
|
|
|
* @param string $value |
82
|
|
|
* @param string $separator |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function option($option, $value = null, $separator = ' ') |
87
|
|
|
{ |
88
|
|
|
if ($option !== null and strpos($option, '-') !== 0) { |
89
|
|
|
$option = "--$option"; |
90
|
|
|
} |
91
|
|
|
$this->arguments .= null == $option ? '' : " " . $option; |
92
|
|
|
$this->arguments .= null == $value ? '' : $separator . static::escape($value); |
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Pass multiple options to executable. The associative array contains |
98
|
|
|
* the key:value pairs that become `--key value`, for each item in the array. |
99
|
|
|
* Values are automatically escaped. |
100
|
|
|
* |
101
|
|
|
* @param array $options |
102
|
|
|
* @param string $separator |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function options(array $options, $separator = ' ') |
107
|
|
|
{ |
108
|
|
|
foreach ($options as $option => $value) { |
109
|
|
|
$this->option($option, $value, $separator); |
110
|
|
|
} |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Pass an option with multiple values to executable. Value can be a string or array. |
116
|
|
|
* Option values are automatically escaped. |
117
|
|
|
* |
118
|
|
|
* @param string $option |
119
|
|
|
* @param string|array $value |
120
|
|
|
* @param string $separator |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function optionList($option, $value = array(), $separator = ' ') |
125
|
|
|
{ |
126
|
|
|
if (is_array($value)) { |
127
|
|
|
foreach ($value as $item) { |
128
|
|
|
$this->optionList($option, $item, $separator); |
129
|
|
|
} |
130
|
|
|
} else { |
131
|
|
|
$this->option($option, $value, $separator); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|