1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the php-ansible package. |
4
|
|
|
* |
5
|
|
|
* (c) Marc Aschmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Asm\Ansible\Command; |
12
|
|
|
|
13
|
|
|
use Asm\Ansible\Process\ProcessBuilder; |
14
|
|
|
use Symfony\Component\Process\Process; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AbstractAnsibleCommand |
18
|
|
|
* |
19
|
|
|
* @package Asm\Ansible\Command |
20
|
|
|
* @author Marc Aschmann <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractAnsibleCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ProcessBuilder |
26
|
|
|
*/ |
27
|
|
|
protected $processBuilder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $options; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $parameters; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $baseOptions; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ProcessBuilder $processBuilder |
46
|
|
|
*/ |
47
|
|
|
public function __construct(ProcessBuilder $processBuilder) |
48
|
|
|
{ |
49
|
|
|
$this->processBuilder = $processBuilder; |
50
|
|
|
$this->options = []; |
51
|
|
|
$this->parameters = []; |
52
|
|
|
$this->baseOptions = []; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get parameter string which will be used to call ansible. |
57
|
|
|
* |
58
|
|
|
* @param bool $asArray |
59
|
|
|
* @return string[] |
60
|
|
|
*/ |
61
|
|
|
protected function prepareArguments(bool $asArray = true) |
62
|
|
|
{ |
63
|
|
|
$arguments = array_merge( |
64
|
|
|
[$this->getBaseOptions()], |
65
|
|
|
$this->getOptions(), |
66
|
|
|
$this->getParameters() |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
if (false === $asArray) { |
70
|
|
|
$arguments = implode(' ', $arguments); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $arguments; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Add an Option. |
78
|
|
|
* |
79
|
|
|
* @param string $name |
80
|
|
|
* @param string $value |
81
|
|
|
*/ |
82
|
|
|
protected function addOption(string $name, string $value): void |
83
|
|
|
{ |
84
|
|
|
$this->options[$name] = $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add a parameter. |
89
|
|
|
* |
90
|
|
|
* @param string $name |
91
|
|
|
*/ |
92
|
|
|
protected function addParameter(string $name): void |
93
|
|
|
{ |
94
|
|
|
$this->parameters[] = $name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get all options as array. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
protected function getOptions(): array |
103
|
|
|
{ |
104
|
|
|
$options = []; |
105
|
|
|
|
106
|
|
|
foreach ($this->options as $name => $value) { |
107
|
|
|
$options[] = $name . '=' . $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $options; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get all parameters as array. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected function getParameters(): array |
119
|
|
|
{ |
120
|
|
|
return $this->parameters; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Add base options to internal storage. |
126
|
|
|
* |
127
|
|
|
* @param string $baseOption |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
protected function addBaseoption(string $baseOption) |
131
|
|
|
{ |
132
|
|
|
$this->baseOptions[] = $baseOption; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generate base options string. |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
protected function getBaseOptions(): string |
143
|
|
|
{ |
144
|
|
|
return implode(' ', $this->baseOptions); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Check if param is array or string and implode with glue if necessary. |
149
|
|
|
* |
150
|
|
|
* @param string|array $param |
151
|
|
|
* @param string $glue |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
protected function checkParam($param, string $glue = ' '): string |
155
|
|
|
{ |
156
|
|
|
if (is_array($param)) { |
157
|
|
|
$param = implode($glue, $param); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $param; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Creates process with processBuilder builder and executes it. |
165
|
|
|
* |
166
|
|
|
* @param null $callback |
167
|
|
|
* @return int|string |
168
|
|
|
*/ |
169
|
|
|
protected function runProcess($callback) |
170
|
|
|
{ |
171
|
|
|
$process = $this->processBuilder |
172
|
|
|
->setArguments( |
173
|
|
|
$this->prepareArguments() |
|
|
|
|
174
|
|
|
) |
175
|
|
|
->getProcess(); |
176
|
|
|
|
177
|
|
|
// exit code |
178
|
|
|
$result = $process->run($callback); |
179
|
|
|
|
180
|
|
|
// text-mode |
181
|
|
|
if (null === $callback) { |
182
|
|
|
$result = $process->getOutput(); |
183
|
|
|
|
184
|
|
|
if (false === $process->isSuccessful()) { |
185
|
|
|
$process->getErrorOutput(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $result; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.