80% of 587OPs |
88% of 109Lines |
65% of 83Branches |
25% of 84Paths |
# | |
---|---|
1 |
<?php |
2 | |
3 |
namespace mageekguy\atoum\cli; |
4 | |
5 |
use |
6 |
mageekguy\atoum |
7 |
; |
8 | |
9 |
class command |
10 |
{ |
11 |
protected $adapter = null; |
12 |
protected $binaryPath = ''; |
13 |
protected $options = array(); |
14 |
protected $arguments = array(); |
15 |
protected $env = array(); |
16 | |
17 |
private $processus = null; |
18 |
private $streams = array(); |
19 |
private $stdOut = ''; |
20 |
private $stdErr = ''; |
21 |
private $exitCode = null; |
22 | |
23 |
public function __construct($binaryPath = null, atoum\adapter $adapter = null)100% |
24 |
{ |
25 |
$this |
26 |
->setAdapter($adapter) |
27 |
->setBinaryPath($binaryPath) |
28 |
; |
29 |
} |
30 | |
31 |
public function __toString()92% |
32 |
{ |
33 |
$command = ''; |
34 | |
35 |
foreach ($this->options as $option => $value) |
36 |
{ |
37 |
$command .= ' ' . $option; |
38 | |
39 |
if ($value !== null) |
40 |
{ |
41 |
$command .= ' ' . $value; |
42 |
} |
43 |
} |
44 | |
45 |
if (sizeof($this->arguments) > 0) |
46 |
{ |
47 |
$command .= ' --'; |
48 | |
49 |
foreach ($this->arguments as $argument) |
50 |
{ |
51 |
$command .= ' ' . key($argument); |
52 | |
53 |
$value = current($argument); |
54 | |
55 |
if ($value !== null) |
56 |
{ |
57 |
$command .= ' ' . escapeshellarg($value); |
58 |
} |
59 |
} |
60 |
} |
61 | |
62 |
if (self::osIsWindows() === true) |
63 |
{ |
64 |
$command = '"' . $this->binaryPath . '"' . $command; |
65 |
} |
66 |
else |
67 |
{ |
68 |
$command = escapeshellcmd($this->binaryPath . $command); |
69 |
} |
70 | |
71 |
return $command; |
72 |
} |
73 | |
74 |
public function __set($envVariable, $value)0% |
75 |
{ |
76 |
$this->env[$envVariable] = $value; |
77 | |
78 |
return $this; |
79 |
} |
80 | |
81 |
public function __get($envVariable)0% |
82 |
{ |
83 |
return (isset($this->{$envVariable}) === false ? null : $this->env[$envVariable]); |
84 |
} |
85 | |
86 |
public function __isset($envVariable)0% |
87 |
{ |
88 |
return (isset($this->env[$envVariable]) === true); |
89 |
} |
90 | |
91 |
public function __unset($envVariable)0% |
92 |
{ |
93 |
if (isset($this->{$envVariable}) === true) |
94 |
{ |
95 |
unset($this->env[$envVariable]); |
96 |
} |
97 | |
98 |
return $this; |
99 |
} |
100 | |
101 |
public function reset()100% |
102 |
{ |
103 |
$this->options = array(); |
104 |
$this->arguments = array(); |
105 |
$this->stdOut = ''; |
106 |
$this->stdErr = ''; |
107 |
$this->exitCode = null; |
108 | |
109 |
return $this; |
110 |
} |
111 | |
112 |
public function getAdapter()100% |
113 |
{ |
114 |
return $this->adapter; |
115 |
} |
116 | |
117 |
public function setAdapter(atoum\adapter $adapter = null)100% |
118 |
{ |
119 |
$this->adapter = $adapter ?: new atoum\adapter(); |
120 | |
121 |
return $this; |
122 |
} |
123 | |
124 |
public function getBinaryPath()100% |
125 |
{ |
126 |
return $this->binaryPath; |
127 |
} |
128 | |
129 |
public function setBinaryPath($binaryPath = null) |
130 |
{ |
131 |
$this->binaryPath = (string) $binaryPath; |
132 | |
133 |
return $this; |
134 |
} |
135 | |
136 |
public function addOption($option, $value = null)100% |
137 |
{ |
138 |
$this->options[$option] = $value ?: null; |
139 | |
140 |
return $this; |
141 |
} |
142 | |
143 |
public function getOptions()100% |
144 |
{ |
145 |
return $this->options; |
146 |
} |
147 | |
148 |
public function addArgument($argument, $value = null)100% |
149 |
{ |
150 |
$this->arguments[] = array($argument => $value ?: null); |
151 | |
152 |
return $this; |
153 |
} |
154 | |
155 |
public function getArguments()100% |
156 |
{ |
157 |
return $this->arguments; |
158 |
} |
159 | |
160 |
public function isRunning()100% |
161 |
{ |
162 |
$isRunning = false; |
163 | |
164 |
if ($this->processus !== null) |
165 |
{ |
166 |
$this->stdOut .= $this->adapter->stream_get_contents($this->streams[1]); |
167 |
$this->stdErr .= $this->adapter->stream_get_contents($this->streams[2]); |
168 | |
169 |
$processusStatus = $this->adapter->proc_get_status($this->processus); |
170 | |
171 |
$isRunning = $processusStatus['running']; |
172 | |
173 |
if ($isRunning === false) |
174 |
{ |
175 |
$this->stdOut .= $this->adapter->stream_get_contents($this->streams[1]); |
176 |
$this->adapter->fclose($this->streams[1]); |
177 | |
178 |
$this->stdErr .= $this->adapter->stream_get_contents($this->streams[2]); |
179 |
$this->adapter->fclose($this->streams[2]); |
180 | |
181 |
$this->streams = array(); |
182 | |
183 |
$this->exitCode = $processusStatus['exitcode']; |
184 | |
185 |
$this->adapter->proc_close($this->processus); |
186 |
$this->processus = null; |
187 |
} |
188 |
} |
189 | |
190 |
return $isRunning; |
191 |
} |
192 | |
193 |
public function getStdout()100% |
194 |
{ |
195 |
return $this->stdOut; |
196 |
} |
197 | |
198 |
public function getStderr()100% |
199 |
{ |
200 |
return $this->stdErr; |
201 |
} |
202 | |
203 |
public function getExitCode()100% |
204 |
{ |
205 |
while ($this->isRunning() === true); |
206 | |
207 |
return $this->exitCode; |
208 |
} |
209 | |
210 |
public function run($stdin = '')93% |
211 |
{ |
212 |
if ($this->processus !== null) |
213 |
{ |
214 |
throw new command\exception('Unable to run \'' . $this . '\' because is currently running'); |
215 |
} |
216 | |
217 |
$pipes = array( |
218 |
1 => array('pipe', 'w'), |
219 |
2 => array('pipe', 'w') |
220 |
); |
221 | |
222 |
if ($stdin != '') |
223 |
{ |
224 |
$pipes[0] = array('pipe', 'r'); |
225 |
} |
226 | |
227 |
$this->processus = @call_user_func_array(array($this->adapter, 'proc_open'), array((string) $this, $pipes, & $this->streams, null, sizeof($this->env) <= 0 ? null : $this->env)); |
228 | |
229 |
if ($this->processus === false) |
230 |
{ |
231 |
throw new command\exception('Unable to run \'' . $this . '\''); |
232 |
} |
233 | |
234 |
if (isset($this->streams[0]) === true) |
235 |
{ |
236 |
while ($stdin != '') |
237 |
{ |
238 |
$stdinWrited = $this->adapter->fwrite($this->streams[0], $stdin, strlen($stdin)); |
239 | |
240 |
if ($stdinWrited === false) |
241 |
{ |
242 |
throw new command\exception('Unable to send \'' . $stdin . '\' to \'' . $this . '\''); |
243 |
} |
244 | |
245 |
$stdin = substr($stdin, $stdinWrited); |
246 |
} |
247 | |
248 |
$this->adapter->fclose($this->streams[0]); |
249 |
unset($this->streams[0]); |
250 |
} |
251 | |
252 |
$this->adapter->stream_set_blocking($this->streams[1], 0); |
253 |
$this->adapter->stream_set_blocking($this->streams[2], 0); |
254 | |
255 |
return $this; |
256 |
} |
257 | |
258 |
private static function osIsWindows()100% |
259 |
{ |
260 |
return (defined('PHP_WINDOWS_VERSION_MAJOR') === true); |
261 |
} |
262 |
} |