|
1
|
|
|
<?php |
|
2
|
|
|
namespace Robo\Common; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\Process\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); |
|
|
|
|
|
|
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
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.