1 | <?php |
||
14 | class Driver_Phantomjs_Connection { |
||
15 | |||
16 | /** |
||
17 | * The file storing the pid of the current phantomjs server process |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $_pid_file; |
||
21 | |||
22 | /** |
||
23 | * The pid of the current phantomjs server process |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $_pid; |
||
27 | |||
28 | /** |
||
29 | * Ulr of the phantomjs server |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $_server = 'http://localhost'; |
||
33 | |||
34 | /** |
||
35 | * Port of the phantomjs server |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $_port; |
||
39 | |||
40 | /** |
||
41 | * Phantomjs binary |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $_phantomjs_binary = 'phantomjs'; |
||
45 | |||
46 | 15 | /** |
|
47 | * Getter / Setter of the phantomjs server port. |
||
48 | 15 | * If none is set it tries to fine an unused port between 4445 and 5000 |
|
49 | 15 | * @param string $port |
|
50 | 2 | * @return string|Driver_Phantomjs_Connection |
|
51 | 2 | */ |
|
52 | public function port($port = NULL) |
||
67 | 15 | ||
68 | /** |
||
69 | 15 | * Getter / Setter of the current phantomjs server url |
|
70 | 15 | * @param string $server |
|
71 | 2 | * @return string|Driver_Phantomjs_Connection |
|
72 | 2 | */ |
|
73 | public function server($server = NULL) |
||
82 | |||
83 | 2 | /** |
|
84 | * Get the host of the current phantomjs server (without the protocol part) |
||
85 | * @return string |
||
86 | */ |
||
87 | public function host() |
||
91 | |||
92 | 1 | /** |
|
93 | * Getter, get the current phantomjs server process pid |
||
94 | * @return string |
||
95 | 3 | */ |
|
96 | public function pid() |
||
100 | 2 | ||
101 | 3 | public function __construct($server = NULL) |
|
108 | |||
109 | /** |
||
110 | * Start a new phantomjs server, optionally provide pid_file and log file. |
||
111 | 2 | * If you provide a pid_file, it will kill the process currently running on that pid, before starting the new one |
|
112 | * If the start is unsuccessfull it will return FALSE |
||
113 | * @param string $pid_file |
||
114 | 2 | * @param string $log_file |
|
115 | 1 | * @return boolean |
|
116 | 1 | */ |
|
117 | 1 | public function start($pid_file = NULL, $log_file = '/dev/null') |
|
142 | |||
143 | 2 | /** |
|
144 | * Check if the phantomjs server has been started |
||
145 | * @return boolean |
||
146 | */ |
||
147 | public function is_started() |
||
151 | |||
152 | 2 | /** |
|
153 | * Check if the phantomjs server is actually running (the port is taken) |
||
154 | * @return boolean |
||
155 | */ |
||
156 | public function is_running() |
||
160 | |||
161 | 2 | /** |
|
162 | 2 | * Gracefully stop the phantomjs server. Return FALSE on failure. Clear the pid_file if set |
|
163 | 2 | * @return boolean |
|
164 | 2 | */ |
|
165 | 2 | public function stop() |
|
183 | |||
184 | 1 | /** |
|
185 | * Getter - get the current pid_file |
||
186 | * @return string |
||
187 | */ |
||
188 | public function pid_file() |
||
192 | 10 | ||
193 | /** |
||
194 | 10 | * Perform a get request on the phantomjs server |
|
195 | * @param string $command |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function get($command) |
||
202 | |||
203 | 10 | /** |
|
204 | * Perform a post request on the phantomjs server |
||
205 | 10 | * @param string $command |
|
206 | 10 | * @param array $params |
|
207 | 10 | * @return mixed |
|
208 | */ |
||
209 | 10 | public function post($command, array $params) |
|
217 | 3 | ||
218 | /** |
||
219 | 3 | * Perform a delete request on the phantomjs server |
|
220 | 3 | * @param string $command |
|
221 | * @return mixed |
||
222 | 3 | */ |
|
223 | public function delete($command) |
||
230 | 15 | ||
231 | /** |
||
232 | 15 | * Get the full url of a command (including server and port) |
|
233 | * @param string $command |
||
234 | * @return string |
||
235 | */ |
||
236 | public function command_url($command) |
||
240 | |||
241 | 14 | /** |
|
242 | * Set phantomjs binary location |
||
243 | 14 | * @param string $binary |
|
244 | 14 | * @throws Exception |
|
245 | 14 | */ |
|
246 | 14 | public function set_phantomjs_binary($binary) |
|
247 | { |
||
248 | 14 | $this->_phantomjs_binary = $binary; |
|
249 | } |
||
250 | 14 | ||
251 | /** |
||
252 | 14 | * Perform a custom request on the phantomjs server, using curl |
|
253 | 14 | * @param string $command |
|
254 | 14 | * @param array $options |
|
255 | 14 | * @return mixed |
|
256 | */ |
||
257 | 14 | protected function call($command, array $options = array()) |
|
258 | 14 | { |
|
259 | $curl = curl_init(); |
||
260 | 14 | $options[CURLOPT_URL] = $this->command_url($command); |
|
261 | $options[CURLOPT_RETURNTRANSFER] = TRUE; |
||
262 | $options[CURLOPT_FOLLOWLOCATION] = TRUE; |
||
263 | 14 | ||
264 | curl_setopt_array($curl, $options); |
||
265 | 14 | ||
266 | 14 | $raw = ''; |
|
267 | |||
268 | 14 | Attempt::make(function() use ($curl, & $raw) { |
|
269 | $raw = trim(curl_exec($curl)); |
||
270 | 14 | return curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200; |
|
271 | }); |
||
272 | |||
273 | $error = curl_error($curl); |
||
274 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
275 | |||
276 | curl_close($curl); |
||
277 | |||
278 | if ($error) |
||
279 | throw new Exception_Driver('Curl ":command" throws exception :error', array(':command' => $command, ':error' => $error)); |
||
280 | |||
281 | if ($code != 200) |
||
282 | throw new Exception_Driver('Unexpected response from the panthomjs for :command: :code', array(':command' => $command, ':code' => $code)); |
||
283 | |||
284 | $result = json_decode($raw, TRUE); |
||
285 | |||
286 | return $result; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * Start a phantomjs server in the background. Set port, server js file, additional files and log file. |
||
291 | * |
||
292 | * @param string $file the server js file |
||
293 | * @param integer $port the port to start the server on |
||
294 | * @param string $additional additional file, passed to the js server |
||
295 | * @param string $log_file |
||
296 | * @return string the pid of the newly started process |
||
297 | * @throws Exception |
||
298 | */ |
||
299 | protected function _start($file, $port, $additional = NULL, $log_file = '/dev/null') |
||
300 | { |
||
301 | if ( ! Network::is_port_open('localhost', $port)) { |
||
302 | throw new Exception('Port :port is already taken', [':port' => $port]); |
||
303 | } |
||
304 | |||
305 | if ($log_file !== '/dev/null' AND ! is_file($log_file)) { |
||
306 | throw new Exception('Log file (:log_file) must be a file or /dev/null', [':log_file' => $log_file]); |
||
307 | } |
||
308 | |||
309 | return shell_exec(strtr('nohup :command > :log 2> :log & echo $!', array( |
||
310 | ':command' => $this->_command($file, $port, $additional), |
||
311 | ':log' => $log_file, |
||
312 | ))); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * kill a server on a given pid |
||
317 | * @param string $pid |
||
318 | */ |
||
319 | protected function _kill($pid) |
||
323 | |||
324 | /** |
||
325 | * Return the command to start the phantomjs server |
||
326 | * |
||
327 | * @param string $file the server js file |
||
328 | * @param integer $port |
||
329 | * @param string $additional additional js file |
||
330 | * @return string |
||
331 | * @throws Exception |
||
332 | */ |
||
333 | protected function _command($file, $port, $additional = NULL) |
||
355 | } |
||
356 |