1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ballen\Executioner; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Executioner Process Execution Library |
7
|
|
|
* Executioner is a PHP library for executing system processes |
8
|
|
|
* and applications with the ability to pass extra arguments and read |
9
|
|
|
* CLI output results. |
10
|
|
|
* |
11
|
|
|
* @author Bobby Allen <[email protected]> |
12
|
|
|
* @package Ballen\Executioner |
13
|
|
|
* @license http://opensource.org/licenses/MIT |
14
|
|
|
* @link https://github.com/allebb/executioner |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
use Ballen\Collection\Collection; |
18
|
|
|
|
19
|
|
|
class Executioner |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The full system path to the application/process you want to exectute. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $applicationPath; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Stores application arguments to be parsed with the application. |
31
|
|
|
* |
32
|
|
|
* @var Collection |
33
|
|
|
*/ |
34
|
|
|
private $applicationArguments; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Stores the CLI response. |
38
|
|
|
* |
39
|
|
|
* @var Collection |
40
|
|
|
*/ |
41
|
|
|
private $executionResponse; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Stores the CLI execution errors. |
45
|
|
|
* |
46
|
|
|
* @var Collection |
47
|
|
|
*/ |
48
|
|
|
private $executionErrors; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Execute the command using sudo? |
52
|
|
|
* |
53
|
|
|
* @var boolean |
54
|
|
|
*/ |
55
|
|
|
private $sudo = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Redirect stderr to stdout? |
59
|
|
|
* |
60
|
|
|
* @var boolean |
61
|
|
|
*/ |
62
|
|
|
private $stdError = false; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Executioner constructor. |
66
|
|
|
*/ |
67
|
|
|
public function __construct() |
68
|
|
|
{ |
69
|
|
|
$this->applicationArguments = new Collection(); |
70
|
|
|
$this->executionResponse = new Collection(); |
71
|
|
|
$this->executionErrors = new Collection(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Optional way to create new instance |
76
|
|
|
* |
77
|
|
|
* @param string $application The command to run. |
78
|
|
|
* @return Executioner |
79
|
|
|
*/ |
80
|
|
|
public static function make($application) |
81
|
|
|
{ |
82
|
|
|
return (new Executioner())->setApplication($application); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Adds an argument to be added to the execution string. |
87
|
|
|
* |
88
|
|
|
* @param string $argument |
89
|
|
|
* @return Executioner |
90
|
|
|
*/ |
91
|
|
|
public function addArgument($argument) |
92
|
|
|
{ |
93
|
|
|
$this->applicationArguments->push($argument); |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the application and path of which to be executed. |
99
|
|
|
* |
100
|
|
|
* @param string $application The full system path to the application to execute. |
101
|
|
|
* @return Executioner |
102
|
|
|
*/ |
103
|
|
|
public function setApplication($application) |
104
|
|
|
{ |
105
|
|
|
$this->applicationPath = $application; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Retrieves the compiled command. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getCommand() |
115
|
|
|
{ |
116
|
|
|
return $this->compileCommand(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Compiles the command that will be executed |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
private function compileCommand() |
125
|
|
|
{ |
126
|
|
|
$command = ''; |
127
|
|
|
|
128
|
|
|
if ($this->sudo) { |
129
|
|
|
$command = 'sudo '; |
130
|
|
|
} |
131
|
|
|
$command .= escapeshellcmd($this->applicationPath) . $this->generateArguments(); |
132
|
|
|
if ($this->stdError) { |
133
|
|
|
$command .= ' 2>&1'; |
134
|
|
|
} |
135
|
|
|
return $command; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Generates a list of arguments to be appended onto the executed path. |
140
|
|
|
* |
141
|
|
|
* @return string The generated list of arguments. |
142
|
|
|
*/ |
143
|
|
|
protected function generateArguments() |
144
|
|
|
{ |
145
|
|
|
$arguments = ''; |
146
|
|
|
if (!$this->applicationArguments->isEmpty()) { |
147
|
|
|
$arguments = ' ' . $this->applicationArguments->implode(); |
148
|
|
|
} |
149
|
|
|
return $arguments; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Ensure that the process is called with 'sudo' (*NIX systems only) |
154
|
|
|
* |
155
|
|
|
* @param bool $enabled Enable (or disable) sudo. |
156
|
|
|
* @return Executioner |
157
|
|
|
*/ |
158
|
|
|
public function sudo($enabled = true) |
159
|
|
|
{ |
160
|
|
|
$this->sudo = $enabled; |
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Enable stderr redirection to stdout. |
166
|
|
|
* |
167
|
|
|
* @param boolean $enabled Enable (or disable) redirection to std. |
168
|
|
|
* @return Executioner |
169
|
|
|
*/ |
170
|
|
|
public function stderr($enabled = true) |
171
|
|
|
{ |
172
|
|
|
$this->stdError = $enabled; |
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Executes the process. |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
* @throws Exceptions\ExecutionException |
181
|
|
|
*/ |
182
|
|
|
private function executeProcess() |
183
|
|
|
{ |
184
|
|
|
$command = $this->compileCommand(); |
185
|
|
|
exec($command, $result, $status); |
186
|
|
|
if ($status > 0) { |
187
|
|
|
throw new Exceptions\ExecutionException( |
188
|
|
|
'Unknown error occurred when attempting to execute: ' . $command . PHP_EOL |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
return $result; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Executes the application with configured arguments. |
196
|
|
|
* |
197
|
|
|
* @return Executioner |
198
|
|
|
* @throws Exceptions\ExecutionException |
199
|
|
|
*/ |
200
|
|
|
public function execute() |
201
|
|
|
{ |
202
|
|
|
$this->executionResponse->reset()->push($this->executeProcess()); |
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Checks if the application/process is executable. |
208
|
|
|
* |
209
|
|
|
* @return boolean |
210
|
|
|
*/ |
211
|
|
|
public function isExecutable() |
212
|
|
|
{ |
213
|
|
|
return is_executable($this->applicationPath); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns an array of class error messages. |
218
|
|
|
* |
219
|
|
|
* @return Collection Collection of error messages. |
220
|
|
|
*/ |
221
|
|
|
public function getErrors() |
222
|
|
|
{ |
223
|
|
|
return $this->executionErrors; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Returns the result (STDOUT) as an array. |
228
|
|
|
* |
229
|
|
|
* @return array Result text (STDOUT). |
230
|
|
|
*/ |
231
|
|
|
public function resultAsArray() |
232
|
|
|
{ |
233
|
|
|
return $this->executionResponse->all()->toArray(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Returns the result (STDOUT) as a JSON string. |
238
|
|
|
* |
239
|
|
|
* @return string Result text (STDOUT). |
240
|
|
|
*/ |
241
|
|
|
public function resultAsJSON() |
242
|
|
|
{ |
243
|
|
|
return $this->executionResponse->all()->toJson(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns the result (STDOUT) as seralized data. |
248
|
|
|
* |
249
|
|
|
* @return string Result text (STDOUT). |
250
|
|
|
*/ |
251
|
|
|
public function resultAsSerialized() |
252
|
|
|
{ |
253
|
|
|
return serialize($this->executionResponse->all()->toArray()); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Returns the result (stdout) as a raw text string. |
258
|
|
|
* |
259
|
|
|
* @return string Result text (STDOUT). |
260
|
|
|
*/ |
261
|
|
|
public function resultAsText() |
262
|
|
|
{ |
263
|
|
|
$buffer = ''; |
264
|
|
|
foreach ($this->executionResponse->all()->toArray() as $stdout) { |
265
|
|
|
$buffer .= $stdout . PHP_EOL; |
266
|
|
|
} |
267
|
|
|
return $buffer; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|