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